LaTeX fill image proportionally

share on google plus share on facebook share on twitter share on linkedin share via email

At present it is quite easy in LaTeX to fit an image proportionally to a frame of fixed width and height. However, to fill a frame with that same image is not straightforward…

This is a snippet that fits the image proportionally to the frame:

\begin{figure}[ht]
  \includegraphics[width=60mm,height=40mm,keepaspectratio]{image}
  \caption{Kakamigahara Crematorium, Japan}
\end{figure}

To fill the frame, we first have to import two packages at the start of your document.

\usepackage[export]{adjustbox}
\usepackage[nomessages]{fp}

followed by this command (developed from this post on StackExchange);

\newcommand{\includegraphicx}[5]{
\FPeval{\mywidtha}{     #3*2.845274*#1}
\FPeval{\mywidthb}{ (1-#3)*2.845274*#1}
\FPeval{\myheighta}{    #4*2.845274*#2}
\FPeval{\myheightb}{(1-#4)*2.845274*#2}%
\adjustbox{min size={#1mm}{#2mm},
    Clip*={#3\width - \mywidtha}   {#4\height - \myheighta}
          {#3\width + \mywidthb}  {#4\height + \myheightb}}%           
          {\includegraphics[max size={#1mm}{#2mm}]{#5}}}

\begin{document}

The factor 2.84574 converts from mm to pt.
Then, we can use the command to fill a frame. In this case, 60mm width, 40mm height, positioned at 50% of the horizontal and vertical dimensions of the original image.

\begin{figure}[ht]
  \includegraphicx{60}{40}{0.5}{0.5}{image}
  \caption{Kakamigahara Crematorium, Japan}
\end{figure}

This definition works, but could be further improved to allow any unit for the width and height.
The resulting two types of figures, fitted and filled, will look like this:

latex-kakamigahara

Leave a response

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>