# What is the best way to include Matlab graphics?

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?

-

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

-
Thanks for your answer, Pieter. Are you from Holland :)? I noted that you wrote "en" instead of "and", a typical Dutch mistake. –  Ingo Oct 11 '10 at 14:16

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.

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 in MATLAB to get an svg file.

(Step 2)Use Inkscape to convert it to eps or pdf

(Step 3)Use \includegraphics in graphicx 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.

-
Very valuable addition to this question. Thanks –  Ingo Nov 14 '12 at 7:21
+1 for the fucking awesome plots alone :) –  Christian Jul 1 '13 at 3:32
I can't describe how much this answer saved my ass, I'm exactly in the above situation. 10+ :) –  Henrik Oct 10 '13 at 9:59

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.

-

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.

-
This is in fact the way I do it nowadays. –  Ingo May 3 '12 at 8:49

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.

-
The 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 –  matth Nov 10 '11 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

1. save a picture as eps files;
2. 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.

-
Matlab will create cropped PDF graphics if you set the papersize correctly: tex.stackexchange.com/questions/5559/… –  Will Robertson Jan 19 '11 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. –  S. Boonto Jan 20 '11 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. –  Brandon Kuczenski Oct 11 '10 at 19:24
Matlab can also create PDF. –  Will Robertson Oct 12 '10 at 1:46

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. –  percusse Apr 29 '13 at 10:07