# Making two figures the same scale and fit on a line

Let's say I have a bunch of images, (two in this MWE)

\begin{figure}[h]
\begin{subfigure}{\linewidth}
\includegraphics[width=.95\linewidth]{whacknot}
\caption{Fritz-Qualdor count of greezable whacknotters}
\end{subfigure}
\begin{subfigure}{\linewidth}
\includegraphics[width=.95\linewidth]{qualprift}
\caption{Corrected null-count of pre-groused qualprifters}
\end{subfigure}
\caption{Counting methods used in theta-analysis}
\end{figure}


Sure enough, I now have two images underneath each other of the same size. However, what I actually want is to have two images of exactly the same scale, but both at most .95\linewidth (the images are generated programatically, but due to overzealous cropping, they all have a slightly different size). I could of course manually twiddle with a [scale=something] on all the figures, but that Is there a way to do this automatically in LaTeX?

• So you want the two subfigures side-by-side, on the same line, but their combined width no more than 0.95\linewidth? – erik May 24 '19 at 21:20
• tex.stackexchange.com/a/492188/189324 might help – wun May 24 '19 at 22:13

The adjustbox environment has a max width option, so you could place your side-by-side subfigures inside an adjustbox. This should preserve their original scales, but scale down both together if necessary. Edit: However, this would scale the subcaption as well. I'm not sure how to prevent this, but you can provide a phantomsubcaption and label in order to refer to the subfigures in the main figure caption.

\documentclass{article}
\captionsetup{subrefformat=parens}

\providecommand{\samelinefigs}[4]{%
{\includegraphics{#3}\phantomsubcaption\label{#4}}
}

\begin{document}
Two smaller figures, unscaled:
\begin{figure}[h]
\centering
\samelinefigs{example-grid-100x100pt}{fig:grid}{example-image-4x3}{fig:4by3}
\caption{Caption for \subref{fig:grid} grid and \subref{fig:4by3} generic image.}
\end{figure}

Two larger figures, both scaled:
\begin{figure}[h]
\centering
\samelinefigs{example-image-16x10}{fig:16by10}{example-image-16x9}{fig:16by9}
\caption{Caption}
\end{figure}

Original size of $16\times9$ figure:

\includegraphics{example-image-16x9}

\end{document}


• this will badly scale the subcaption font – wun May 24 '19 at 21:37
• @wun Indeed. I've proposed a workaround in my edited answer. – erik May 24 '19 at 22:09

One can modify the question Print the "scale factor" on each figure as a watermark to save the previous scaling factor for usage in the next image:

\documentclass{article}

\usepackage{subcaption}
\usepackage{graphicx}

\makeatletter
\let\ORG@Gscale@box\Gscale@box
\long\def\Gscale@box#1{%
\xdef\thelastscalefactor{#1}%
\ORG@Gscale@box{#1}}
\makeatother

\newlength{\imagea}
\newlength{\imageb}
\newbox\vhbox

\begin{document}

% Enter the image names here
\newcommand{\imgA}{example-image-16x10}
\newcommand{\imgB}{example-image-9x16}

\settowidth{\imagea}{\includegraphics{\imgA}}
\settowidth{\imageb}{\includegraphics{\imgB}}
\begin{figure}[h]
\ifdim\imagea>\imageb
\begin{subfigure}{\linewidth}
\includegraphics[width=.95\linewidth]{\imgA}
\caption{Fritz-Qualdor count of greezable whacknotters}
\end{subfigure}
\begin{subfigure}{\linewidth}
\includegraphics[scale=\thelastscalefactor]{\imgB}
\caption{Corrected null-count of pre-groused qualprifters}
\end{subfigure}
\else
\setbox\vhbox\vbox{\includegraphics[width=.95\linewidth]{\imgB}}
\begin{subfigure}{\linewidth}
\includegraphics[scale=\thelastscalefactor]{\imgA}
\caption{Fritz-Qualdor count of greezable whacknotters}
\end{subfigure}
\begin{subfigure}{\linewidth}
\includegraphics[width=.95\linewidth]{\imgB}
\caption{Corrected null-count of pre-groused qualprifters}
\end{subfigure}
\fi
\caption{Counting methods used in theta-analysis}
\end{figure}

\end{document}

• Not bad! Now to figure out how to extend this to a larger number of images... – Sanchises May 25 '19 at 9:01