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

Is it possible to trim an image by a certain number of pixels rather than a length?

I have a set of images that I'm including, and in one figure, a trim of 135 on the bottom works nicely, but a second version of the same images that has undergone some image processing got saved with a different DPI (not a different size), so the same trim appears to trim them quite differently.

However, both sets of images are 512x384, so is it possbile to just specify a trim in terms of image pixels, e.g. 100px? Thanks.

share|improve this question
AFAIK pdflatex supports px as length unit. Did you tried using \includegraphics[clip,trim=0 100px 0 0]{image}? – Martin Scharrer Jun 27 '11 at 16:42
@Martin: you need to first set the pixel width; if the image resolution is 300dpi, \pdfpxdimen=\dimexpr 1in/300\relax should do. The default value of 1px is 1bp, i.e., the default resolution is 72dpi. – egreg Jun 27 '11 at 18:04
@egreg: Ah-hah, that explains things. I was using px initially, but it didn't work the same for both images, so I guess the problem was the pdfdimen variable. If I change this just before a figure that has a different resolution, I assume I should reset it to default right after that figure so it doesn't mess up other graphics. Can you tell me how to do this? I.e. what's the default resolution I should set it back to. I read 72dpi elsewhere, but is this always the case? If not, is there a way to retrieve the current default resolution? Thanks. – SSilk Jun 27 '11 at 19:10
If you change the dimension in a group (the figure environment, for example) the setting will be restored when the group is closed. Or say {\pdfpxdimen=\dimexpr 1in/<res>\relax\includegraphics[...]{...}}. – egreg Jun 27 '11 at 19:14
OK, so just to be clear, suppose I have two copies of the same image, one at 96dpi, and one at 300dpi, but both having the same total size (512x384). If I set \pdfpxdimen=\dimexpr 1in/96\relaxin the figure environment for the first image and \pdfpxdimen=\dimexpr 1in/300\relax for the second, then apply a trim of 100px for both, shouldn't this perform a nearly identical trim on them in the final document? Because I tried this and it's clearly a different though similar level of trim. Too big a difference to be rounding error at the pixel level I think. Thanks. – SSilk Jun 27 '11 at 19:24
up vote 8 down vote accepted

(Summarising the comments as an answer.)

pdfTeX will work with px units, but you have to set these up appropriately using \pdfpxdimen. This is the physical width of one pixel, and has default value of 1 bp, meaning that images initially are assumed to be 72 dpi. \pdfpxdimen is a low-level dimen primitive, and so is best set using \dimexpr:

\pdfpxdimen=\dimexpr 1 in/<dpi>\relax

where <dpi> is the resolution of the image.

With that set correctly, you can then use \includegraphics as normal, adding px to the values used by the trim (or other) key to get the right result.

As an example, consider the two images

600 dpi


72 dpi

which have the same pixel size but different resolution. Using the LaTeX file

\setlength\fboxsep{0 pt}
\pdfpxdimen=\dimexpr 1 in/600\relax
\includegraphics[clip,trim=0 100px 200px 100px]{Figure-a} % 600 dpi
\pdfpxdimen=\dimexpr 1 in/72\relax
\includegraphics[clip,trim=0 100px 200px 100px,scale = 0.12]{Figure-b} % 72 pdi

results in the output file


which shows the result of the trimming - both are the same. (I've scaled the second image so that the two are printed the same size by pdfTeX.)

share|improve this answer
If you need this more often I would define a dpi option for \includegraphics: \documentclass{standalone} \usepackage{graphicx} \makeatletter \define@key{Gin}{dpi}{% \pdfpxdimen=\dimexpr 1in/(#1)\relax } \makeatother \begin{document} \includegraphics[dpi=600,clip,trim=0 100px 200px 100px]{Figure-a} % 600 dpi \includegraphics[dpi=72,clip,trim=0 100px 200px 100px,scale = 0.12]{Figure-b} % 72 dpi \end{document} – Martin Scharrer Jul 17 '11 at 8:58

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.