Matlab-Latex interface

I would like to welcome guest blogger Yaroslav Don, known to vim users as the author of the Matlab color-scheme. Today Yaroslav discusses some little-known aspects of Matlab’s support for Latex.

One of the most appealing aspects of Matlab is the cross-talk with other programs via a friendly user-interface. An excellent example is the Latex interpreter inside the Handle-graphics axes and texts, which comes to a good use to many inside the scientific community. In recent years I’ve noticed a slow but steady tendency of degrading the Latex capabilities inside Matlab. The most apparent example is the decreasing fonts; I’ll show its downward progression below:

Matlab R2010a

When I started using Latex in Matlab, I wrote a small script (latexfonts.m) to check the available Latex options in Matlab. In the figure below several attributes are depicted: font-families (roman, sans-serif and typewriter), font-shapes (upright, slanted, italics and small-caps) and font-series (medium and bold). It also shows other tweaks such as kerning, ligatures, underline, etc.

Latex support in R2010a

Latex support in R2010a

It is clearly seen that most of the Latex options above are available. Analyzing further, the list of fonts inside Matlab is quite vast, as seen below:

>> dir(fullfile(matlabroot,'sys','fonts','ttf','cm'))
.             cmbxsl10.ttf  cmmi12.ttf    cmr7.ttf      cmssi12.ttf   cmti10.ttf    eufb5.ttf     eurb9.ttf     eusm7.ttf     
..            cmbxti10.ttf  cmmi5.ttf     cmr8.ttf      cmssi17.ttf   cmti12.ttf    eufb6.ttf     eurm10.ttf    eusm8.ttf     
README        cmcsc10.ttf   cmmi6.ttf     cmr9.ttf      cmssi8.ttf    cmti7.ttf     eufb7.ttf     eurm5.ttf     eusm9.ttf     
cmb10.ttf     cmcsc8.ttf    cmmi7.ttf     cmsl10.ttf    cmssi9.ttf    cmti8.ttf     eufb8.ttf     eurm6.ttf     msam10.ttf    
cmbsy10.ttf   cmcsc9.ttf    cmmi8.ttf     cmsl12.ttf    cmssq8.ttf    cmti9.ttf     eufb9.ttf     eurm7.ttf     msam5.ttf     
cmbsy6.ttf    cmdunh10.ttf  cmmi9.ttf     cmsl8.ttf     cmssqi8.ttf   cmtt10.ttf    eufm10.ttf    eurm8.ttf     msam6.ttf     
cmbsy7.ttf    cmex10.ttf    cmmib10.ttf   cmsl9.ttf     cmsy10.ttf    cmtt12.ttf    eufm5.ttf     eurm9.ttf     msam7.ttf     
cmbsy8.ttf    cmex7.ttf     cmmib6.ttf    cmsltt10.ttf  cmsy5.ttf     cmtt8.ttf     eufm6.ttf     eusb10.ttf    msam8.ttf     
cmbsy9.ttf    cmex8.ttf     cmmib7.ttf    cmss10.ttf    cmsy6.ttf     cmtt9.ttf     eufm7.ttf     eusb5.ttf     msam9.ttf     
cmbx10.ttf    cmex9.ttf     cmmib8.ttf    cmss12.ttf    cmsy7.ttf     cmu10.ttf     eufm8.ttf     eusb6.ttf     msbm10.ttf    
cmbx12.ttf    cmff10.ttf    cmmib9.ttf    cmss17.ttf    cmsy8.ttf     cmvtt10.ttf   eufm9.ttf     eusb7.ttf     msbm5.ttf     
cmbx5.ttf     cmfi10.ttf    cmr10.ttf     cmss8.ttf     cmsy9.ttf     euex10.ttf    eurb10.ttf    eusb8.ttf     msbm6.ttf     
cmbx6.ttf     cmfib8.ttf    cmr12.ttf     cmss9.ttf     cmtcsc10.ttf  euex7.ttf     eurb5.ttf     eusb9.ttf     msbm7.ttf     
cmbx7.ttf     cminch.ttf    cmr17.ttf     cmssbx10.ttf  cmtex10.ttf   euex8.ttf     eurb6.ttf     eusm10.ttf    msbm8.ttf     
cmbx8.ttf     cmitt10.ttf   cmr5.ttf      cmssdc10.ttf  cmtex8.ttf    euex9.ttf     eurb7.ttf     eusm5.ttf     msbm9.ttf     
cmbx9.ttf     cmmi10.ttf    cmr6.ttf      cmssi10.ttf   cmtex9.ttf    eufb10.ttf    eurb8.ttf     eusm6.ttf     
 
>> dir(fullfile(matlabroot,'sys','fonts','type1','cm'))
.             cmbxsl10.pfm  cmmi12.pfm    cmr6.pfm      cmss17.pfm    cmssqi8.pfm   cmti10.pfm    lasy10.pfm    lcmssi8.pfm   
..            cmbxti10.pfb  cmmi5.pfb     cmr7.pfb      cmss8.pfb     cmsy10.pfb    cmti12.pfb    lasy5.pfb     line10.pfb    
README        cmbxti10.pfm  cmmi5.pfm     cmr7.pfm      cmss8.pfm     cmsy10.pfm    cmti12.pfm    lasy5.pfm     line10.pfm    
cmb10.pfb     cmcsc10.pfb   cmmi6.pfb     cmr8.pfb      cmss9.pfb     cmsy5.pfb     cmti7.pfb     lasy6.pfb     linew10.pfb   
cmb10.pfm     cmcsc10.pfm   cmmi6.pfm     cmr8.pfm      cmss9.pfm     cmsy5.pfm     cmti7.pfm     lasy6.pfm     linew10.pfm   
cmbsy10.pfb   cmdunh10.pfb  cmmi7.pfb     cmr9.pfb      cmssbx10.pfb  cmsy6.pfb     cmti8.pfb     lasy7.pfb     logo10.pfb    
cmbsy10.pfm   cmdunh10.pfm  cmmi7.pfm     cmr9.pfm      cmssbx10.pfm  cmsy6.pfm     cmti8.pfm     lasy7.pfm     logo10.pfm    
cmbx10.pfb    cmex10.pfb    cmmi8.pfb     cmsl10.pfb    cmssdc10.pfb  cmsy7.pfb     cmti9.pfb     lasy8.pfb     logo8.pfb     
cmbx10.pfm    cmex10.pfm    cmmi8.pfm     cmsl10.pfm    cmssdc10.pfm  cmsy7.pfm     cmti9.pfm     lasy8.pfm     logo8.pfm     
cmbx12.pfb    cmff10.pfb    cmmi9.pfb     cmsl12.pfb    cmssi10.pfb   cmsy8.pfb     cmtt10.pfb    lasy9.pfb     logo9.pfb     
cmbx12.pfm    cmff10.pfm    cmmi9.pfm     cmsl12.pfm    cmssi10.pfm   cmsy8.pfm     cmtt10.pfm    lasy9.pfm     logo9.pfm     
cmbx5.pfb     cmfi10.pfb    cmmib10.pfb   cmsl8.pfb     cmssi12.pfb   cmsy9.pfb     cmtt12.pfb    lasyb10.pfb   logobf10.pfb  
cmbx5.pfm     cmfi10.pfm    cmmib10.pfm   cmsl8.pfm     cmssi12.pfm   cmsy9.pfm     cmtt12.pfm    lasyb10.pfm   logobf10.pfm  
cmbx6.pfb     cmfib8.pfb    cmr10.pfb     cmsl9.pfb     cmssi17.pfb   cmtcsc10.pfb  cmtt8.pfb     lcircle1.pfb  logosl10.pfb  
cmbx6.pfm     cmfib8.pfm    cmr10.pfm     cmsl9.pfm     cmssi17.pfm   cmtcsc10.pfm  cmtt8.pfm     lcircle1.pfm  logosl10.pfm  
cmbx7.pfb     cminch.pfb    cmr12.pfb     cmsltt10.pfb  cmssi8.pfb    cmtex10.pfb   cmtt9.pfb     lcirclew.pfb  
cmbx7.pfm     cminch.pfm    cmr12.pfm     cmsltt10.pfm  cmssi8.pfm    cmtex10.pfm   cmtt9.pfm     lcirclew.pfm  
cmbx8.pfb     cmitt10.pfb   cmr17.pfb     cmss10.pfb    cmssi9.pfb    cmtex8.pfb    cmu10.pfb     lcmss8.pfb    
cmbx8.pfm     cmitt10.pfm   cmr17.pfm     cmss10.pfm    cmssi9.pfm    cmtex8.pfm    cmu10.pfm     lcmss8.pfm    
cmbx9.pfb     cmmi10.pfb    cmr5.pfb      cmss12.pfb    cmssq8.pfb    cmtex9.pfb    cmvtt10.pfb   lcmssb8.pfb   
cmbx9.pfm     cmmi10.pfm    cmr5.pfm      cmss12.pfm    cmssq8.pfm    cmtex9.pfm    cmvtt10.pfm   lcmssb8.pfm   
cmbxsl10.pfb  cmmi12.pfb    cmr6.pfb      cmss17.pfb    cmssqi8.pfb   cmti10.pfb    lasy10.pfb    lcmssi8.pfb   
 
>> dir(fullfile(matlabroot,'sys','tex','tfm'))
.               cmbxsl10.tfm    cmmi12.tfm      cmr6.tfm        cmssdc10.tfm    cmtex8.tfm      icmmi8.tfm      lcmss8.tfm      
..              cmbxti10.tfm    cmmi5.tfm       cmr7.tfm        cmssi10.tfm     cmtex9.tfm      icmsy8.tfm      lcmssb8.tfm     
cmb10.tfm       cmcsc10.tfm     cmmi6.tfm       cmr8.tfm        cmssi12.tfm     cmti10.tfm      icmtt8.tfm      lcmssi8.tfm     
cmbsy10.tfm     cmcsc8.tfm      cmmi7.tfm       cmr9.tfm        cmssi17.tfm     cmti12.tfm      ilasy8.tfm      line10.tfm      
cmbsy5.tfm      cmcsc9.tfm      cmmi8.tfm       cmsa10.tfm      cmssi8.tfm      cmti7.tfm       ilcmss8.tfm     linew10.tfm     
cmbsy6.tfm      cmdunh10.tfm    cmmi9.tfm       cmsl10.tfm      cmssi9.tfm      cmti8.tfm       ilcmssb8.tfm    manfnt.tfm      
cmbsy7.tfm      cmex10.tfm      cmmib10.tfm     cmsl12.tfm      cmssq8.tfm      cmti9.tfm       ilcmssi8.tfm    old_manfnt.tfm  
cmbsy8.tfm      cmex7.tfm       cmmib5.tfm      cmsl8.tfm       cmssqi8.tfm     cmtt10.tfm      lasy10.tfm      trip.tfm        
cmbsy9.tfm      cmex8.tfm       cmmib6.tfm      cmsl9.tfm       cmsy10.tfm      cmtt12.tfm      lasy5.tfm       
cmbx10.tfm      cmex9.tfm       cmmib7.tfm      cmsltt10.tfm    cmsy5.tfm       cmtt8.tfm       lasy6.tfm       
cmbx12.tfm      cmff10.tfm      cmmib8.tfm      cmss10.tfm      cmsy6.tfm       cmtt9.tfm       lasy7.tfm       
cmbx5.tfm       cmfi10.tfm      cmmib9.tfm      cmss12.tfm      cmsy7.tfm       cmu10.tfm       lasy8.tfm       
cmbx6.tfm       cmfib8.tfm      cmr10.tfm       cmss17.tfm      cmsy8.tfm       cmvtt10.tfm     lasy9.tfm       
cmbx7.tfm       cminch.tfm      cmr12.tfm       cmss8.tfm       cmsy9.tfm       dummy.tfm       lasyb10.tfm     
cmbx8.tfm       cmitt10.tfm     cmr17.tfm       cmss9.tfm       cmtcsc10.tfm    icmcsc10.tfm    lcircle10.tfm   
cmbx9.tfm       cmmi10.tfm      cmr5.tfm        cmssbx10.tfm    cmtex10.tfm     icmex10.tfm     lcirclew10.tfm

Matlab R2011b

Each time a new Matlab version is introduced, the joy of getting many fresh or upgraded features is mixed with the worry of getting some good-old ones removed. Unfortunately, such a regression was exactly the case in the R2011b version (and to this day in R2013a), as seen below:

Latex support in R2011b

Latex support in R2011b

Suddenly, many of the former features are missing and a cascade of warnings is thrown:

Warning: Font cmss10 is not supported. 
Warning: Font cmss10 is not supported. 
Warning: Font cmss10 is not supported. 
Warning: Font cmss10 is not supported. 
Warning: Font cmssi10 is not supported. 
Warning: Font cmssi10 is not supported. 
Warning: Font cmssi10 is not supported. 
Warning: Font cmssi10 is not supported. 
Warning: Font cmcsc10 is not supported. 
Warning: Font cmcsc10 is not supported. 
Warning: Font cmcsc10 is not supported. 
Warning: Font cmcsc10 is not supported. 
Warning: Font cmcsc10 is not supported. 
Warning: Font cmcsc10 is not supported. 
Warning: Unable to interpret LaTeX string "\ttfamily\scshape\mdseries WALT bla fi f{}i ff" 
Warning: Font cmssbx10 is not supported. 
...

Note: you can turn these warnings off using:

>> warning off MATLAB:gui:latexsup:UnableToInterpretLaTeXString
>> warning off MATLAB:gui:latexsup:UnsupportedFont

Having the fonts inspected, the cause becomes crystal-clear: most of the fonts used by Latex have been removed from Matlab’s installation. Apparently, I am not the only one to have noticed this problem.

>> dir(fullfile(matlabroot,'sys','fonts','ttf','cm'))
.                 mwa_cmbsy10.ttf   mwa_cmitt10.ttf   mwa_cmsltt10.ttf  mwb_cmbsy10.ttf   mwb_cmitt10.ttf   mwb_cmsltt10.ttf  
..                mwa_cmbx10.ttf    mwa_cmmi10.ttf    mwa_cmsy10.ttf    mwb_cmbx10.ttf    mwb_cmmi10.ttf    mwb_cmsy10.ttf    
LICENCE           mwa_cmbxsl10.ttf  mwa_cmmib10.ttf   mwa_cmti10.ttf    mwb_cmbxsl10.ttf  mwb_cmmib10.ttf   mwb_cmti10.ttf    
README            mwa_cmbxti10.ttf  mwa_cmr10.ttf     mwa_cmtt10.ttf    mwb_cmbxti10.ttf  mwb_cmr10.ttf     mwb_cmtt10.ttf    
mwa_cmb10.ttf     mwa_cmex10.ttf    mwa_cmsl10.ttf    mwb_cmb10.ttf     mwb_cmex10.ttf    mwb_cmsl10.ttf    
 
>> dir(fullfile(matlabroot,'sys','fonts','type1','cm'))
.                 mwa_cmbx10.pfb    mwa_cmmi10.pfb    mwa_cmsy10.pfb    mwb_cmbx10.pfb    mwb_cmmi10.pfb    mwb_cmsy10.pfb    
..                mwa_cmbxsl10.pfb  mwa_cmmib10.pfb   mwa_cmti10.pfb    mwb_cmbxsl10.pfb  mwb_cmmib10.pfb   mwb_cmti10.pfb    
README            mwa_cmbxti10.pfb  mwa_cmr10.pfb     mwa_cmtt10.pfb    mwb_cmbxti10.pfb  mwb_cmr10.pfb     mwb_cmtt10.pfb    
mwa_cmb10.pfb     mwa_cmex10.pfb    mwa_cmsl10.pfb    mwb_cmb10.pfb     mwb_cmex10.pfb    mwb_cmsl10.pfb    
mwa_cmbsy10.pfb   mwa_cmitt10.pfb   mwa_cmsltt10.pfb  mwb_cmbsy10.pfb   mwb_cmitt10.pfb   mwb_cmsltt10.pfb  
 
>> dir(fullfile(matlabroot,'sys','tex','tfm'))
.               cmbxti10.tfm    cmmib10.tfm     cmssi10.tfm     cmu10.tfm       lcirclew10.tfm  
..              cmcsc10.tfm     cmr10.tfm       cmssq8.tfm      icmcsc10.tfm    line10.tfm      
cmb10.tfm       cmdunh10.tfm    cmsl10.tfm      cmssqi8.tfm     icmex10.tfm     linew10.tfm     
cmbsy10.tfm     cmex10.tfm      cmsltt10.tfm    cmsy10.tfm      lasy10.tfm      manfnt.tfm      
cmbx10.tfm      cmitt10.tfm     cmss10.tfm      cmti10.tfm      lasyb10.tfm     trip.tfm        
cmbxsl10.tfm    cmmi10.tfm      cmssbx10.tfm    cmtt10.tfm      lcircle10.tfm

It seems that Mathworks have changed their approach to Latex; less fonts with different names imply a different future direction — perhaps to diminish Latex’s capacity in Matlab.

Summary

Being neither Tex nor Matlab expert, this is the farthest I could achieve. I’ve tried unsuccessfully to install the missing fonts in the new Matlab (as suggested by bug reports 249537 and 398506); I even tried to modify some tex-files — all in vain. Perhaps someone could find a workaround or a full solution to this issue. If so, please post a comment.

Note that I do not wish to incorporate Matlab figures in a Latex document (this can be done using the LaPrint utility or its variants), but rather the reverse – including Latex formatting within Matlab figures.

Cross reference: additional aspects/followup on CSSM thread #330973.

Related posts:

  1. JMI – Java-to-Matlab Interface JMI enables calling Matlab functions from within Java. This article explains JMI's core functionality....
  2. Matclipse – Eclipse-Matlab interface Matclipse is an open-source plugin for the popular Eclipse IDE that connects it with Matlab. ...
  3. Matlab-Java interface using a static control The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....
  4. HG’s undocumented parameters interface Some HG functions also accept inputs parameters in a struct fields rather than the normal P-V pairs format. ...
  5. Customizing Matlab labels Matlab's text uicontrol is not very customizable, and does not support HTML or Tex formatting. This article shows how to display HTML labels in Matlab and some undocumented customizations...
  6. handle2struct, struct2handle & Matlab 8.0 This article explains how we can use a couple of undocumented functions in Matlab GUI, and what we can learn from this about Matlab's future....

Categories: Guest bloggers, Handle graphics

Tags: , ,

Bookmark and SharePrint Print

13 Responses to Matlab-Latex interface

  1. Daniel Shub says:

    First, I would like to say that I really like the idea of latexfonts.m, but there is a lot going on in this post, of which I am not sure much is relevant …

    First, LaTeX does not select the font until you add \selectfont. The details are more TeXnical then I understand. If you modify line 35 of latexfonts.m to be

    regular_text = @(fa,sh,se) sprintf('%s%s%s%s %s', family{fa},shape{sh},series{se},'\selectfont',txt);

    so that \selectfont is called, you get a very different result (at least on R2011a Linux) with no warnings and all the fonts showing up.

    The reduction in fonts seems to be limited to removing true type fonts (TTF files) which LaTeX doesn’t use and alternate sizes of the LaTeX fonts. While the the subtle details of the Computer Modern font shape depend on the size, the differences are subtle so losing the alternate sizes is likely not a big deal (especially given all the other problems related to the appearance of MATLAB graphics). Further, TMW has disabled, potentially intentionally, the standard LaTeX size changing commands (e.g., \large gives an error). Changing the font size with set(h, 'fontsize', 20) does change the font size, but it is not clear what the command is doing since it is closed source. It is unlikely to be using LaTeX to do the resizing and rather it is more likely just scaling the text box (and its contents) appropriately.

    Much of the LaTeX processor, much of the source is open, but not free. It starts with tex.m where a tex file is constructed and is processed by a closed source mex file. My guess is this is a somewhat dated tex engine (but since tex is so stable it doesn’t matter). One can modify tex.m to call a local TeX engine (e.g., TeXLive) if the built in one is not sufficient. You can also modify tex.m to include all sorts of packages (although getting the path correct is difficult). Going back to the disabling of font size commands, you can look at tex.m and see MATLAB is using a document class called mwarticle.cls. The only differences between mwarticle.cls and the standard LaTeX article.cls is the disabling of size commands.

  2. Ryan W. says:

    This is a problem that I have long been interested in. As stated above, Matlab’s available LaTeX fonts only extend to the Computer Modern font family. As I have rather strict requirements with respect to the fonts that I can use in my scientific figures, this has caused some problems. I looked in to LaPrint, but I want figures that are stand alone and don’t need to be compiled in a LaTeX document. Furthermore, when creating something such as videos in Matlab, you need the actual text in the figure.

    I eventually took the plunge and started sifting through Matlab’s somewhat hacked together distribution of LaTeX. In R2009b, on Mac OS X, I have actually been able to successfully install additional fonts (Helvetica and Symbol), although it is a rather crude implementation. Unfortunately, attempts at a similar implementation in Windows were unsuccessful (although I have come across some posts online that suggest it is possible: http://www.mathworks.com/matlabcentral/newsreader/view_thread/253780 and http://www.mathworks.com/matlabcentral/newsreader/view_thread/283068). This was all fine, as I primarily working in OS X; however, Matlab has been changing the LaTeX engine and my current method no longer works in releases after R2010. I’ve fiddled around with things in an attempt to get the installation procedure to work on the later releases of Matlab; however, they keep moving father and farther from standard LaTeX practices (such as moving from standard naming conventions, having different names between the ttf, pfb, and tfm files, and removing fonts all together as stated above), which makes it difficult to figure out how things work.

    For other peoples reference, here is a basic overview of what LaTeX does when type setting and how Matlab (at least in R2009) works. LaTeX typesetting is a rather complicated procedure. For a good overview of all that you need to install a new LaTeX font, see http://www.tug.org/mactex/fonts/fonttutorial-current.html. Unfortunately, as stated previously, Matlab has been moving away from the structure outlined in the aforementioned website. To start with, there is a font definition file (.fd) file, which outlines which font files are used based on font specifications (such as bold, italic, bold-italic, etc.) and the corresponding encoding. Based on the specified encoding, a enc.def file is read in which specifies the mapping of all of the font symbols. Based on this mapping, the engine takes the text and uses the corresponding tfm files, which contain the font metric information (or sizing), and setups up the location for where each character will be placed (see http://chenfuture.wordpress.com/2009/02/07/tex-fonts-ii-fd-def-and-tfm/ for more information). This is essentially what happens when you call LaTeX from the commandline: latex .tex. In the end, you get a DVI file that contains the location of text based on the tfm information. It is not until you convert the DVI into a post script (using dvips), or a pdf, that the actual font is embedded into the file. Therefore, more often than not, current LaTeX users actually then take this DVI and convert it to a post script file, followed by a conversion to a pdf. However, in the case of Matlab, the DVI information is used natively.

    So, that is how LaTeX works in general when typesetting text. Matlab’s use of the LaTeX engine, to place text in it’s figures, is not at all documented. Everything that I discuss here is based on what I have pieced together myself and is specific to R2009 (although most versions are similarly structured). Lets start with the basic file structure found in Matlab. The base of the installation is found in /Applications/MATLAB_R/sys on a Mac (to find it on Windows, go into the programs folder for Matlab, followed by the “sys” folder). Within this folder, the important folders are
    |-fonts
    |—ttf
    |—–cm
    |—type1
    |—–cm
    |-tex
    |—format
    |—–bigendian
    |—–smallendian
    |—latex
    |—–base
    |—tfm
    The “fonts” folder contains the ttf and type1 font files for Computer Modern (and a couple additional ones such as Euler). The “tex” folder contains the LaTeX specific files. Of interest within this folder are the “tfm” and the “base” folders. The “tfm” folder contains all of the tfm files that correspond to the Computer Modern fonts found in the “fonts” folder. The “tex” folder contains the LaTeX class file “mwarticle.cls” which Matlab uses to typeset the LaTeX text. This class file is essentially LaTeX’s default “article.cls” class, with some modifications made specifically to play nice with Matlab. Finally, the “base” folder contains all of the font definition files (.fd) and the encoding files (.enc).

    There are two ways to go about installing a new font. You can either modify the Matlab file tex.m (which is discussed in a previous post) or you can physically put files into the folder structure. As both methods consist of medling with the Matlab distribution, I strongly suggest backing up files before you start messing around with things. To manually install a new font into the file structure, you essentially need to add the corresponding ttf, type1 (pfb and pfm files), tfm, and fd files into this file structure, along with some modifications to the “mwarticle.cls” file. If you want to install a preexisting LaTeX font, you simply need to track down the ttf, type1, tfm and fd files and place them in their respective folders. Then, to use this font by default, you go into the “mwarticle.cls” file, find the text,

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% MODIFIED FOR MATLAB HERE
    \renewcommand\normalsize{\fontsize{10pt}{12pt}\selectfont}
    \setlength{\textwidth}{6.5in}
    \setlength{\textheight}{8in}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    and add the line:

    \renewcommand{\rmdefault}{<fontname>}

    where <fontname> corresponds to the filename of your .fd file; you could similarly do this for \sfdefault and \ttdefault. You could theoretically specify the font with this command each time you write text in a figure in Matlab using \selectfont, but it is more convenient to do this in the class file if you indent on always using this font (or in the tex.m file). With access to the class file, you can actually customize the fonts quite a bit (for instance, mixing and matching fonts within mathmode, or installing additional LaTeX packages, etc). If you want to install a non-standard font file (i.e., one that doesn’t come with LaTeX), you need to get even more creative and use software, such as FontForge (which is free), and create all of the necessary font files, along with hand writing the .fd file. I have done this for the case of Helvetica and Symbol fonts; however, this is a non-trivial task that I have massaged to work (and only Mac OS X).

    There is a very useful tools to be aware of when trying to install your own fonts into Matlab (although Matlab has recently changed it in R2011 or R2012, making it less helpful). To test to make sure everything is installed correctly, use the following command in the Matlab command line:

    tex('helloworld $x = 5$','verbose',2)

    which should print out some of the LaTeX compiler information, specifying which font definitions are called and if any errors occurred.

    A final note, it is often not too difficult to get Matlab to show your newly installed font in its figure windows; however, when you go to export these figures to vector formats (such as pdf or eps), it can have issues embedding the correct fonts (especially if you have cooked up your own non-standard font). This is something that I have yet to figure out exactly how things work.

    If anyone has further insight on how LaTeX works in Matlab, especially in the case of newer releases and in the case of Windows, please let me know.

    • Dan says:

      If you are having trouble understanding how to deal with the MATLAB LaTeX engine, but understand how to use your local LaTeX engine (e.g., TeXLive or MikTeX), you can cut the MATLAB LaTeX engine out of the loop. For example, you can compile

      \documentclass{article}
      \begin{document}
      \setbox0=\hbox{$\frac{1}{2}$}\copy0\special{bounds: \the\wd0 \the\ht0 \the\dp0}
      \end{document}

      with your local LaTeX engine to create a file called matlab.dvi. You can then overload tex.m to be

      function [dviout,errout,auxout] = tex(varargin)
      	fid = fopen('matlab.dvi');
      	dviout = fread(fid, 'uint8');
      	dviout = uint8(dviout);
      	fclose(fid);
      	errout = [];
      	auxout = [];
      end

      and then call text(0.5, 0.5, 'XXX', 'Interpreter', 'LaTeX') — whatever is in matlab.dvi will show up in your figure. You of course could/should make the overloaded tex.m much more functional and do the compiling based on the input string, but the key is you can completely bypass the MATLAB LaTeX engine. There are still limits as to what can go into a LaTeX hbox and limits on the dvi output format, but standard LaTeX fonts shouldn’t be a problem and my guess is you might be able to get XelaTeX to create an appropriate dvi file with system fonts.

      Right now tex.m is a small undocumented glimpse between a lot of proprietary processing. As long as MATLAB continues to provide this hole by which we can inject user generated dvi content, it doesn’t matter what they do with the LaTeX interpreter.

    • Ryan W. says:

      That is a great idea. I’m kind of embarrassed that I didn’t think of this myself (I guess I got caught up with trying to tweak what Matlab already had there). I gave your sample code a try and it worked great (although it might still be a good idea to use Matlab’s mwarticle.cls file, or a variation of it, to get the text located in the proper location in the figure). I briefly gave XeLaTeX a try with hopes that I could use system fonts. As XeLaTeX produces an extended dvi file (xdv), I modified your overload function for tex.m to be

      function[dviout,errout,auxout] = tex(varargin)
          fid = fopen('matlab.dvi');
          dviout = fread(fid, 'uint8');	
          dviout = uint8(dviout);	
          fclose(fid);	
          errout = [];	
          auxout = [];
      end

      If I use the same .tex file that you specify above, than everything works the same as with a dvi produced with LaTeX. Unfortunately, when I compile the following code with XeLaTeX:

      \documentclass[11pt]{article}
      \usepackage{fontspec}
      \setmainfont{Helvetica}

      \begin{document}
      \setbox0=\hbox{$x = \alpha$}\copy0\special{bounds: \the\wd0 \the\ht0 \the\dp0}
      \end{document}

      in an attempted to use the standard Hevletica font, and feed the xdv file into Matlab, Matlab doesn’t like it and crashes. Any thoughts?

    • Jan says:

      I despise the route that matlab has gone into regarding latex since version 2010. Thank you Dan, for the nice work-around. I’m interested to what people have cooked up regadring more elaborate tex.m contraptions.

  3. Malcolm Lidierth says:

    Possible alternatives to MATLAB tex interpreting within MATLAB:

    1. Use the MATLAB javacomponent function with JLatexMath which is used in SciLab.

    2. More complex but more flexible if using Java 7 (or 6 with JavaFX added): use javacomponent to add a JFXPanel containing WebView to a MATLAB figure/uipanel and then use MathJax to display the content.

  4. Brian says:

    This may be a slight tangent, but I was wondering how to set the background color of a single word in a text box in a figure.
    (aka: highlight with big bold yellow :)

    I’ve been using tex to set the font color of individual words successfully,
    but background color doesn’t seem to be supported by Matlab’s tex engine.
    Perhaps overloading matlab’s tex.m is the answer.
    Though I assume I would have to get everyone else who uses my script to install the new LaTeX engine?
    What would be the best LaTeX engine for changing background color?

    My main restriction is that I’m dealing with figures and text objects that already exist, so I can’t simply use a different method (javacomponent) to create the text object.

    Thanks,
    Brian

    • The easiest way to highlight some text is to use soul package:

      \usepackage{soul}
      \hl{Highlighted text.}

      It is recommended to employ also package color in order to use color references.

      But there is one thing: I’m not sure how MATLAB will interpret \usepackage command. Anyway, you can try it in proper (La)TeX editor and if you find it easy to use then switch to MATLAB and see what happen ;)

    • Malcolm Lidierth says:

      Can you not just set the BackgroundColor of the uicontrol?

    • Brian says:

      Thanks Lubos. Matlab’s text engine doesn’t seem to support the soul package. When I get some time, I will try to switch out matlab’s tex engine and try the soul package.

      Malcolm, yes, I can (and do) set the background color of the entire uicontrol. But I failed mention earlier that the text box has lots of words in it (sometimes many lines of text). My goal is to highlight only one of the words in yellow…aka showing the results from a text search.

  5. Carsten says:

    Hello,

    thanks for this post and the interesting additional information in the comments. However, after some testing and poking around, I don’t believe that tex.m is the relevant point to solve this problem. (For reference: I use R1013a on 64-bit Linux.) True that you can hack tex.m to make Matlab use another (La)TeX engine. But that’s only part of the deal, because the latex run doesn’t really render the font, it only produces the dvi file, which just contains positioning information.

    When I run tex(‘$\sf x = 5$’,'verbose’,2), it reports no errors but a number of successes, including finding cmss10.tfm. That is the file that latex uses in order to typeset cmss10; it contains the font metrics, but not the font glyphs needed for rendering.

    When I put a debugging breakpoint at the end of tex.m, and then run xlabel(‘$\sf x = 5$’, ‘Interpreter’, ‘latex’), I don’t get an error message until the breakpoint, either. At this point, the rendering to the figure window hasn’t happened yet. If I let Matlab continue from the breakpoint, the figure window is updated and the familiar message “Warning: Font cmss10 is not supported” appears.

    The problem does not lie in the built-in latex not being able to process the string into dvi, it lies in Matlab not being able to render the dvi. I suspect that in order to render, Matlab would need a file like mwa_cmss10.ttf in sys/fonts/ttf/cm for screen display and a file like mwa_cmss10.pfb in sys/fonts/type1/cm for eps output.

    A current installation of TeXlive does not contain cmss10 anymore. Computer Modern has been replaced by Latin Modern, available as an OpenType file in texmf/fonts/opentype/public/lm/lmsans10-regular.otf; and I can’t find a pfb version on my system.

    Instead, I tried to copy cmss10.ttf from an installation of matplotlib to sys/fonts/ttf/cm/mwa_cmss10.ttf, but with no effect. I guess there is some kind of font registration in which the file has to be listed to be available. For the pfb-files there is sys/fonts/fonts.scale, fonts.dir, and encodings.dir, but I couldn’t find anything for ttf files.

    I also tried to install cmss10.ttf as a system font. It does show up in Matlab’s font selection dialogue as “cmss10″, but if used the rendering is wrong, and it doesn’t keep ‘Interpreter’ = ‘latex’ from complaining.

    Any more ideas, anyone? I’d really like to be able using latex sans serif again.

  6. Pingback: How to make fonts available to the LaTeX interpreter in Matlab R2013a? | Technology & Programming Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

*

<pre lang="matlab">
a = magic(3);
sum(a)
</pre>