In general, using Matlab with the LaTeX interpreter works fine. Today I focus a really stupid bug in the visualization of the minus sign.

In Matlab I write a text with the LaTeX string $K_{-1}$ and it simply shows the output of $K_{1}$. On the one hand the minus sign does not work. On the other hand the plus sign and plenty of other signs work.

I have found some "solutions" but they do not work.

1. The first one tries to link another LaTeX version to the Matlab installation directory, so that Matlab should use the newer version of LaTeX. But it does not work for me.

i solved this problem linking my latex installation inside the base latex directory of matlab

1. rename (or delete if u prefer) /installation_matlab_path/matlab/sys/tex/latex/base in /installation_matlab_path/matlab/sys/tex/latex/old_base
2. cd installation_matlab_path/matlab/sys/tex/latex/
3. ln -s /usr/share/texmf-texlive/tex/latex/base/ base

if u don't know where's your latex installation path just search for a file inside the tex/latex/base directory

2. The second one uses additional braces to differenciate between the minus sign and the following characters but it does not work either.

Instead of $^{-1}$, try $^{{-}1}$. That is include the minus sign in its own braces. It worked for me.

Perhaps, anyone can help me out with this problem.

Thanks!

• Maybe this is looking ok for you: $K^{{}^{\_}1}$ ;-) – matheburg Jul 25 '13 at 15:08
• Can you define macros? If so, how about defining one that just inserts the minus? I'm not sure what's going with MATLAB that might be causing the problem, so I'm just guessing... – Werner Jul 25 '13 at 18:53
• The workaround of matheburg works for me. But instead of {}^{\_} I use the negative space \!^{\_}. Thanks for that comment! – who9vy Jul 25 '13 at 18:55

Currently, I solved the problem by a small workaround but better / correct solutions are welcome. The workaround has been proposed by matheburg. Thanks for that!

Instead of $K_{-1}$, I now use $K_{\!^{\_}1}$. It sounds weird, but it looks similar to the minus sign. The underline is visible, so that I add an invisible negative space \! and take the underline \_ as superscript.

When you ask MATLAB to display text on a graphics object (e.g., an axis label) with the LaTeX interpreter, MATLAB essentially generates a dvi file with the tex.m function, then converts this dvi file into an array of doubles, and then passes this array of double to its graphics engine for displaying. Portions of the MATLAB "TeX" process are open source, but not free, while the graphic display process is entirely closed source. Looking through the source code and the documentation of the tex.m function one can see that it takes a string argument and returns an array of doubles corresponding to the dvi file produced by the MATLAB TeX engine. The documentation says

The MATLAB implementation of TeX is compiled from Donald Knuth's original TeX parser (version: 3.14159) located on the TeX Archive Network. The LaTeX distribution was also obtained from the TeX Archive Network.

This makes it possible to test if the MATLAB TeX implementation is working. For example, running the MATLAB command

dvi = tex('$K_{-1}$');


results in MATLAB processing the following code with LaTeX:

\nofiles \documentclass{mwarticle} \begin{document}\setbox0=\hbox{$K_{-1}$}\copy0\special{bounds: \the\wd0 \the\ht0 \the\dp0} \end{document}


This allows you to confirm that the MATLAB implementation of LaTeX is correct by comparing the array of doubles produced by MATLAB to a dvi file produced by a known working local installation of LaTeX (e.g., TeX Live). It is also possible to write the array of doubles returned by tex('$K_{-1}$') into a file

fid = fopen('test.dvi', 'w');
fwrite(fid, tex('$K_{-1}$'), 'uint8');
fclose(fid);


If this test.dvi file displays correctly in a standard DVI viewer, then you can be confident that the MATLAB installation of LaTeX is working correctly and your computer can display minus signs. If however, a command like

text(0.5,0.5,'$K_{-1}$', 'interpreter', 'latex', 'FontSize', 20)


produces incorrect results, then the problem lies in the MATLAB graphical display system and really has nothing to do with LaTeX. The MATLAB graphical system has a large number of documented and undocumented bugs. Some of these are related to font and font embedding problems (e.g., this problem about Linux fonts and this one about problems with Adobe Illustrator displaying a minus sign).

• Yes, it works. The minus sign is there. Do you know a possibility to add such a file (or better the perfect crop of this) to the legend of a plot? – who9vy Jul 25 '13 at 18:52