321

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

9
  • 1
    Have a look at the package adjustbox. May 26, 2012 at 12:35
  • 4
    @MarcoDaniel: graphicx can crop images by itself, and the OP doesn't want any non-core package anyway. May 26, 2012 at 12:50
  • 2
    @MartinScharrer: Your package is my standard package for this ;-) May 26, 2012 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. May 26, 2012 at 12:56
  • 1
    It's graphicx not graphicsx and its a very old package and part of the required set of packages for LaTeX. May 26, 2012 at 13:08

5 Answers 5

490

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.

13
  • @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, 2012 at 12:54
  • Note that XeTeX cannot crop correctly. May 26, 2012 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) May 26, 2012 at 12:59
  • 3
    @naught101: You can't. trim/clip are applied before the resizing. adjustbox also provides Trim and Clip which can do it afterwards. May 26, 2012 at 13:02
  • 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} May 27, 2012 at 12:39
31

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

2
  • Sad news: clipping with PSTricks or TikZ also does not reduce the size of file importing the image. May 27, 2012 at 19:38
  • Works perfectly.
    – ScumCoder
    Nov 16 at 23:19
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

2
  • 2
    Thanks, but if I was going to crop the source, I'd probably just use GIMP.
    – naught101
    Jan 5, 2018 at 2: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) :) Mar 2, 2021 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .