Matlab: loss of Latex font on saving eps file - matlab

In Matlab, I am using LaTex interpreter as default and CMU Serif as in Latex as my default font through the following commands inside startup.m.
set(0,'DefaultTextFontname', 'CMU Serif');
set(0,'DefaultAxesFontName', 'CMU Serif');
set(0,'defaulttextinterpreter','latex');
The plot produced from Matlab appears like this, with the fonts in place:
However, upon saving the above image as eps using either print or saveas command produces figure with all the ticks and label in a different font:
Can someone suggest, what is the issue resulting in different fonts?

Related

Fix extra space in MATLAB's title plot

I'm writting the results for my thesis, this includes the generation of figures for my LaTeX document by using MATLAB code. I do this by making a figure of the data, and then I use the print command to save in an EPS file.
The problem is that the plot in the MATLAB window is correct as you can see here:
But when I compile my document in LaTeX (Lyx), the result is this:
.
As you can see, I have an unexpected big extra space in "iLm" title. The same occurs when I use LaTeX code in the label of different signals.
Searching in the web I tried the following command:
set(groot,'DefaultTextInterpreter','latex');
But just prints "iL_m" like I wrote in the code. How can I make the spacing consistent in the EPS file?
Here's the code I'm using:
clear h n
figure(1)
h(1) = plot(iLmVal.time,iLmVal.data(:,2),'LineWidth',1,'color','k','DisplayName','Modelo');
hold on
h(2) = plot(iLmVal.time,iLmVal.data(:,4),'LineWidth',1,'color','r','DisplayName','Circuito');
legend(h,'Location','southeast'),...
axis([0 0.06 -18 27]),title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times-Roman'),...
set(gca,'Color','white');
set(gca,'XTick',0:0.005:0.06),...
set(gca,'XTickLabel',0:5:60,'FontSize',20,'FontName', 'Times-Roman','XMinorGrid','on'),...
xlabel('Tiempo [ms]','FontSize',20,'FontName', 'Times-Roman'),...
set(gca,'YTick',-18:4:28),...
set(gca,'YTickLabel',-18:4:28,'FontSize',20,'FontName', 'Times-Roman','YMinorGrid','on'),...
ylabel('Corriente [A]','FontSize',20,'FontName', 'Times-Roman'),...
n = gca;
n.YAxis.MinorTick = 'on'; n.YAxis.MinorTickValues = -18:1:28;
n.XAxis.MinorTick = 'on'; n.XAxis.MinorTickValues = 0:0.0025:0.07;
grid on; hold off
I'm using MATLAB R2018a and Lyx 2.3.2-2. Also, by printing in PNG this problem doesn't occur, but the quality and resolution is very poor.
I don't think this is related to LyX, you should see this issue in the exported EPS file. You can fix this using a different font.
As you can see in the appearance of the figure in MATLAB, where the title is shown using a sans-serif font (definitely not 'Times-Roman'), MATLAB does not recognize the 'Times-Roman' font, and uses an alternative for rendering. This alternative font is used to determine the location of the subscript, which is positioned independently of the main text by MATLAB. However, this font name is written to the EPS file. When rendering the EPS file in a different program, the 'Times-Roman' font is recognized and used to render the text. Because this font has different metrics to the one used by MATLAB, the location of subscripts is not correct.
When printing to PNG, MATLAB creates a bitmap, therefore this problem does not occur.
On my computer (macOS), the fooling produce a correct representation on screen:
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times')
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times-Roman')
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','TimesRoman')
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times New Roman')
The following doesn't:
title("Corriente de magnetización iL_m",'FontSize',20,'FontName','Times Roman')
On different computers, different font names will be available. Use a name that is recognized on your computer. Your best bet is 'Times', which is the PostScript name for this font and should be recognized everywhere.
Alternatively, use the export_fig utility on the File Exchange. This is a great tool for exporting MATLAB figures to EPS. It will not only fix your fonts, but make many other little tweaks that will improve the look of your figures.

LaTeX interpreter but sans-serif font on Matlab figures

On LaTeX I often use the \usepackage{cmbright} font when I want to have a clean, sans-serif font both in the text and in the mathematical formula I write.
In order to have an homogeneous result between my figures and the text, I would like the text in my figures (labels, axis ticks, title, annotations...) to be as well in this font.
Unfortunately so far on Matlab, when using the LaTeX interpreter, only the default font is available. I would like to be able to write both text and mathematical formulas in a sans-serif font (and the same for both text and math) on the figures I plot.
Also, I would need to export the result as .eps figures (preserving the sans-serif font and all).
Any idea?
I am on Mac OS 10.14.1 (Matlab 2018a) and also on Ubuntu 18.10 (Matlab 2018b).
Thank you for your help!

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...

EPS figures created with MATLAB, edited with Illustrator, Different Colors in after dvipdf prepared PDF

I create figures in MATLAB, and export to EPS using the normal File->Save As dialog box. I added some additions to the EPS file using Adobe Illustrator CS6, and then saved the EPS again. Viewing the EPS file with Evince, everything looks normal.
But when adding these figures to a LaTEX file, compiling as normal, then converting the .dvi to a PDF using dvipdf, the colors of the Illustrator modified EPS come out different. Any ideas what can be done to fix them?
I solve the problem by the following procedure. When you save the EPS file in illustrator, DO NOT check the "Include CMPK PostScript in RGB Files". Then the colors in the compiled LaTeX file won't be in dull color.

MATLAB: figure fonts

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.