Matlab figures in Latex done properly

I’ve played around a bit with Tikz, and I consider it to be hands down the most latex friendly tool for inputting images in Latex documents.

I have also been having issues with putting figures from Matlab into my Latex documents. Up until now, I was saving them as eps, then using \includefigure to add them to latex. This meant that I had to size all the figures exactly the same in Matlab so that they would look good in Latex when putting them in a subfigure environment; it also meant that the labels and axis grid numbering were in the wrong font and size when compared to the rest of my latex document.

After looking around for a better solution I found matlab2tikz by Nico Schlömer. I have to say this piece of code is excellent. It will take most of your matlab figures and save them as a Tikz compatible file that you can simply \input into your Latex. I have mostly used it for 2D graphs though, so I’m not sure how it fares with 3D plots.

A few things you need to be aware of when using this are :

  • Use LuaLatex for dynamic memory allocation. This helps get around the 30MB limit when compiling latex files, but you should really try to reduce the size of your Tikz files as well.
  • Generating plots from Tikz files is time consuming when compiling Latex. Externalize Tikz so that your Tikz files are not converted every time you compile http://www.howtotex.com/tips-tricks/faster-latex-part-ii-external-tikz-library/. This also gives you the maximum amount of compilation memory for each image compiled, rather than sharing compilation memory between all images. External compilation of Tikz is done with PDFLatex by default, so if you want to use Lualatex you need to specify it with \tikzset{external/system call={lualatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"}} in your preamble.
  • Be careful how big the outputted Tikz file is. If you have a very large number of points in your plot (such as for time simulations with large number of timesteps) your output Tikz files will be several megabytes. This will pose problems for compiling your Latex, perhaps running out of memory if not using LuaLatex. I recommend reducing the 'floatFormat' option in the matlab2tikz call, and using the wonderful script LinePlotReducer by Tucker McClure before calling matlab2tikz. What this does is that it tries to remove unnecessary points from your plot, so that the plot looks the same at the plotted size, but contains only the necessary data. This worked wonders for me, but your mileage may vary.
  • Specify height and width when calling matlab2tikz, like this matlab2tikz('myfile.tikz', 'height', '\figureheight', 'width', '\figurewidth');. This allows you to then specify the size of the grid box for your figure in Latex with
    \newlength\figureheight
    \newlength\figurewidth
    \setlength\figureheight{4cm}
    \setlength\figurewidth{6cm}
    \input{myfile.tikz}
    . Note that only the grid box size is defined. The labels are then added around the grid and the spacing is calculated automatically. If you do not leave enough space for your figures and labels in the subfigure environment, the labels will overlap other figures around them.
  • If you are using the subfigure environment, and your axis grids don’t line up perfectly, you have to specify the size of the tick labels with (you might only need x or y)
    \pgfplotsset{yticklabel style={text width=3em,align=right}}
    \pgfplotsset{xticklabel style={text width=3em,align=right}}
    where you set the width to whatever is required so the labels are not cut off. This is explained in here

Some more information about tips for using Matlab2Tikz is also available here