# How can I vertically align two minipages with subfigures and differing caption length?

Following "Placing two figures (each having subfigures) next to each other", I used the following code in order to place two figures with subfigures next to each other putting the subfigures vertically:

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subfig} % Needs subfig for this example.

\begin{document}

\begin{figure}
\begin{minipage}{.48\textwidth}
\centering
\subfloat[]{\includegraphics[width=.4\linewidth]{fig1}}\\
\subfloat[]{\includegraphics[width=.4\linewidth]{fig2}}
\caption{First figure with two subfigures with very very very very long caption}
\label{fig:testa}
\end{minipage}\hfill
\begin{minipage}{.48\textwidth}
\centering
\subfloat[]{\includegraphics[width=.4\linewidth]{fig3}}\\
\subfloat[]{\includegraphics[width=.4\linewidth]{fig4}}
\caption{Second figure with two subfigures}
\label{fig:testb}
\end{minipage}
\end{figure}

\end{document}


This is already the style that I wanted but it does not look very nice if the captions are different because this leads to the fact that the pictures placed next to each other are not at the same hight. How can I change this? I tried to place a minipage around every picture but this does not change anything.

Edit

Actually, I think I have not really asked the right question, sorry! In fact, I have three subfigures in each minipage, each having a caption and these captions have a different length. In this case it does not help me to align the two figures at the top or bottom because the subfigures do still not match. Sorry for not having explained this better in the first place.

• If you need more information about vertical alignment of mini-pages, you may want to read this: tex.stackexchange.com/a/34172/32245 – LaRiFaRi Sep 3 '13 at 19:26
• Please see my updated answer for a possible solution for your new requirement when the subcaptions have different height. – Gonzalo Medina Sep 3 '13 at 19:43

You can use t as the optional argument for the minipages to have vertical alignment at the top:

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subfig} % Needs subfig for this example.

\begin{document}

\begin{figure}
\begin{minipage}[t]{.48\textwidth}
\centering
\subfloat[]{\includegraphics[width=.4\linewidth]{fig1}}\\
\subfloat[]{\includegraphics[width=.4\linewidth]{fig2}}
\caption{First figure with two subfigures with very very very very long caption}
\label{fig:testa}
\end{minipage}\hfill
\begin{minipage}[t]{.48\textwidth}
\centering
\subfloat[]{\includegraphics[width=.4\linewidth]{fig3}}\\
\subfloat[]{\includegraphics[width=.4\linewidth]{fig4}}
\caption{Second figure with two subfigures}
\label{fig:testb}
\end{minipage}
\end{figure}

\end{document}


After the edit to the question: the same idea (minipages with appropriate vertical alignment can be used if the sucaptions have different height); now, the second optional argument for minipage can be used to specify its height; this will guarantee that each image on the left will be placed at the same height as its corresponding image on the right:

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage{subfig} % Needs subfig for this example.

\begin{document}

\begin{figure}
\begin{minipage}[t]{.48\textwidth}
\centering
\begin{minipage}[t][5cm][t]{\linewidth}
\centering
\subfloat[a little longer text here; it will span several lines]{\includegraphics[width=.4\linewidth]{fig1}}
\end{minipage} \\
\begin{minipage}[t][6cm][t]{\linewidth}
\centering
\subfloat[a little longer text here; it will span several lines just for illustration in this example]{\includegraphics[width=.4\linewidth]{fig2}}
\end{minipage} \\
\begin{minipage}[t][5cm][t]{\linewidth}
\centering
\subfloat[a little longer text here; it will span several lines]{\includegraphics[width=.4\linewidth]{fig3}}
\end{minipage}
\caption{First figure with two subfigures with very very very very long caption}
\label{fig:testa}
\end{minipage}\hfill
\begin{minipage}[t]{.48\textwidth}
\centering
\begin{minipage}[t][5cm][t]{\linewidth}
\centering
\subfloat[short text]{\includegraphics[width=.4\linewidth]{fig4}}
\end{minipage} \\
\begin{minipage}[t][6cm][t]{\linewidth}
\centering
\subfloat[short text]{\includegraphics[width=.4\linewidth]{fig5}}
\end{minipage} \\
\begin{minipage}[t][5cm][t]{\linewidth}
\centering
\subfloat[short text]{\includegraphics[width=.4\linewidth]{fig6}}
\end{minipage}
\caption{Second figure with two subfigures}
\label{fig:testb}
\end{minipage}
\end{figure}

\end{document}


One could even automate the calculation for the necessary heights.

Now, it has been mentioned in a comment that the images might possibly have different heights (and widths, so this is not important); in this case, one can go with a tabular and \captionof from the subcaption package; a little careful work has to be done with the counters:

\documentclass{book}
\usepackage[demo]{graphicx}
\usepackage{array}
\usepackage{subcaption}

\captionsetup[subfigure]{size=footnotesize,labelformat=parens,labelsep=space}

\newcolumntype{C}{>{\centering\arraybackslash}p{0.4\linewidth}}
\newcounter{oldfig}

\begin{document}

\begin{figure}[!ht]
\begin{tabular}{CC}
\stepcounter{figure}
\setcounter{oldfig}{\value{figure}}
\stepcounter{oldfig}
\includegraphics[width=.6\linewidth,height=1cm]{fig1}
&
\includegraphics[width=.3\linewidth,height=2cm]{fig4}
\\
\captionof{subfigure}{a little longer text here; it will span several lines}\label{sfig:11}
&
\setcounter{figure}{\value{oldfig}}
\captionof{subfigure}{short text}\label{sfig:12}
\\
\includegraphics[width=.4\linewidth,height=1cm]{fig2}
&
\includegraphics[width=.5\linewidth,height=2cm]{fig5}
\\
\captionof{subfigure}{a little longer text here; it will span several lines just for illustration in this example}\label{sfig:21}
&
\stepcounter{figure}
\stepcounter{subfigure}
\setcounter{figure}{\value{oldfig}}
\captionof{subfigure}{short text}\label{sfig:22}
\\
\includegraphics[width=.4\linewidth]{fig3}
&
\includegraphics[width=.2\linewidth,height=2.4cm]{fig6}
\\
\captionof{subfigure}{a little longer text here; it will span several lines}\label{sfig:31}
&
\stepcounter{figure}
\stepcounter{subfigure}
\setcounter{figure}{\value{oldfig}}
\captionof{subfigure}{short text}\label{sfig:32}
\\
\captionof{figure}{First figure with two subfigures with very very very very long caption}\label{fig:testa}
&
\captionof{figure}{second figure with short caption}\label{fig:testb}
\end{tabular}
\end{figure}

Some references to figures ~\ref{fig:testa} and \ref{fig:testb}.

Some references to subfigures ~\ref{sfig:11}, \ref{sfig:12}, \ref{sfig:21}, \ref{sfig:22}, \ref{sfig:31}, and~\ref{sfig:32}.

\end{document}


• Thanks! One last question about this: What do I do if my pictures have different aspect ratios? In that case the subcaptions are still not aligned even if the pictures are. – warsaga Sep 3 '13 at 20:48
• @warsaga why didn't you mention this from the beginning? Had you done so, a lot of time would have been saved. Please edit your question and add there a realistic example code of your current situation. – Gonzalo Medina Sep 3 '13 at 20:56
• @warsaga please see my updated answer, with a possible solution for this new requirement. – Gonzalo Medina Sep 3 '13 at 22:36
• How to align unequal size images based on top? – pushpen.paul Oct 18 '17 at 16:38