Which way of exporting Matlab graphics delivers the best result for inclusion in LaTeX? Is it possible to export scalable vector graphics, or even LaTeX graphics?
10 Answers
In case the kind of graphics you wish to export is supported by matlab2tikz
, there is only one way to go: matlab2tikz
.
You get true vector graphics in TikZ, i.e., no fiddling with PostScript, being restricted to latex
, no going back and redoing graphics when your font or color scheme changes, small changes in for instance the legend are done in-place, ...
Some updates on using plot2svg
in MATLAB R2014b as of Mar 2015:
Jürg hasn't updated his awesome script for a while. For those of you who got an error while using plot2svg
in R2014b, the simplest solution is just changing every str2num
function in the original plot2svg
to str2double
, and problem solved.
(Original post)
matlab2tikz
is the best vector graphics solution for small dataset graph as Pieter said in his answer. However, in some situations, using matlab2tikz
to generate codes is not an efficient solution IMHO.
Here I will show two examples where using matlab2tikz
to export the graphics is less than ideal a solution.
- First is with a large dataset like in the following triangulated sphere:
matlab2tikz
generated a tex file with 3k lines of codes and took minutes to compile for once.
- Second is when there are lighting, alpha, and blur in Jürg Schwizer's blog post:
matlab2tikz
's output gives me:
For these situations: either you have a large dataset like a complicated triangulation/flow fields, or you have options such aslighting
, camproj
,FaceAlpha
, etc in your MATLAB
codes, or other Axes/Patch/Quivergroup properties tweak. The best way to include these graphics is:
(Step 1)Use
plot2svg
inMATLAB
to get an svg file.(Step 2)Use Inkscape to convert it to eps or pdf
(Step 3)Use
\includegraphics
ingraphicx
package of any TeX-distributions to include eps or pdf
plot2svg
is a small pkg in MATLAB
to produce scalable vector graphics by Jürg Schwizer.
There is a large thread of How to include SVG diagrams in LaTeX? on TeX.SE, the step 2 is what is in the accepted answer, if you are using Linux, once you installed Inkscape, you could use these shell scripts to convert svg to pdf/eps/png file formats.(Beware: the sh in the link is using zsh shell)
Other advantages of using this approach to export MATLAB
figures:
For scientific publications, most publishers are still using ancient TeX-distributions which doesn't include
tikz
pkg, for example: SIAM requires authors to use PostScript figures in the submission.Inkscape has a UI for editting svg files also.
-
2I can't describe how much this answer saved my ass, I'm exactly in the above situation. 10+ :)– htdOct 10, 2013 at 9:59
-
@Shuhao_Cao I see that MATLAB2015b can save figures directly to SVG. Does this render the plot2svg step redundant? Mar 24, 2016 at 9:14
-
@HennadiiMadan Thanks for the heads up. I had several tests. MATLAB's own built-in output seems superior in the test case where I use LaTeX as renderer for texts. For the triangulated surface example, MATLAB generates a much bigger file though. Still for the bounding box, the output is not ideal for MATLAB's built-in version, you have to edit it in Inkscape for a better pdf output. I may update my answer later. Mar 24, 2016 at 14:35
-
1@ShuhaoCao Thanks for your reply, I was asking to avoid testing myself (: I'm with matlab2tikz now but plan to use your approach for the next paper (just because changing something in inkscape does not involve 1.5 hour debugging sessions, like it does for me to fiddle with tikz output). Mar 25, 2016 at 9:47
There is a great library called pgfplots, which creates great looking plots directly in latex. This package is used by matlab2tikz. You can write the data you want to plot in an ascii table, and create legends, axis labels and such directly in latex, very much like the code you would have written in Matlab anyway.
You can easily change the contents, colors (for example for B/W prints), width and heigh, resolution, and much more without having to recreate figures.
-
1
There are two Matlab packages for exporting graphs to EPS plus psfrag, which replaces the labels inside the figures with strings that are typeset by LaTeX. They are laprint and matlabfrag; I recommend the latter as it will work for more graphic types, and laprint is no longer supported.
The support for surface-plot output may well make this a better option than the tikz based converters, otherwise the quality will be largely the same.
To include these types of graphics into pdfLaTeX, use the pstool package.
The print
command in Octave allows several devices for LaTeX output: -dtex
, -depslatex
, -depslatexstandalone
, -dpstex
, and -dpslatex
. Making plots using Octave, gnuplot, and LaTeX by Marco De la Cruz-Heredia has some good examples.
The print
command in Matlab can write to eps (see Elenaher's comment), so that is another option.
-
1The print command in Matlab can write to many output formats, including not only eps but also vector formats pdf, svg, emf and bitmap formats png, jpg– matthNov 10, 2011 at 9:24
In MATLAB all labels and titles, you can use LaTeX interpreter to make your plot more professional.
% for
text(2,2,'$e^{2\log(x)}$','interpreter','latex','fontsize',18)
% for legend
s = legend('$x^3$','$x^2$');
set(s,'interpreter','latex');
Then the best way to include MATLAB plots into your latex document you have to follow these steps
- save a picture as eps files;
- use epstopdf to convert eps to pdf, then you can use both formats.
You should not save picture in pdf format since the bounding box will be not correct.
-
4Matlab will create cropped PDF graphics if you set the papersize correctly: tex.stackexchange.com/questions/5559/… Jan 19, 2011 at 22:09
-
@Robertson I think eps and then epstopdf is better in any cases since you may need eps for latex and pdf for pdflatex. Jan 20, 2011 at 6:16
If you want to have scalable results, the better way is maybe to use the vectorial format eps (encapsulated postscript) but you will have to compile with "classic" latex and not pdflatex.
I am pretty sure that matlab is able to generate eps.
-
I use the matlab command
print(gcf,'-deps2c',myfilename)
to print a figure to encapsulated postscript in color, which I then include with \includegraphics. Couldn't be easier. but it does require using latex and not pdflatex, as mentioned. If you do this, the font sizes you specify in matlab for text on the graph will be maintained in the eps file. Oct 11, 2010 at 19:24 -
2
you may also have a look at the export in pstricks : fig2tex together with the provided links.
I really like the print command. To avoid margin and text resizing problems: I like to take a lot of control when exporting my figures by pre-defining the margins and paper-size to be my desired figure size. This makes fonts size correctly, and lets you choose the margins you want.
First I define my figure width and height (these are in metric, but you can use US units also).
FigW=13.49414;
FigH=21.08462*.3;
Next, I set my figure properties. First I define the text interpreter to be latex, and then I set the paper size. You can define margins in the paper position parameter, but i prefer to do that later. if you want to use inches, that's fine here. Finally the 'Position' option just makes the figure on screen look identical to what will be printed to PDF. The first two parameters in 'Position' are the screen position and the second are the width and height of the figure.
Figure1=figure(1);clf;
set(Figure1,'defaulttextinterpreter','latex',...
'PaperUnits','centimeters','PaperSize',[FigW FigH],...
'PaperPosition',[0,0,FigW,FigH],'Units','centimeters',...
'Position',[1,10,FigW,FigH]);
I then use the subaxis package from the mathworks file exchange to take control of my margins, padding and spacing of all my subplots. For one plot this might look like:
subaxis(1,1,1,'Margin',.1);
Finally, I export to PDF using the print command in matlab:
print('-dpdf','-r500','filename.pdf')
The easiest way is to use export_fig that convert Matlab figures to PDF automatically with some nice and useful features.
For both advanced and enthusiastic Matlab users, the possible way is to run through 11 pages of recommendations in "How To Make Pretty Figures With Matlab" manual.
-
Notice that the author also warns for Note that export_fig does not transparency in patch objects in vector formats. Only a transparent background is supported.. In general MATLAB is not a good collaborator. Better practice is to take the data to gnuplot or other scientfic plotting platforms.– percusseApr 29, 2013 at 10:07