10

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 Matlab

same code rendered in Matlab Windows 64 Bit by Luis Mendo: 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.

EDIT 4: The bug seems to be fixed in 2022a. (7 years after reporting)

Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
  • By any chance using *LaTeX*? – knedlsepp Apr 01 '15 at 23:48
  • 2
    one problem here is that Matlab strings are [decidedly *not* Unicode](http://www.mathworks.com/help/matlab/ref/strings.html), so the fact that µ works, at all, is amazing in and of itself. You probably want to throw in a [unicode2native](http://www.mathworks.com/help/matlab/ref/unicode2native.html) at the very least. – Mike 'Pomax' Kamermans Apr 02 '15 at 04:07
  • 2
    On my R2015a (Windows 7, 64 bits) the ylabel doesn't have that unwanted spacce – Luis Mendo Apr 02 '15 at 04:20
  • @knedlsepp I have tried LaTeX, but the text looks completely different then. And everything is slanted or italic, when it should not be. – Jonas Stein Mar 13 '16 at 20:09
  • @LuisMendo could you provide a picture of the same code on your windows matlab? – Jonas Stein Mar 13 '16 at 20:11
  • 1
    @JonasStein There you go. I'm using 2015b now (still on Windows 7 64 bits). Matlab figure: http://s000.tinyupload.com/index.php?file_id=13781200548861023685 Printed to pdf: http://s000.tinyupload.com/index.php?file_id=18224558652822484724 – Luis Mendo Mar 13 '16 at 20:25
  • 1
    @JonasStein Figure saved as png, to match that in your question: http://s000.tinyupload.com/index.php?file_id=07231175977320797378 – Luis Mendo Mar 13 '16 at 20:27
  • Can't replicate in R2015b on OS X so this may be a Linux issue. I generally export all of my publication figures to EPS or PDF and do final editing in a program like Adobe Illustrator (Inkscape might be an alternative on Linux). It's just too much trouble and often impossible to get quality results with Matlab alone. – horchler Mar 13 '16 at 22:13
  • 2
    And that's definitely not Helvetica of any kind. It's substituting something else (poorly). I'm not sure Helvetica even comes with Windows or Linux unless you purchased it. Have you tried Arial? Does the problem occur with other typefaces? And [this](http://www.mathworks.com/matlabcentral/answers/93500-why-is-helvetica-the-default-axes-font-type-in-matlab-when-this-font-does-not-exist-on-my-windows) might be relevant. – horchler Mar 13 '16 at 22:18
  • 1
    I had similar problems. Solved by: `set(0,'defaulttextinterpreter','latex');`. – peng Mar 13 '16 at 22:50
  • @peng could you provide a picture of the example above with your latex code? I tried it, but could not get the fonts right. I do not want to have slanted times here and upright helvetica there... A sans serif font is a good choice for a diagram, one with serifs is fine for long text. – Jonas Stein Mar 13 '16 at 22:57
  • @Mike'Pomax'Kamermans Thank you for the interesting links! I do not understand, how to fix the `mu` problem with `unicode2native`. Can you provide more information, please? – Jonas Stein Mar 13 '16 at 23:10
  • I can't, but I have google, and so do you - something like http://www.mathworks.com/matlabcentral/newsreader/view_thread/36238 maybe? – Mike 'Pomax' Kamermans Mar 14 '16 at 00:04
  • 5
    horchler is correct here, Windows is sneaky about Helvetica and will silently substitute it with Arial (there’s a registry key deep in Windows territory that aliases Helvetica for Arial: https://technet.microsoft.com/en-us/library/cc939628.aspx). In fact this is a common pitfall for web designer not specific to MATLAB.. – Amro Mar 14 '16 at 00:04
  • 3
    @Mike'Pomax'Kamermans about your earlier comment, starting with R2014b (new HG2 graphics), MATLAB can handle unicode strings much better, both as strings or in plots: http://www.mathworks.com/products/matlab/matlab-graphics/#multilingual_text_and_symbols. Example: `mu=char(956); text(0.5,0.5,mu); xlabel(mu); title(mu)` – Amro Mar 14 '16 at 00:17
  • 3
    peng's suggestion is equivalent to explicitly specifying the `Interpreter` property, as in: `xlabel(str, 'Interpreter','latex')` (same for `title` and `text` calls): http://www.mathworks.com/help/matlab/creating_plots/text-with-mathematical-expressions-using-latex.html. Note that "tex" and "latex" interpreters are not the same thing: http://www.mathworks.com/help/matlab/creating_plots/greek-letters-and-special-characters-in-graph-text.html – Amro Mar 14 '16 at 00:24
  • @JonasStein: a quick demo: http://pastebin.com/P19phFVn, result: http://i.stack.imgur.com/NBYky.png – Amro Mar 14 '16 at 00:57
  • @JonasStein Sorry, I have currently no matlab available.I did this some years ago. I executed this command in the beginning of the script to set the interpreter globally. It worked fine for me because I can handle all the labeling issues like in latex. – peng Mar 14 '16 at 01:25
  • @JonasStein Did anything help? – peng Mar 15 '16 at 09:57
  • @JonasStein Doing this from a windows machine at the moment. Try placing the contents of `ylabel=` to the string you want to display so it would read `ylabel=y`. Not absolutely sure if it would change anything but there may be a chance that it does correct it, so might as well try it. – Jouster500 Mar 17 '16 at 22:52
  • @JonasStein can you try running that small demo on a Linux/Mac? is the "extra space" bug present in all of them? Also while you're at it, test it by changing `text` calls into `xlabel`, `ylabel` and `title` to see if you get the same issue. – Amro Mar 19 '16 at 03:23
  • Couldn't reproduce on linux 2014a – xvan Mar 19 '16 at 19:24
  • @xvan As written the bug is in 2015a till now in all Matlab versions for Linux – Jonas Stein Mar 20 '16 at 20:37
  • @JonasStein I know, just added that to prevent anybody else trying to reproduce on an older version. – xvan Mar 20 '16 at 20:47
  • @JonasStein which cases exhibit the bug? It would be helpful if you could run the [test](http://pastebin.com/P19phFVn) in your environment and show the output (I'm on Windows so cant test it)... Also if you could try the same code but replace the calls with xlabel/ylabel/title. Just to be clear, by "bug" I mean the extra-space issue. As for the helvetica thing, you gotta use a cross-platform font like I explained.. – Amro Mar 21 '16 at 10:57
  • @Amro is it correct, that the plain line in your test is just an `u`? I will add the output to the question. – Jonas Stein Mar 21 '16 at 18:33
  • 1
    Matlab with HG2 (R2014b and newer) is internally spacing the characters differently than they end up in the EPS/PDF output. The result of this is that all of the labels that are supposed to be centered are ever-so-slightly too far left. Matlab tries to set text as long runs of strings in the EPS, but superscripts are a different size, so they get a new block of text which is placed at the point matlab thinks it should be, leaving a gap. For whatever reason spacing is correct for the latex font and courier, but not for any others I have tried. – arcticmac Jun 24 '16 at 04:55
  • @JonasStein Please mark user2457516 's answer if you consider it solved with this. The question is rather inactive, but keeps lingering in the unanswered list. – tvo Sep 09 '16 at 15:05

1 Answers1

2

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:

  1. Windows: C:\Program Files\MATLAB\<version>\sys\fonts\ttf\cm
  2. Linux: <MATLAB root>\<version>\sys\fonts\ttf\cm

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.