TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am preparing image files to be included in a latex document. The image file is generated by printing a Matlab plot.

If I print the plot in eps format, the content of the eps file is fully occupied with the plot;

if I print the plot in pdf format, then there are big margins above and below the plot in the pdf file;

if I use ps2pdf to convert the eps file into a pdf file, the big margins will be added above the plot.

I would like to use pdflatex, so I prefer pdf format of image files. I was wondering how I can save the plot in pdf format without the big margin above and below the plot?

share|improve this question
Maybe your question is answered here. (Possible duplicate?) – Hendrik Vogt Nov 18 '10 at 6:06
@Hendrik That question is related but not a duplicate IMO. – Will Robertson Nov 18 '10 at 6:25
Here's a blog entry describing a way to reduce the whitespace that Matlab puts around plots: nibot-lab.livejournal.com/73290.html – Tobin Fricke Jul 11 '11 at 20:40

12 Answers 12

up vote 21 down vote accepted

This is partially off-topic, since LaTeX doesn't have anything to do with how the PDF is generated. Nonetheless, it's relevant enough for my tastes.

In Matlab, you can create a cropped PDF by setting the papersize to the same size as the graphic; see this small figuresize.m function to do this automatically.

Usually if you're generating graphics from Matlab, though, I'd recommend using matlabfrag so that your figure labels are typeset by LaTeX. You can use psfrag-based graphics in pdfLaTeX by using the pstool package.

Finally, if you just want to crop a graphic, TeX Live and MiKTeX both come with a command line tool pdfcrop to perform this on an arbitrary PDF.

share|improve this answer
And of course there is matlab2tikz (win.ua.ac.be/~nschloe/content/matlab2tikz), for all your Matlab related graphics in tikz. – Pieter Nov 18 '10 at 6:45
@Pieter Yes, I forgot to mention, because I haven't started using it yet; so far I'm still at the stage of exporting my data from Matlab manually when I want to use pgfplots, which technique works well so far. – Will Robertson Nov 18 '10 at 6:54
Just for posterity: matlab2tikz has moved to github.com/nschloe/matlab2tikz. – Dougal Nov 15 '12 at 21:42

pdfcrop should help remove the margins. I use it like so:

pdfcrop -margins 10 fig.pdf fig.pdf
share|improve this answer

how about trimming the image by using the options of the \includegraphics command?

here is an example \includegraphics[trim = 35mm 95mm 40mm 100mm, clip, width=0.475\textwidth]

share|improve this answer
IS there a way of obtaining the trim numbers apart from trial and error? – Eshwar Nov 24 '11 at 16:08

The easiest way of removing whitespace around a Matlab figure is

subplot('position',[0 0 1 1]);
share|improve this answer

Export_fig makes all the work automatically, including margins and pdf creation. The original Matlab save result is: Original Matlab print command export_fig results (picture size is the same) is: enter image description here

Note: this command is improvement of savefig command.

share|improve this answer

if you are happy with the eps images and want similar pdf images (in terms of margins), you should use epstopdf (and not ps2pdf).This is mentionned in Hendrik's link.

share|improve this answer

I recommend using eps2pdf matlab script.

print -depsc testeps;
eps2pdf testeps.eps;
share|improve this answer

Here is a MATLAB package that automatically generates figures with a tighter crop (and offers other nice things): savefig

share|improve this answer

And, of course, if you're a complete graphic-fewl you can export your matlab data to asymptote for making really beautiful plots that rise to the level of the rest of your beautiful latex-created document. Asymptote can create pdf output directly, which looks really great in pdflatex docs. I haven't noticed that there's any extra spacing either.

share|improve this answer
Personally I prefer matplotlib (a python library), it allows the same quality graphics with matlab syntax (a + for new learners) and can output eps or pdf as well.And since it sits in numpy, it can do all your data analysis as well :D – crasic Nov 18 '10 at 21:30

I voted question up even though I also agree that is a bit off topic. I use MATLAB in particular to create nice animation but when it comes to pictures I just export pictures into PostScript and then use GraphcisMagic to crop, resize or do whatever you like. GraphcisMagic can also convert from ps/eps format into pdf but people already mentioned half dozen tools that can accomplish the same.

share|improve this answer

I really 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).


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.

                'PaperUnits','centimeters','PaperSize',[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:


Finally, I export to PDF using the print command in matlab:

share|improve this answer

set(gca, 'LooseInset', get(gca,'TightInset'))

Deletes the margins in the figure previous to the exporting.

share|improve this answer
This doesn't appear to work. (Tested with Matlab 2014b.) – Torbjørn T. Oct 13 '15 at 16:43

Your Answer


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.