[Tex/LaTex] Find optimal figure size to fit two images on one page

automationfloatsgraphicsscaling

I often encounter a situation where I would like to fit two or three figures on one page.
I define my figures as follows:

\begin{figure}[htpb]     
\centering       
\includegraphics[width=0.xx\linewidth]{images/filename.png}     
\caption{Caption}   
\label{fig:tag}      
\end{figure}

The xx value depends on the size of the image I'm using and it takes some trial and error to find the optimal value. With square graphs xx=50 approximately.

I am looking for a way to avoid the trial and error part. I assume I need to use something else than \linewidth, but I don't know what.

Updates:

I've tried 0.5\pageheight instead of \linewidth but this is not satisfactory. I can fit two images on one page even if I use 0.6\pageheight for the size of each image. This is in landscape mode though.
This is not logical to me as that would mean the two images together would span 1.2 times the height of the page.

I've also tried to set my height of the image in portrait mode just to test. I've set height=0.5\textheight and height=0.5\paperheight but both generate images that are too large to fit together on one page.

Best Answer

The following example assumes that the images can be scaled to the half of the text height, then the example measures the height of the captions and calculates the available height for the images.

\documentclass{article}
\usepackage[pass,showframe]{geometry}

\newsavebox\CaptionBoxA
\newsavebox\CaptionBoxB
\newlength\ImgHeight   

\begin{document}

\begin{figure}
  \vbox to \textheight{%
    \centering
    \setbox\CaptionBoxA=\vbox{%
      \begingroup % color support
        \centering
        \caption{My first figure}%
        \label{fig:first}%
      \endgroup
    }
    \setbox\CaptionBoxB=\vbox{%
      \begingroup % color support
        \centering
        \caption{My second figure}%
        \label{fig:second}%
      \endgroup
    }
    \setlength{\ImgHeight}{%
      .5\dimexpr\textheight 
        -\ht\CaptionBoxA-\dp\CaptionBoxA
        -\ht\CaptionBoxB-\dp\CaptionBoxB
        -\floatsep
      \relax
    }

    \rule{10mm}{\ImgHeight}%
    % \includegraphics[height=\ImgHeight,width=\linewidth,keepaspectratio]{...}

    \unvbox\CaptionBoxA

    \vspace{\floatsep}
    \vspace{0pt minus .25\floatsep}% glue for safety
    \vspace{0pt plus 1fil}% glue for smaller images 
    \nointerlineskip % interline skip affects the calculation of \ImgHeight

    \rule{10mm}{\ImgHeight}
    % \includegraphics[height=\ImgHeight,width=\linewidth,keepaspectratio]{...}

    \unvbox\CaptionBoxB
    % \vspace{0pt plus 1fil}% glue for smaller images
}
\end{figure}

\end{document}

Result with paper size A5 and rules instead of images:

Result

Remarks:

  • The code would have to be put in a macro for practical usage.
  • There is room for improvements. For example, the available space is equally distributed among the images. If one image cannot be scaled to the available space because of width limitations, then the other image could grow.
  • There is room for extensions (three and more images, ...).
Related Question