Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

10 Answers 10

up vote 25 down vote accepted

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

share|improve this answer

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.

share|improve this answer
    
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
2  
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.

share|improve this answer

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.

share|improve this answer
    
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

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.

share|improve this answer

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.

share|improve this answer
1  
This is in fact the way I do it nowadays. –  Ingo May 3 '12 at 8:49

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.

share|improve this answer
2  
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

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: layered sphere

matlab2tikz generated a tex file with 3k lines of codes and took minutes to compile for once.

matlab2tikz's output gives me:

wrong sphere


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.

share|improve this answer
    
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

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.

share|improve this answer
    
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

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')
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.