327

I'm inserting an image with a simple:

\includegraphics[height=5cm]{filename.png}

This results in an image that maintains the aspect ratio, and so gives me something like 6cm width. I only really want the left side of the image though, and would like to crop it to 50% width. Is it possible to do this?

If possible, I'd like to do this in a portable way (i.e. something that other authors of this document won't have to install packages for, and something that works with a reasonably old version of pdflatex - our computers at work are in sore need of an upgrade).

Martin Scharrer
  • 262,582
naught101
  • 8,224
  • 1
    Have a look at the package adjustbox. – Marco Daniel May 26 '12 at 12:35
  • 5
    @MarcoDaniel: graphicx can crop images by itself, and the OP doesn't want any non-core package anyway. – Martin Scharrer May 26 '12 at 12:50
  • 2
    @MartinScharrer: Your package is my standard package for this ;-) – Marco Daniel May 26 '12 at 12:53
  • 2
    naught101: I generalized the question a little by removing the file type at some places. Cropping in LaTeX is not file type specific (at least not at the user level) and questions should not be unnecessary specific. I hope you don't mind. – Martin Scharrer May 26 '12 at 12:56
  • @MartinScharrer: my install (ubuntu 12.04, texlive 2009) doesn't seem to have a graphicsx, just a graphics package. Is graphicsx part of 2010? – naught101 May 26 '12 at 13:00
  • @MartinScharrer: of course not, thanks. I was just including 'png' to imply that I would want to use \includegraphics{}, but it's probably clear enough :) – naught101 May 26 '12 at 13:01
  • 1
    It's graphicx not graphicsx and its a very old package and part of the required set of packages for LaTeX. – Martin Scharrer May 26 '12 at 13:08
  • argh.. I just noticed that. stupid. should have read this first: Packages: graphics vs graphicx :) – naught101 May 26 '12 at 13:17
  • General remark: If the PDF will be sent for printing to paper, at a print-on-demand service, then it is best to pre-crop the images (graphics editor) before building the PDF, so that TeX does not perform cropping or any other image transform. This is not a technical issue; it is how some P.O.D. services want it, whether you like it or not. – rallg Feb 27 '23 at 19:27

5 Answers5

506

You can crop your image with graphicx.

\documentclass{article}

\usepackage{graphicx}

\begin{document} % Answer: [trim={left bottom right top},clip] % Ex. 1: trim from left edge \includegraphics[trim={5cm 0 0 0},clip]{example-image-a} % Ex. 2: trim from right edge \includegraphics[trim={0 0 5cm 0},clip]{example-image-a} \end{document}

Use the trim option, which takes four space separated values.

 trim={<left> <lower> <right> <upper>}

If you don’t give a unit, the package assumes bp i.e. big points to be the unit. After setting these values you must activate the cropping with clip=true or just clip.

If you combine trim with height or something similar the image will be cropped and then resized. That means that the crop values must fit the original size. If found no solution to say crop to 50 % width.

Update

As Martin said in the comments you can use adjustbox to clip the image exactly by 50 %. Note that you must replace \includegraphics by \adjincludegraphics, to access the \width.

\documentclass{article}

\usepackage[export]{adjustbox}

\begin{document} \adjincludegraphics[height=5cm,trim={0 0 {.5\width} 0},clip]{example-image-a} \end{document}

adjustbox also provides \height, \depth and \totalheight.

Tobi
  • 56,353
  • @tobi: looks good. While you're expanding, note that I still want to resize the image first with height=5cm. How does that work with trim? – naught101 May 26 '12 at 12:54
  • Note that XeTeX cannot crop correctly. – kiss my armpit May 26 '12 at 12:56
  • 4
    With \usepackage[export]{adjustbox} you can use: \includegraphics[trim=0 0 {.5\width} 0,clip]{image} to clip 50% from the right. (The braces around all four values as Tobi has done it are actually not required, but can also be used if wanted) – Martin Scharrer May 26 '12 at 12:59
  • @Forgiver: Joseph and I wrote a xetex.def which allows clipping (aka. cropping): https://bitbucket.org/martin_scharrer/adjustbox/raw/c567b2d38a41/xetex/xetex.def. Also adjustbox allows for clipping with XeTeX. – Martin Scharrer May 26 '12 at 13:01
  • 3
    @naught101: You can't. trim/clip are applied before the resizing. adjustbox also provides Trim and Clip which can do it afterwards. – Martin Scharrer May 26 '12 at 13:02
  • @MartinScharrer: Thanks for this hint. Do you want to post it as another answer? If you dont’t want I’ll include it in mine – Tobi May 26 '12 at 13:02
  • @Tobi feel free to include it in yours. There might be a solution to get it resized and trimmed like the OP wants using saveboxes. I might post an answer for this instead. – Martin Scharrer May 26 '12 at 13:04
  • This answer works for me, because the 50% only needs to be approximate. I'm still a bit confused about how the 5cm trim works, since the png is sized in pixels, not cm. Is it based on the Exif data of the png, or the default resolution of pdflatex, or what? – naught101 May 26 '12 at 13:26
  • @MartinScharrer: Thanks I tried it but the MWE (see my update) doesn’t work, TeX says that \width is undefined. – Tobi May 27 '12 at 12:34
  • 2
    @Tobi: Sorry, my fault. The \width etc. macros are not supported directly with \includegraphics. You can use ``\documentclass{article}

    \usepackage{adjustbox}

    \begin{document} \adjincludegraphics[height=5cm,trim={0 0 {.5\width} 0},clip]{example-image-a} \end{document} ``

    – Martin Scharrer May 27 '12 at 12:39
  • This is brilliant! Together with the subcaption package, this provides a slick method for piecing together subfigure (without having to regenerate them). – Steven C. Howell Oct 19 '15 at 18:28
  • Is this an "expensive" operation? That is, does this increase the compile time significantly versus cropping the image beforehand and just including? – rrrrr Sep 27 '19 at 20:39
  • I don’t know but I’d say it won’t affect the compile time. You could set up a test file and measure it yourself … – Tobi Sep 28 '19 at 14:07
32

To clip 50% of the right of your image without using extra packages you can use a savebox to measure the natural size of the image first. This only required the graphicx package which is part of LaTeX itself and always installed. Note that all scaling/resizing is applied after the trimming. If you want the original image to be scaled to 5cm width and then 50% clipped, just resize the clipped half to a width of 2.5cm afterwards:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begingroup \sbox0{\includegraphics{example-image}}% \includegraphics[clip,trim=0 0 {.5\wd0} 0,width=2.5cm]{example-image} \endgroup

\end{document}


It is also possible to use an internal macro of graphics/x to calculate the scale factor which would be used to scale the original image to a 5cm width and then use this factor on the clipping image as well:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\includegraphics[width=5cm]{example-image} %<---- for comparison

\begingroup \sbox0{\includegraphics{example-image}}% \makeatletter \Gscale@div\myscale{5cm}{\wd0} \includegraphics[clip,trim=0 0 {.5\wd0} 0,scale=\myscale]{example-image} \endgroup

\end{document}

Paul Wintz
  • 402
  • 2
  • 14
Martin Scharrer
  • 262,582
12

The viewport key of graphicx can also be used to simulate trimming or cropping. viewport has 4 space-separated length arguments: left bottom right top. The remaining code should be self-explanatory.

\documentclass{article}

\def\FirstScale{0.5}% scale for loading
\def\SecondScale{1}% scale for final
\def\FileName{example-image-a}% file name


\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\FirstScale]{\FileName}}

\usepackage{pgf}

\newlength\xL
\newlength\yL
\newlength\xR
\newlength\yR

\pgfmathsetlength{\xL}{0*\wd\IBox/\FirstScale}% please adjust
\pgfmathsetlength{\yL}{0*\ht\IBox/\FirstScale}% please adjust
\pgfmathsetlength{\xR}{0.5*\wd\IBox/\FirstScale}% please adjust
\pgfmathsetlength{\yR}{1.0*\ht\IBox/\FirstScale}% please adjust


\usepackage[tightpage,active,graphics]{preview}
\PreviewBorder=0pt

\begin{document}
\includegraphics[viewport={\xL} {\yL} {\xR} {\yR},clip,scale=\SecondScale]{\FileName}
\end{document}

enter image description here

Note that neither trim nor viewport reduces the size of file importing the image.

7

I wrote a tool to determine coordinates used to crop an image. Hope it helps. Compile it with either xelatex or latex-dvips-ps2pdf. Note that example-image-a.eps temporarily does not work with latex-dvips-ps2pdf, so please use another image.

enter image description here

\documentclass{article}

\def\M{5}% columns
\def\N{5}% rows
\def\scale{1.5}% scale
\def\filename{example-image-a}% filename

\usepackage{graphicx}
\newsavebox\IBox
\savebox\IBox{\includegraphics[scale=\scale]{\filename}}


\usepackage{pstricks-add}
\newpsstyle{gridstyle}
{
    gridcolor=gray,
    subgridcolor=cyan!50,
    gridlabels=5pt,
}

\psset
{
   xunit=\dimexpr\wd\IBox/\M\relax,
   yunit=\dimexpr\ht\IBox/\N\relax,
   style=gridstyle,
}

\newlength\xL
\newlength\xR
\newlength\yL
\newlength\yR

\usepackage{pgf}

\def\Zoom(#1,#2,#3)(#4,#5)(#6,#7){%
    \psframe[linecolor=#2,linewidth=2pt,dimen=inner](!#4 #6 sub #5 #7 sub)(!#4 #6 add #5 #7 add)
    \expandafter\gdef\csname#1\endcsname{%
    \begingroup
        \pgfmathsetlength{\xL}{\psxunit/\scale*(#4-#6)}
        \pgfmathsetlength{\yL}{\psyunit/\scale*(#5-#7)}
        \pgfmathsetlength{\xR}{\psxunit/\scale*(#4+#6)}
        \pgfmathsetlength{\yR}{\psyunit/\scale*(#5+#7)}
        \includegraphics[scale=#3,viewport={\xL} {\yL} {\xR} {\yR},clip]{\filename}
    \endgroup}}


\usepackage[tightpage,active]{preview}
\PreviewBorder=12pt
\PreviewEnvironment{pspicture}


\begin{document}

\begin{pspicture}[showgrid=top](\M,\N)
    \rput[bl](0,0){\usebox\IBox}
    \Zoom(One,red,5)(2.5,2.5)(0.5,0.5)
\end{pspicture}

\begin{preview}
    \One
\end{preview}

\end{document}
1

If cropping the source is a possibility (which in my case was when I was searching for the option to do it with \includegraphics), the following online cropping tool for cropping PDF does a great job: https://pdfresizer.com/crop

pengu1n
  • 119
  • 1
  • 2
    Thanks, but if I was going to crop the source, I'd probably just use GIMP. – naught101 Jan 05 '18 at 02:25
  • @pengu1n Agree sometimes we complicate things. Just cropping the picture is often the easiest and fastest way—though irreversible ;). I would recommend not to crop on a web application, just for the sake of privacy; any image processing program will do, since cropping is probably the most basic functionality. My favorite application is Gthumb (for Linux users) :) – loved.by.Jesus Mar 02 '21 at 09:56