Department of Engineering

IT Services

Matlab graphics with LaTeX

Much of the time you'll be able to use graphics from matlab in LaTeX documents just as you use graphics from elsewhere - use the graphicx package. Sometimes however you might want to have sophisticated LaTeX in your matlab graphics, or you may not want the text in the graphics to scale when you scale the figure. Several solutions have emerged to deal with these situations. The use of pdflatex complicates matters, and some solutions require several non-standard packages to be installed. This page describes the possibilities

psfrag

The psfrag package is part of most LaTeX installations. If you have an EPS (encapsulated postscript) file with some displayed text, the package lets you replace the text with a latex fragment. Suppose for example that you wanted the result of latex's $\sqrt{x}$ to appear in a matlab figure. All you need to do is put some text (test, say) where you want the maths to be on the figure, export the figure as EPS (fig1.eps in this example), then in your document have

   \begin{figure}[htbp]
   \psfrag{test}{$\sqrt{x}$}
   \includegraphics{fig1.eps}
   \end{figure}

Note that this doesn't work with PDF files, but you can still use this method with pdflatex if you have the pst-pdf package installed. With a file (called foo.tex, say) containing

  \documentclass{article}
  \usepackage{psfrag,pst-pdf,graphicx}
  
  \begin{document}
  \begin{figure}[htbp]
  \psfrag{test}{$\sqrt{x}$}
  \includegraphics{fig1.eps}
  \end{figure}
  
  and
  
  \begin{figure}[htbp]
  \includegraphics{fig2.pdf}
  \end{figure}
  
 \end{document}

you can create a PDF file by running

  pdflatex foo.tex
  latex foo.tex
  dvips -o foo-pics.ps foo.dvi
  ps2pdf foo-pics.ps
  pdflatex foo.tex

matlabfrag

matlabfrag (from matlab central) has been developed from the also popular laprint program. According to its own documentation

  • It has much more emphasis on WYSIWYG.
  • It handles figures that need the OpenGL or Z-Buffer renderers.
  • Text objects are sized correctly (such as with a legend).
  • Tick labels are handled better.

when compared to laprint so although it's not so easy to install I'll describe its use here with the latex support it prefers - pstool. First download the matlabfrag.m file and the extensive documentation. Then there are some latex files to download. I think they're all part of the oberdiek bundle (30M), but I've made the ones I needed available for download (March 2010). If you have trouble with these you can use the pst-pdf package as shown in the earlier example.

Inside matlab you produce your figure in the usual way then run matlabfrag, giving it at least a file name - e.g.

  >> plot(1:10) ; title('help')
  >> matlabfrag('fragtest')

Then you create a LaTeX file like this (called foo.tex, say)

   \documentclass{article}
   \usepackage{pstool}
   \begin{document}
   \psfragfig{fragtest}
   \end{document}

and then run

   pdflatex -shell-escape foo.tex

You should end up with a foo.pdf. Once that works you can start exploiting the extra features that matlabfrag.m offers - see its documentation for details.