How to crop an eps file?

When you create a figure (in e.g. eps format) with R, the margins around the main context are always too wide. To save the space in the final documents, e.g. LaTeX generated pdf file. I have figured out two ways of reducing the margins.If you just want to shrink the white margin of an eps file, try Method A–C
If you want to crop an esp file, see Method D 

  • Method A (R users) Before you make your graph in R, use par(mar=c(bottom, left, top, right))to specify the margin you want to keep. The default value is c(5, 4, 4, 2) + 0.1. Try this example to see the differences.
    par(mar=c(5,4,4,2)+0.1) # The defualt margins plot(rnorm(100)) dev.copy2eps()          # Save as eps 
    par(mar=c(4,4,0,0)+0.1) # Figure with very tight margins plot(rnorm(100)) dev.copy2eps()
  • Method B (use epstool) Very handy tool that can handle the optimal bounding box
    epstool --copy --bbox file.eps file_new.eps
  • Method C (use ps2epsi)It automatically calculates the bounding box required for all encapsulated PostScript files, so most of the time it does a pretty good job
    ps2epsi <input.eps> <output.eps>
  • Method D (DIY for any eps )Use a text editor open your eps file and you will find a line like this
    %%BoundingBox: 0 0 503 503

    in the front lines of the file. Adjust these vales to proper integers. Save it and test if the margins are better. When you want to crop an eps file and include it into LaTeX with \includegraphics command, you should use  \includegraphics* instead. Because If * is present, then the graphic is ‘clipped’ to the size specified. If * is omitted, then any part of the graphic that is outside the specified ‘bounding box’ will over-print the surrounding text. By the way, the options trim, bb, viewport options in \includegraphics can do the same job in a different manner without editing the eps file, see the help document for details.

Categorized as Default, R

By Feng Li

Dr. Feng Li is an Associate Professor of Statistics in the School of Statistics and Mathematics at Central University of Finance and Economics in Beijing, China. Feng obtained his Ph.D. degree in Statistics from Stockholm University, Sweden in 2013. His research interests include Bayesian computation, econometrics and forecasting, and distributed learning. His recent research output appeared in statistics and forecasting journals such as the International Journal of Forecasting and Statistical Analysis and Data Mining, AI journals such as Expert Systems with Applications, and medical journals such as BMJ Open.


Leave a comment

Your email address will not be published. Required fields are marked *