[Tex/LaTex] Cropping/Trimming an image with includegraphics by specifying percentages

adjustboxgraphics

Say I have an image my_image.png that I am loading in my LaTeX document using:

\begin{figure}
\includegraphics[width=0.5cm]{my_image.png}}
\end{figure}

Is there a way of telling LaTeX to only render a specific rectangular portion (in percentages) of the original image?

For example, say I want to just render a cropped image:

  • Trim 5% of the original image width from the left
  • Trim 10% of the original image width from the right
  • Trim 15% of the original image height from the top
  • Trim 20% of the original image height from the bottom

Best Answer

\documentclass{article}
\usepackage{graphicx}
\usepackage{adjustbox}

\begin{document}

\adjustbox{trim={.05\width} {.2\height} {0.1\width} {.15\height},clip}%
  {\includegraphics[width=0.5cm]{cupdot.png}}

\end{document}

Since \includegraphics produces no depth, you can use the fact that \adjustbox knows the \width and the \height of the image.