Latex fonts in matlab

后端 未结 7 954
执念已碎
执念已碎 2020-12-24 08:19

Is it possible to convert the font of a matlab plot to be the same of latex fonts. For example I can modify the font of a plot by:

x = -pi:.1:pi;
y = sin(x);         


        
7条回答
  •  借酒劲吻你
    2020-12-24 08:52

    Disclaimer: I'm not the expert.

    However, linux's command fc-list lists all fonts on your system, I think they are all supported by Matlab.

    In ubuntu (and possibly other distro's) the latex font is called Latin Modern, or lm for short. You can find them all via:

    # fc-list | grep lmroman
    /usr/share/texmf/fonts/opentype/public/lm/lmroman10-bold.otf: Latin Modern Roman,LM Roman 10:style=10 Bold,Bold
    /usr/share/texmf/fonts/opentype/public/lm/lmroman7-italic.otf: Latin Modern Roman,LM Roman 7:style=7 Italic,Italic
    ... etc etc...
    

    Between the colon and the first comma it says Latin Modern Roman, which is the name of the Roman font of Latin Modern, there is also:

    • Latin Modern Sans
    • Latin Modern Roman Caps
    • Latin Modern Mono
    • etc etc

    I think these fonts are used when you call \textrm (roman), \textsf (serif), etc etc, in latex in mathmode. Of course, you can find them all via the fc-list command.

    To get the latex font in your plots, simply execute:

    plot(rand(10), 'o');
    xlabel('index', 'FontName', 'Latin Modern Roman', 'FontSize', 25); 
    ylabel('value', 'FontName', 'Latin Modern Roman', 'FontSize', 25); 
    set(gca, 'FontName', 'Latin Modern Roman', 'FontSize', 25);
    

    And the result is a nice:

    example plot

    PS: Latin Modern is not exactly the same as Computer Modern, but they look alike and I wouldn't know how much they really differ.

    Regarding Matlab's Interpreter option, to the best of my knowledge it does not apply to all textual elements of a plot, like the axe labels:

    >> plot(rand(10), '.'); set(gca, 'Interpreter', 'latex');
    Error using hg.axes/set
    The name 'Interpreter' is not an accessible property for an instance of class 'axes'.
    

    Unfortunately, matlab's print function is flawed, as it is not able to embed fonts into eps or pdf files. For this reason generated files may have substituted fonts, even on the same system. To tackle this, this library allows you to embed the fonts: http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig

    Make sure to set the background of your figure to white, before exporting it and note that the library may take a lot of memory, as it calls ghostscript.

    Moreover, changing the interpreter seems like overkill if you wish to change the font.

提交回复
热议问题