Latex fonts in matlab

风格不统一 提交于 2019-12-18 10:44:40

问题


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);
plot(x,y)
set(gca,'FontName','Helvetica');

Is it possible to do the same but for latex fonts (I say latex fonts as I am not sure of the actual name of the font latex uses as its basic font).


回答1:


For any text object you just need to set the 'Interpreter' property to 'latex'. So, for example you could do

xlabel('$$\int_0^x\!\int_y dF(u,v)$$','Interpreter','latex');

For tick labels it is more difficult, though there may be files available to make it easier (example).




回答2:


I'd recommend setting the default interpreter to LaTex at the beginning of your script/function:

set(0,'defaulttextinterpreter','latex')

You can also download a version of Computer Modern (The LaTeX Font Family) and install it to your machine. Techniques may vary if you're running windows or mac, for Mac you'll need to download the OTF version and add it into the FontBook (Cmd-Space: FontBook)

Next, restart Matlab

Finally, you can use the LaTeX Font in Matlab:

   set(0,'DefaultTextFontname', 'CMU Serif')
   set(0,'DefaultAxesFontName', 'CMU Serif')

This is a nice work-around for having constant fonts in your tick-labels, although it has some trouble exporting in some formats.




回答3:


You can define the font within the latex strings. For instance, to change between serif font (Roman) and sans serif font (Helvetica, I guess):

text(0.5, 0.8, '\textsf{sans serif}','interpreter','latex')
text(0.5, 0.7, '\textrm{roman}','interpreter','latex')
text(0.5, 0.6, '$$\mathsf{math\,\,mode\,\,sans\,\,serif}$$','interpreter','latex')
text(0.5, 0.5, '$$\mathrm{math\,\,mode\,\,roman}$$','interpreter','latex')



回答4:


For true matching of fonts (including LaTeX-style kerning, ligatures etc.), the text in the Matlab figure needs to be typeset with LaTeX. The laprint script, which uses psfrag, is a straightforward way of doing this.




回答5:


If you export to .eps you can just edit the figure afterwards with a simple text editor and exchange the fonts in there. It is a bit fiddly but does the trick. You can also change the kerning of each character individually (because its position is hard-coded in there).

It is also possible to change each character's font individually (I sometimes do this, if a need a symbol from Latex (i.e. Computer Modern), but want the rest of the label in Helvetica again)




回答6:


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:

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.




回答7:


If you aim at exporting MATLAB figures into LaTeX and want a consistent look-and-feel (including the fonts), you should use matlab2tikz (a project I once started).



来源:https://stackoverflow.com/questions/11212172/latex-fonts-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!