Crop an inserted image?
You can crop your image with graphicx
\documentclass{article}
\usepackage{graphicx}
\begin{document}
% trim from left edge
\includegraphics[trim={5cm 0 0 0},clip]{example-image-a}
% 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
.
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\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}
Note that neither trim
nor viewport
reduces the size of file importing the image.