45

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?

12 Answers 12

25

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.

19

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

pdfcrop -margins 10 fig.pdf fig.pdf
8

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]

  • 7
    IS there a way of obtaining the trim numbers apart from trial and error? – Eshwar Nov 24 '11 at 16:08
8

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.

  • This worked well for me. I'm using Windows and I had to install ghostscript in order to be able to produce .pdf files with export_fig. My steps were: 1) Install ghostscript; 2) Download the export_fig .zip file, unzip, add the resulting folder to my matlab path; 3) Create a MATLAB figure and use the MATLAB command export_fig('MyFigure.pdf') to save the figure as a .pdf file. – littleO Feb 14 at 23:18
5

The easiest way of removing whitespace around a Matlab figure is

subplot('position',[0 0 1 1]);
2

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.

2

I recommend using eps2pdf matlab script.

print -depsc testeps;
eps2pdf testeps.eps;
2

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

2

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

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')
2

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

Deletes the margins in the figure previous to the exporting.

  • 1
    This doesn't appear to work. (Tested with Matlab 2014b.) – Torbjørn T. Oct 13 '15 at 16:43
  • It worked for me, when PaperSize and related properties were set correctly. Strangely, I only needed to add this option on Ubuntu. On Mac OS even without this line the code produces the expected result. Seems like there is a difference in rendering mechanism depending on the platform. Hope this helps. – Dr_Zaszuś May 9 '17 at 13:35
1

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.

  • 2
    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
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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