MATLAB: figure fonts - matlab

A command of the form xlabel('$<stuff>$','interpreter','latex'); will produce an axis label that is typeset by TeX using a font that is presumably ComputerModern. However, the axis tick labels (e.g., 0, 1, 2, ...) appear in the default font (Helvetica?). I would like to synchronize all the fonts in the figure (preferably to ComputerModern).
Toward that end, I presume that a command of the form set(0,'DefaultAxesFontName', '<fontname>') may be useful. However, I need to know the exact name or path of the font used by the MATLAB TeX interpreter. How can I retrieve a string value for the font name or a path pointing to the font file for the default figure font and the default TeX-interpreted font?

You could also plot the axis ticks with latex, look here:
http://alex.bikfalvi.com/research/latex_in_matlab_ticks/
Actually, your command was right to mget the default font (when NOT using latex):
get(0,'defaultaxesfontname')
But I didn't find any to get the default latex font. Perhaps it would be possible by using the internal java routines (to get some ideas, look at http://undocumentedmatlab.com/)

You can download the computer modern font, and install it on your local machine. For Mac, you'll need to download the OTF format (i'm not sure on windows machines).
Then, researt matlab, and you can use
set(0,'DefaultAexsFontName', 'CMU Serif')
to make things look like latex font.

Related

What fonts are actually available for MATLAB graphics text? [duplicate]

I have a Matlab program where I need to include a plane icon (Zapf Dingbats 40) in the plot. Everytime I run it, it falls back to a system font.
Is there anything that I am doing wrong? This is not the exact code, but illustrates the problem:
title(char(40),'fontname','ZapfDingbats','fontsize',50);
The resulting plot always displays ( instead of the Dingbats plane icon ✈.
I verified that the font is installed and I can type with it on Word.
I am using Matlab R2013b on Mac OSX 10.9.1.
EDIT: It prints correctly to a pdf, but does not display correctly.
You may use the 'Wingdings' font for that. The following code
text(0.5, 0.5, char(81), 'fontname', 'Wingdings', 'fontsize',50);
gives
The 'ZapfDingbats' font may not be in the /Library/Font folder (but 'Wingdings' is), even if listfonts tells you that the font is there. Actually, the listfonts
function adds some extra fonts to the list of the available fonts, and I don't get the rational of that.
% always add postscipt fonts to the system fonts list.
systemfonts = [fonts;
{
'AvantGarde';
'Bookman';
'Courier';
'Helvetica';
'Helvetica-Narrow';
'NewCenturySchoolBook';
'Palatino';
'Symbol';
'Times';
'ZapfChancery';
'ZapfDingbats';
}];

Adjusting graphical LaTeX text size in Matlab

I am trying this example:
http://www.mathworks.com/help/symbolic/latex.html
Let's say I wanted to resize those labels. How? I can't figure out any LaTeX code, and 'fontsize' does not do the trick.
This hits a sore spot when it comes to Matlab's support for (or use of) Latex. The normal font-size commands from Latex aren't available. (In Latex normally you'd just say \Large{Text ... $x$} or even \normalsize ....)
To do this in a Matlab plot you can add fontsize spec at the end
title(['For $x$ and ...'], 'Interpreter', 'latex', 'fontsize', 14)
For more discussion and how to change font type as well see this post. Note that there are not so many fonts readily available in Matlab. To preserve sanity I'd keep this kind of tweaking to the minimum.
There is another method that will work anywhere in Matlab where you can use Latex -- in any text, in the middle of a string, etc: You can drop to Latex's lower level font specification.
title(['\fontsize{15}{0}\selectfont For $x$ and ...'], 'Interpreter', 'latex')
The first command \fontsize{}{} specifies the font, the second one \selectfont actually changes it for the rest of the text. When you want to switch to a different font, even mid-string, you again issue \fontsize{12}{0}\selectfont and you have that font size after that point. The only thing you need to change is the size (I used 15 and 12 as examples), the rest is boilerplate (for this purpose).
See what these things mean and more discussion in this post. For far more detail on fonts in Matlab see this article. For how to change fonts across the whole document see this post.
There are yet other ways but it gets progressively trickier and this should be enough. Probably the best advice is to set it once for the whole document. That also makes sense typesetting-wise.
Note. The font command of the second example must be given outside of math mode. Latex has two major modes, text and math. To make it go to 'math mode', where it processes everything as it were math symbols, you put a $, or $$. (There are yet many other ways, but in Matlab's strings this is all you'll ever use.) When you want it to go back to typesetting text normally, you end math mode with another $, or $$. All math is in between $...$, everything else is normal text.
This is some text, now typeset some math: $y = x^2$ ... back to text.
The font commands do not work in math mode but need be given outside the $...$. They will apply to any following math as well. In a plot command we'd say
ylabel(['\fontsize{16}{0}\selectfont $\dot{x}$'], 'interpreter','latex');
Here is the line that worked:
ylabel('$x_e, x_c$', 'interpreter','latex', 'fontsize', 32);
Obviously, between the dollar signs can be whatever appropriate LaTeX expression desired.

MatLab Eps Print Webdings interpreter

So recently I decided to not use the standard markers that Matlab provides and use my own via: a set of fonts including Webdings or WingDings. I make a standard scatter plot, and plot the text (in wingdings), using the text command, over the locations of the markers.
When I save these plots as png files, they print perfectly. They also appear perfectly on my screen after plotting.
However when I save these plots as eps files, the webdings are turning into their original letters 'l' or 'w'. It also looks like it's plotting them in a Courier font, but my default is Helvetica.
I've read through this previous post, but my question differs in that he is looking to use Latex as the interpreter and to include fonts, whereas I don't want to use Latex as the interpreter. However, it seems like my default interpreter (not sure what that is), isn't doing the job when converting to EPS.
Example:
imageR='w'
text(xf2,yf2,imageR,'fontName',font,'FontSize',fontR,'HorizontalAl','left','color','w')
I figured it out....took me way too long.
Ghostscript / Postscript only export with a few fonts when you are exporting eps files.
This link was incredibly helpful.
I just switched from wingdings/webdings to ZapfDinbats...Practically the same thing...

Is it possible to get around MATLAB's limitation on font embedding in vector format files?

According to the MATLAB manual, when you save a figure using print or by choosing file|save, if you choose the painters renderer and save to PDF or EPS vector formats, all fonts get substituted. Is there a way to get around this limitation?
Whenever I output a figure, whether I use print or export_fig, the fonts get substituted, and so they no longer match the fonts in the document that I plan on putting the figure into. I would prefer to keep them in a vector format, because I use LaTeX and so I want to be able to use the same figures in my documents as in my beamer presentations and have them scale nicely without bloating the file size.
If I'm reading that link correctly, not all fonts get substituted. From 'Choosing a Printer Driver':
The table below lists the fonts supported by the MATLAB PostScript and Ghostscript drivers when generated with the Painters renderer (fully vectorized output). This same set of fonts is supported on both Windows and UNIX:
AvantGarde
Helvetica-Narrow
Times-Roman
Bookman
NewCenturySchlbk
ZapfChancery
Courier
Palatino
ZapfDingbats
Helvetica
Symbol
So, if you use one of the above fonts, the output vector-format figure should maintain the correct font. See for example:
list_fonts = listfonts
figure('renderer','painters'),
plot(peaks),
xlabel('this font is Helvetica','fontname','Helvetica','fontsize',24)
set(gcf,'paperpositionmode','auto')
print(gcf,'-depsc2','test1.eps')
Which produces:
So, choose one of the fonts from the list above, and the font will be output correctly. Otherwise, change the font in your presentation to match one of the above fonts.
I also encountered this problem for many times, and I have an simple but effective way that never fails me (on Windows, need GSview).
1) save fig as PDF
2) save PDF as ps
3) open ps with GSview, then click "File->PS to EPS", specify a file name and done.
Hope this helps.

Latex fonts in matlab

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).
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).
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.
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')
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.
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)
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.
From Matlab version 2014, the below command can be used.
set(gca,'TickLabelInterpreter','latex')
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).