Imagine having a beautiful picture, but the subject is very small or not centered. What if you want to place several graphs side-by-side with subfig, but all or some of the graphs have a white border taking up a lot of space.
So how to trim pictures and eliminate unwanted margins of graphs?
The graphics bundle provides a way to crop an image using the trim, clip
options of the includegraphics
command.
\usepackage{graphicx} ... \begin{figure}[htbp] \begin{center} \includegraphics[trim=left bottom right top, clip]{file} \caption{default} \label{default} \end{center} \end{figure}
Unless you know exactly how much to crop, you will probably have to try a few times, until you get the numbers right. Make sure you don’t forget clip
, since trim
alone won’t work. The default units are big points (bp). However, any unit available in LaTeX can be used. See here for more information on units.
Finally, using negative values adds white space to a figure, doing exactly the opposite of cropping.
The graphics bundle user guide has more details and a comprehensive list of options available.
Here is an example:
\documentclass[11pt]{article} \usepackage{graphicx} \begin{document} \begin{figure}[h!t] \begin{center} \includegraphics[width=\textwidth]{crab} \end{center} \label{fig:orig} \end{figure} \begin{figure}[h!b] \begin{center} \includegraphics[trim=1200 400 1500 1500,clip,width=\textwidth]{crab} \end{center} \label{fig:crop} \end{figure} \end{document}
copiancestral
Nice tip!
Rachel Ziebell
Thanks for your post
meepo
Thank you. My life is truly better because of this.
tom
Thanks for the comment. Happy to help. Tom.
Hamlet
If your image is PostScript, the Ghostview (`gv`) program present in most X (UNIX) systems will report the same type of coordinate used by “trim” (“big point”?) as you move the mouse around the image.
That is an alternative to trial-and-error method.
tom
Thanks for the tip! Cheers, Tom.
M.A. Khan
You can convert your .pdf file to .ps or .eps using Adobe Acrobat Pro. Then you get the coordinates of .eps or .ps file using Ghostview (as Hamlet wrote). Same coordinates can be used to insert .pdf file BUT then use bb instead of trim,e.g.,
\includegraphics[bb= 112 240 484 540, clip, width=7.2cm]{file.pdf}
Allan
MakeFile Problem
I am having problems with compiling eps Images, I don’t know whether it is a problem with my make file or. When even I am compiling a document with eps images, it takes like 4minutes struggling to compile them, how can I overcome that? Is it the makefile problem? If it is can some one help me with a working example. Or could u help me with a template with a makefile and a sample image included, I just dont know whether eps is becoming obsolete but it is taking ages to be compiled.
tom
Hi Allan,
Thanks for your comment. Without seeing any code, I suggest you try to convert your eps-image to pdf. Every Linux/UNIX distribution and Mac OS X will have that command (
eps2pdf
or similar). The image quality will be the same. If loading the pdf instead doesn’t resolve your problem, please provide a minimal working examples that I can run on my computer.Thanks, Tom.
Nga Nga
Thank you so much. You save my life.
Jim Gomi
When tuning the clip parameters, it is helpful to enclose the image in a framebox:
then you can immediately see where the borders of the clipped image are.
tom
Cool, thanks!
Ranga
Thanks a lot Tom…
Joan Solà
Good tip. One problem though is that the ‘clip’ option interferes with the synctex algorithms to synchronize source and preview windows when the document is split into different files using \input or \include. Synctex is used by TexShop and TexWorks, and probably other tex guis. This is certainly a bug in synctex but… do you have any experience on this?
See for example:
http://tex.stackexchange.com/questions/263105/synctex-fails-for-input-files-related-to-fancyhdr-graphicx
tom
I’m not sure you find this useful. But one idea might be to ‘clip’ the figure in a separate tex file of class
standalone
and then load the pdf. To automate processing of both documents, use latexmk.Eugê
Thank you. This is very helpful. I am glad that this is the number one Google result, as it should be! 🙂
Ganesan Subburaj
Thank you so much. Very useful tip.
Martin
For some reason, when I use trim and clip as in the example, my picture is flip upside down… Any idea where it might come from?