How to prevent MATLAB printing false space and use wrong fonts?
Matlab 2015a inserts space before µ in long strings, but not in short ones (xlabel). In some cases one can work around by using UTF-8 letters, but this will fail in other situations (see ^2)
The font of the text should be Helvetica
but it looks different. Although get Fontname
returns Helvetica
.
This is a bug in MATLAB and already reported. I do not want to wait months until MATHWORKS fixes this bug.
How can I fix this bug my self? I tried to change the renderer to opengl
, but this mixes up all fonts even worse.
plot([2014 2015 2016], [0 1 0])
xlabel('MATLAB (mu)')
ylabel('Space-bugs (mum^2) (µm²) (µm^2)')
title('Number of wrong spaces in MATLAB')
textTT = text(2015-1, 1/2,'ugliest plot bugs ever', 'FontSize', 20, 'FontName', 'Helvetica');
text(2015, 1/3, get(textTT, 'Fontname')) % says Helvetica, but lies.
print -depsc2 -r864 -painters '/tmp/test.eps'
print -dpdf -r864 -painters '/tmp/test.pdf'
code rendered in Matlab 2015a on Linux 64 Bit
same code rendered in Matlab Windows 64 Bit by Luis Mendo:
EDIT 1: The bug is still present in 2015b and 2016a. The bug shows up in the Linux version, but not in the Windows version.
EDIT 2: Some suggested to use the internal latex interpreter. I think this is no solution, because it makes the code really hard to read if all fonts get replaced to slanted (or italic?) computer modern. There should be one font for the plot and units, title and labels must be written upright.
EDIT 3: The bug is still present in 2017b.
I'm using R2015b on my linux machine, but I get the same issues when printing.
All the strings shown in a plot are passed to an interpreter before they are passed to the renderer. There are three modes for the interpreter 'tex' (default)
, 'latex'
and 'none'
.
Changing Fonts
The TeX / LaTeX interpreter will not check your OS fonts, but brings its own set of fonts for displaying included here:
C:Program FilesMATLAB<version>sysfontsttfcm
<MATLAB root><version>sysfontsttfcm
For me I have the first part of the BaKoMa Fonts Collection (according to my readme). The following fonts are aviable (to me):
============================ Basic text fonts ==========================
cmr 5 6 7 8 9 10 12 17
cmbx 5 6 7 8 9 10 12
cmti 7 8 9 10 12
cmsl 8 9 10 12
cmcsc 8* 9* 10
cmtex 8 9 10
cmb 10
cmbxsl 10
cmbxti 10
where cm = computer modern
(from what I can tell).
So even if you tell matlab to plot it in Helvetica, matlab passes it to TeX and TeX doesn't know what Helvetica is and just uses its default font instead. One could think about adding helvetica manualy, but I didn't explore that further.
Dealing with the (annoying) spaces
To remove the spaces in ylabel
you can pass a latex string. For this change
ylabel('Space-bugs (mum^2) (µm²) (µm^2)')
to
ylabel('Space-bugs ($mu m^2$) ($mu m^2$) ($mu m^2$)','interpreter','latex');
However this will use the cmr
(computer modern regular) font and not Helvetica, but remove the space bug.
To avoid a mix of fonts in the plot, the interpreter of all text fields can be changed to latex
. Unless you have the computer modern
font aviable on your OS, in which case you can simply set the appropriate font type in matlab.