# Best practices for using MATLAB images in LaTeX?

I am writing a paper currently using PDFTEX, and I make heavy use of MATLAB figures.

The problem is that when I make a MATLAB figure, I can adjust it to any size, before saving it. When I save it, I can save it as a .png, .tiff, .jpg, etc. Lastly, on LaTeX itself, I can adjust the size of the image there as well, (height, width, etc etc).

All those choices give me various quality images on my paper, so I am not sure what to do... do I have MATLAB images always large and then shrink them in LaTeX? Or make them small in MATLAB and expand them LaTeX? Is one image format better for display purposes in a PDF? What are best practices here?

Thank you.

• I would recommend saving as PDF. PDF is a vector format, which means that, except for the spacing of the legend relative to the lines, the figure window size is irrelevant when you save it in terms of the resolution. Then you can set whatever size you will need in your LaTeX file. All that said, I'd really recommend you learn pgfplots. I am using/learning it now, and it makes for much better figures, and all you need are the data files! – darthbith May 20 '14 at 19:59
• @darthbith Cool! Just to be clear, if I go to file->saveas on the MATLAB figure, and select '.pdf', is that the same as the pdf flag used if I did it manually ? Is there a difference between PDF as a document, and "PDF Color PDF file format" as per your first link? Thanks. – TheGrapeBeyond May 20 '14 at 20:10
• I don't know if they do the same thing... There is no difference between the two formats though. – darthbith May 20 '14 at 21:23
• Odd that nobody mentioned matlab2tikz. I find it problematic for all but the simplest figures, though. – Scott Seidman May 11 '17 at 18:25

My usual practice is to export MATLAB figures as .pdf files, because they are readily recognizable by PDFTEX and are rendered as vectorized images so resizing does not affect quality. The following is a nice MATLAB extension that works great for exporting figures as .pdf without the problem of exporting the image as a whole page:

http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig

It also has a nice documentation page: https://sites.google.com/site/oliverwoodford/software/export_fig

• Hi, sorry, but does thsi ghostscript work for Win-7 OS's? ... I cant seem to get this guy's code working on here. – TheGrapeBeyond May 21 '14 at 0:29

A bit late to the party here, but I've been putting a lot of MATLAB figures in a few papers I'm working on and have developed an effective routine...

## Determine text width

Use the method from this answer to determine the \textwidth of your LaTeX document. You can extrapolate from here to print \textheight too.

## Set MATLAB figure width

Set your MATLAB figure to this width:

set(1, 'units', 'centimeters', 'pos', [0 0 15 10])


The brackets contain: [x-position y-position width height]. I usually play around with the height value to find something that works for my plot, but a good rule of thumb is 1/4 to 1/3 of the text height.

Note that x-position and y-position don't really matter -- they define where the figure will appear on your screen, relative to the bottom left corner.

## Set MATLAB margins and export

As @user3228896 mentioned, you can control the pdf margins from within MATLAB, but no extension is necessary. Just stick this after each figure:

figure(figNum)
% plot(somestuff);
pos = get(figNum,'Position');
set(figNum,'PaperPositionMode','Auto','PaperUnits','centimeters','PaperSize',[pos(3), pos(4)])
print(figNum,'myfigure','-dpdf','-r0')


If you've got multiple plots make sure to set a unique value for myfigure so they don't all overwrite each other.

All of this can be done in inches too.

## LaTeX interpreter for plot labels in MATLAB

You can use LaTeX for MATLAB plot labels to keep variable names and such consistent.

It's also possible to change the font family, though personally I find the default MATLAB sans-serif font works well.

## Place the figure

When you insert the figure, you can adjust it a bit using factors of \textwidth:

\begin{figure}[!htb]
\captionsetup{skip=0.5\baselineskip,size=footnotesize}
\centering
\includegraphics[width=1.1\textwidth]{Figures/myfigure}
\caption{This is the caption text}
\label{fig:myfigure}
\end{figure}


Since you're using a pdf, a bit of stretching up or down won't make a difference in quality, so I usually just play with this a bit until it looks right.

The point is that Matlab saves figures with a different resolution and sometimes different tick marks than the ones shown on screen. At some point in your procedure, you will have to set, in Matlab, 'PaperPositionMode' to 'auto'. In this way, Matlab will save the figure at the size displayed on screen.