Related
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.
if I create a plot with some tabs in the titles and try saving that to a local pdf file (via print function), I get some hashtags instead of tabs in the pdf. This does not occur in the visible figure.
For example, I plotted the residuals of an approximant to the runge function on a grid:
plot(g, f(g) - runge(g));
title(sprintf('residuals,\t max_x(s-f) = %.3f', max(f(g)-runge(g))));
Then after some axes manipulation (grid, boxes, etc..) I execute
h = gcf;
set(h,'Units','Inches');
pos = get(h,'Position');
set(h,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
print(h,'data/runge_example.pdf','-dpdf','-r200')
close(h)
Is somebody aware of that behaviour or better able to found that as already solved than I am?
EDIT: Same behaviour with saveas and saving to eps. This does not happen with \n
EDIT2: I'm using Matlab Version R2017b (9.3.0.7...)
So, after a long discussion we figured out that we have no idea of how to use tabs in matlab titles in combination with latex mode.
To clarify: use math environments and escape tex directives, otherwise it will throw an error.
so
sprintf('residuals,\t $max_x(\\vert s-f\\vert) = %.3f$', max(f(g)-runge(g))));
will give you a nice string:
residuals, $max_x(\vert s-f\vert) = 0.264$
The problem is, that it really is a tab. And the matlab latex interpreter (don't know which one that uses, the system or an own) crashes on that. I copy-pasted that to a tex document and pdflatex ran fine on it (but not showing that much space unfortunately).
So, I came up with the following fix:
use the latex directive \quad or \qquad:
title(sprintf('residuals,\\quad $max_x(\\vert s-f\\vert) = %.3f$', max(abs(f(g)-runge(g)))));
This will give you more space than a normal space.
EDIT: For this to work you need the interpreter of matlab to be set to "latex" instead of the default "tex". Do this by changing the title to
title(title_string, 'Interpreter', 'latex')
or by setting (globally for that script)
set(groot, 'defaultTextInterpreter', 'latex')
I am using ipython qtconsole (today it is called jupyter console). When printing latex (with sympy or any other way), the resulting output in a nice latex in black.
Is there a way to change the color to white because if the background of the terminal is not white (like what happens if you run qtconsole --style monokai), the output is unreadable.
Example:
>>> from sympy import *
>>> init_printing()
>>> k = symbols('k')
>>> Sum(k**2,(k,1,100))
So now it is unreadable. Is there any way to turn it white
If you change the black color background, you might get some other visual issues, however you can change the latex output color from black to white, using:
printing.init_printing(use_latex=True,forecolor="White")
Hope that works for you !
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 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).