# Crop an inserted image?

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

-
Have a look at the package adjustbox. –  Marco Daniel May 26 '12 at 12:35
@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
@MartinScharrer: Your package is my standard package for this ;-) –  Marco Daniel May 26 '12 at 12:53
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
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

You can crop your image with graphicx

\documentclass{article}

\usepackage{graphicx}

\begin{document}
% left
\includegraphics[trim={5cm 0 0 0},clip]{example-image-a}
% right
\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 than 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}

\begin{document}
\end{document}


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

-
Awww... just seconds faster than me... :) –  Count Zero May 26 '12 at 12:50
@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. –  cyanide-based food May 26 '12 at 12:56
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
@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

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

-

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\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

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

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


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

-
Sad news: clipping with PSTricks or TikZ also does not reduce the size of file importing the image. –  cyanide-based food May 27 '12 at 19:38

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.

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

\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){%
\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}

-