18

I am trying to create a document that requires the inclusion of images at a precise size (h x w inches). I would like to reuse the document template, so I would like to create a command that would resize and clip any image (sized H x W inches) to the right size (h x w in), provided that the full width (W) is first scaled down to the new width (w), and then the new H is trimmed to the height h. How do I do this in LaTeX?

+---------------------+---+
|       w             |   |
|                     |   |
|                     |   |
|h                    |   |
|                     |   |
|                     |  H|
|                     |   |
+---------------------+   |
|                         |
|                         |
|             W           |
+-------------------------+

[EDIT]

Reading some posts here, I have tried to solve this using the following:

\documentclass[11pt,article]{memoir}
\usepackage{graphicx}
\usepackage{adjustbox}

\newlength{\oH}
\newlength{\nH}

\newcommand{\PrintImage}[3]{%
% #1 : desired width
% #2 : desired height
% #3 : image
\settoheight{\oH}{\includegraphics{#3}}
\settoheight{\nH}{\includegraphics[width=#1]{#3}}

\adjincludegraphics[width=#1,trim={0 {\oH - #2} 0 0},clip=true]{#3}%
}

\begin{document}
\centering

\PrintImage{2in}{2in}{myimage}

\end{document}

But it doesn't work. Thinking that the problem was that I was using the original height oH, I changed the \adjincludegraphics line to:

\adjincludegraphics[width=#1,trim={0 {\nH - #2} 0 0},clip=true]{#3}%

But it doesn't work either. Why?

  • I assume you actually want something that is even more general: scale down (only if needed) so that either height or width needs to be trimmed to fit within the h:w proportions. – Werner Jun 23 '12 at 2:22
  • Yes, that is the idea. So any image could be used without the hassle of scaling it and trimming it by hand. – Amaru Jun 23 '12 at 2:38
  • \includegraphic[width=W in]{figure1} would be sufficient then isn't it? – Rick de Groot Jun 23 '12 at 9:44
  • @RickdeGroot: That wouldn't give the right height. – Martin Scharrer Jun 24 '12 at 9:55
  • @Amaru: You need to give more information. Should the images be trimmed on top or on both sides? Do you really mean trimming or actually clipping, i.e. should the trimmed part still be visible or not? – Martin Scharrer Jun 24 '12 at 9:57
6

Here a \PrintImage macro that clips the bottom of the included image if its height is too large.

\documentclass{article}
\usepackage{graphicx}
\usepackage{calc}
\usepackage{ifthen}
\newlength{\oH}
\newlength{\oW}
\newlength{\rH}
\newlength{\cH}
\newcommand\PrintImage[3]{% width, height, image
  \settototalheight{\oH}{\includegraphics{#3}}%
  \settowidth{\oW}{\includegraphics{#3}}%
  \setlength{\rH}{\oH * \ratio{#1}{\oW}}
  \ifthenelse{\lengthtest{\rH < #2}}{
    \includegraphics[width=#1]{#3}%
  }{%
    \setlength{\cH}{(\rH-#2)*\ratio{\oW}{#1}}%
    \includegraphics[width=#1,clip,trim=0 \cH{} 0 0]{#3}%
  }%
}

\begin{document}
\PrintImage{6cm}{2cm}{yourimage}
\end{document}
  • Thank you very much. I very new to LaTeX, and I was struggling with this issue. I have changed includegraphics for adjincludegraphics, because the former didn't seem to work. This requires loading adjustbox too. With some small changes, based on your excellent solution, I have created a generic ClipImage command that will fit an image to a given size, clipping the side that overflows. – Amaru Jun 25 '12 at 1:20
4

Here is the generic ClipImage command, based on PolGab answer:

\documentclass[11pt,article]{memoir}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{calc}
\usepackage{ifthen}

\newlength{\oH}
\newlength{\oW}
\newlength{\rH}
\newlength{\rW}
\newlength{\cH}
\newlength{\cW}
\newcommand\ClipImage[3]{% width, height, image
\settototalheight{\oH}{\includegraphics{#3}}%
\settowidth{\oW}{\includegraphics{#3}}%
\setlength{\rH}{\oH * \ratio{#1}{\oW}}%
\setlength{\rW}{\oW * \ratio{#2}{\oH}}%
\ifthenelse{\lengthtest{\rH < #2}}{%
    \setlength{\cW}{(\rW-#1)*\ratio{\oH}{#2}}%
    \adjincludegraphics[height=#2,clip,trim=0 0 \cW{} 0]{#3}%
}{%
    \setlength{\cH}{(\rH-#2)*\ratio{\oW}{#1}}%
    \adjincludegraphics[width=#1,clip,trim=0 \cH{} 0 0]{#3}%
}%
}

\begin{document}
\centering

\ClipImage{0.5in}{3in}{myimage}

\end{document}

Thank you very much again.

  • As Martin Sharrer said in its comment, your ClipImage command is not completely generic. Why do you clip right or bottom side and not left or top side ? Why do you not center the image before clipping two opposite sides ? – Paul Gaborit Jun 25 '12 at 5:28
  • I stand corrected. It is not completely generic, but for my project this is what I need. The images I will be using include the most relevant information lined up to the upper left corner. I can see, thought, that a generic solution could be tailored from this command. Again, thank you very much for your help. – Amaru Jun 25 '12 at 6:34
1

You can use the Clip key of \adjincludegraphics together with the \height macro which is set to the current height of the adjusted content. Note that Clip works differently as trim with clip because it is executed in a different order (as the later two come from graphics).

\documentclass[11pt,article]{memoir}
\usepackage{graphicx}
\usepackage{adjustbox}



\newcommand{\PrintImage}[3]{%
% #1 : desired width
% #2 : desired height
% #3 : image
    \adjustimage{width={#1},Clip=0 {\height-#2} 0 0}{#3}%
}

\begin{document}
\centering
\hspace{2in}\rule{2in}{2in}

\rule{2in}{2in}\PrintImage{2in}{2in}{example-image-a4}

\end{document}

Output

The black bars are for size reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.