# Side by side figures adjusted to have equal height

I have two graphics of different dimensions which I would like to put into my document side by side such that:

1. The two figures are scaled to have the same height.
2. The two figures take up the entire text width.

I have seen the mini-page approach to side-by-side figures, but you have to declare the size of the mini-pages ahead of time. That will not work here.

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{center}
X\dotfill X

\resizebox{\textwidth}{!}{%
\includegraphics[height=1cm]{example-image}
\includegraphics[height=1cm]{example-image-4x3}}
\end{center}

\end{document}


The initial 1cm height is arbitrary, then scale the combined pair to text width.

• This does what I want with the graphics, but is there some way to combine this with the figure environment (so I can include labels, captions, etc). When I tried just including two figures instead of the plain \includegraphics, I got 18 errors. – Trevor J Richards May 13 '15 at 0:12
• @Trevor you can put that in a figure environment and can have two captions (eg each in a minipage) but as you wouldn't want the caption text scaled and the dividing line between the images is implicit may take a bit of calculation or hand work to position the captions. – David Carlisle May 13 '15 at 0:15

Here is a way to do it (I first show it with 4 figures, and later in the answer, with 2). It requires some work, but at least one does not have to manually calculate the widths of minipages. In my MWE, I show more than is needed, just for explanation purposes. I show my 4 figures (\figa through \figd) as simple \rule boxes of different colors.

I then use my \scalerel package to make them all the same height as figd and show that result. I show a rule of width \textwidth to illustrate what the desired overall width is.

I then calculate the scale ratio (using the fp package) to get the aggregate row of pictures to fill \textwidth-3ex where the 3ex is the total gap to be introduced between pictures.

I use that scale ratio to on each of the respective images, in a temporary box0 so that I can use that width for a minipage. Once in the top-aligned minipage, I can regurgitate the temporary box and place a \captionof caption beneath it.

\documentclass{article}
\usepackage{scalerel}
\usepackage{fp}
\usepackage{caption}
\usepackage{xcolor}
\parindent 0pt
\begin{document}
\def\figa{\color{blue}\rule{.7in}{.7in}}
\def\figb{\color{red}\rule{.7in}{.3in}}
\def\figc{\color{green}\rule{.5in}{.5in}}
\def\figd{\color{black}\rule{1.in}{.8in}}

Figures as is:

\figa\figb\figc\figd % REMOVE THIS TO SUPPRESS "Figures as is"

figures scaled to height of figd

\newsavebox\x
\sbox\x{\scalerel{$\figc$}{$\figd$}}
\sbox\x{\scalerel{$\figb$}{\usebox{\x}}}
\sbox\x{\scalerel{$\figa$}{\usebox{\x}}}
\usebox{\x}% REMOVE THIS TO SUPPRESS "Figures scaled to height of figd"

textwidth rule

\rule[1ex]{\textwidth}{.1ex}

\newcount\figwidthc
\newcount\textwidthc
\figwidthc=\wd\x
\textwidthc=\dimexpr\textwidth-3ex
\FPdiv\scaleratio{\the\textwidthc}{\the\figwidthc}

individual figures scaled to textwidth (-3ex, allowing for 1ex gap)

\setbox0=\hbox{\scalebox{\scaleratio}{\scalerel*{$\figa$}{$\figd$}}}
\begin{minipage}[t]{\wd0}
\box0
\captionof{figure}{The first figure}
\end{minipage}\kern1ex%
\setbox0=\hbox{\scalebox{\scaleratio}{\scalerel*{$\figb$}{$\figd$}}}%
\begin{minipage}[t]{\wd0}
\box0
\captionof{figure}{The next figure}
\end{minipage}\kern1ex%
\setbox0=\hbox{\scalebox{\scaleratio}{\scalerel*{$\figc$}{$\figd$}}}%
\begin{minipage}[t]{\wd0}
\box0
\captionof{figure}{The third figure}
\end{minipage}\kern1ex%
\setbox0=\hbox{\scalebox{\scaleratio}{\figd}}%
\begin{minipage}[t]{\wd0}
\box0
\captionof{figure}{The last figure}
\end{minipage}%

\end{document}


And here it is tailored for two figures only:

\documentclass{article}
\usepackage{scalerel,fp,caption,xcolor}
\parindent 0pt
\newsavebox\x
\newcount\figwidthc
\newcount\textwidthc
\begin{document}
textwidth rule\par
\rule[1ex]{\textwidth}{.1ex}

\def\figc{\color{green}\rule{.5in}{.5in}}
\def\figd{\color{black}\rule{1.in}{.8in}}
\sbox\x{\scalerel{$\figc$}{$\figd$}}
\figwidthc=\wd\x
\textwidthc=\dimexpr\textwidth-3ex
\FPdiv\scaleratio{\the\textwidthc}{\the\figwidthc}
individual figures scaled to textwidth (-3ex, allowing for 3ex gap)

\setbox0=\hbox{\scalebox{\scaleratio}{\scalerel*{$\figc$}{$\figd$}}}%
\begin{minipage}[t]{\wd0}
\box0
\captionof{figure}{The first figure}
\end{minipage}\kern3ex%
\setbox0=\hbox{\scalebox{\scaleratio}{\figd}}%
\begin{minipage}[t]{\wd0}
\box0
\captionof{figure}{The last figure}
\end{minipage}%
\end{document}