LaTeX fill image proportionally
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:
1.
\begin
{figure}
[ht]
2.
\includegraphics
[width=60mm,height=40mm,keepaspectratio]
{image}
3.
\caption
{Kakamigahara Crematorium, Japan}
4.
\end
{figure}
To fill the frame, we first have to import two packages at the start of your document.
1.
\usepackage
[export]
{adjustbox}
2.
\usepackage
[nomessages]
{fp}
followed by this command (developed from this post on StackExchange);
01.
\newcommand
{\includegraphicx}
[5]
{
02.
\FPeval
{\mywidtha}{ #3*2.845274*#1}
03.
\FPeval
{\mywidthb}{ (1-#3)*2.845274*#1}
04.
\FPeval
{\myheighta}{ #4*2.845274*#2}
05.
\FPeval
{\myheightb}{(1-#4)*2.845274*#2}
%
06.
\adjustbox
{min size={#1mm}{#2mm}
,
07.
Clip*=
{#3\width - \mywidtha} {#4\height - \myheighta}
08.
{#3\width + \mywidthb} {#4\height + \myheightb}}
%
09.
{\includegraphics[max size={#1mm}{#2mm}]{#5}}}
10.
11.
\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.
1.
\begin
{figure}
[ht]
2.
\includegraphicx
{60}{40}{0.5}{0.5}{image}
3.
\caption
{Kakamigahara Crematorium, Japan}
4.
\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: