# Vertically center two side by side figures, leaving captions on the same line

I would like to place two independent figures (no subfigures) besides each other. Each figure should have its own caption. This approach uses two minipages inside a figure environment and is nearly what I'm looking for. But my figures don't have the same height, so I want to center them vertically, whereas the captions should stay on the same line.

Here is an example:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}

\begin{document}

\begin{figure}
\centering
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth, height=.8\linewidth]{image1}
\captionof{figure}{A figure}
\label{fig:test1}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth, height=.4\linewidth]{image1}
\captionof{figure}{Another figure}
\label{fig:test2}
\end{minipage}
\end{figure}

\end{document}


This shows how the output looks vs. how I would like it to look:

A layman's solution is to use four minipages - two for figures and two for captionof.

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption,showframe}

\begin{document}

\begin{figure}
\centering
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth, height=.8\linewidth]{example-image-a}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth, height=.4\linewidth]{example-image-b}
\end{minipage}
\par
\medskip
\noindent
\begin{minipage}[t]{.49\textwidth}
\centering
\captionof{figure}{A figure}
\label{fig:test1}
\end{minipage}%
\hfill
\begin{minipage}[t]{.49\textwidth}
\centering
\captionof{figure}{Another figure Another figure Another figure Another figure Another figure Another figure Another figure}
\label{fig:test2}
\end{minipage}
\end{figure}

\end{document}


• For me, the captions will be horizontally centered only if I set their respective minipages' width to .5\textwidth as well (this is due to the \hfill). In addition, the vertical space between figure 1 and its caption looks too small compared to a normal figure. – ph4nt0m Jun 11 '14 at 7:34

I constructed \figstrut which takes two argument.

• # 2: Height of highest object

It creates a \vrule to centre the object with respect to a certain height. In the example the highest object is the other figure with .8\linewidth and the object to centre is .4\linewidth high. Hence we need

\figstrut{.4\linewidth}{.8\linewidth}


to adjust the line to the correct height.

# Example

\documentclass{article}
\pagestyle{empty}
\usepackage[demo]{graphicx}
\usepackage{caption}
\newcommand\figstrut[2]{
% #1: Height of object
% #2: Height of highest object
\dimen0=#1%
\divide\dimen0 by -2%
\dimen1=#1%
\vrule height \dimen1 depth \dimen0 width 0pt\relax%
}
\begin{document}
\begin{figure}
\centering
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=.4\linewidth, height=.8\linewidth]{image1}
\captionof{figure}{A figure}
\label{fig:test1}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\figstrut{.4\linewidth}{.8\linewidth}
\includegraphics[width=.4\linewidth, height=.4\linewidth]{image1}
\captionof{figure}{Another figure}
\label{fig:test2}
\end{minipage}
\end{figure}
\end{document}


• Thanks for your suggestion. Some of my figures are TikZ pictures, though, in which case it's not that easy to find the exact height parameters for your \figstrut command. I would prefer some automatic solution, but as a workaround, this is certainly ok. :) – ph4nt0m Jun 10 '14 at 19:40

I use a box to save the heighest object. Using height and depth of this box the other object is set in a vertical centered minipage of the same totalheight:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}

\newsavebox\mybox

\begin{document}

\begin{figure}
\sbox\mybox{%
\includegraphics[width=.2\linewidth, height=.4\linewidth]{image1}
}
\centering
\begin{minipage}{.5\textwidth}
\centering
\usebox\mybox
\captionof{figure}{A figure}
\label{fig:test1}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\begin{minipage}[c][\dimexpr\ht\mybox+\dp\mybox\relax][c]{\linewidth}
\centering\includegraphics[width=.4\linewidth, height=.4\linewidth]{image1}
\end{minipage}%
\captionof{figure}{Another figure}
\label{fig:test2}
\end{minipage}
\end{figure}

\end{document}


Note that \linewidth inside a minipage means the width of the minipage.

I found an easy solution using the floatrow package, which I find most satisfactory, so I'm going to accept this as the answer:

\documentclass{article}
\usepackage{caption}
\usepackage[rawfloats=true]{floatrow}

\begin{document}

\begin{figure}
\begin{floatrow}
\ffigbox{%
\rule{3cm}{6cm}% replace with figure
}{%
\captionof{figure}{A figure}%
\label{fig:test1}%
}
\ffigbox{%
\rule{3cm}{3cm}% replace with figure
}{%
\captionof{figure}{Another figure}%
\label{fig:test2}%
}
\end{floatrow}
\end{figure}

\end{document}


The vertical alignment can be modified simply by changing the valign setting with \floatsetup. You can also set this globally by moving the respective line into your preamble.

The rawfloats package option is important in order not to break existing standard floats.