# Laying out subfigures with minipages

I want to place 3 subfigures in a figure with two columns. There is one big subfigured placed to the right. The two subfigures on the left should be aligned so that the first is aligned with the top of the big subfigure, and the second is aligned with the caption of the big subfigure.

\begin{figure}[h!]
\begin{minipage}[t]{0.49\linewidth}
\begin{subfigure}{\linewidth}
\begin{lstlisting}[language=c,escapechar=+]
+{public void}+ Method () {
.. Before
.. Original code
}
\end{lstlisting}
\label{fig:before}
\end{subfigure}
\begin{subfigure}{\linewidth}
\begin{lstlisting}[language=c,escapechar=+]
+{public void}+ Method () {
.. Around ( *(Original code) )
}
\end{lstlisting}
\label{fig:around}
\end{subfigure}
\end{minipage}
\begin{minipage}{0.49\linewidth}
\begin{subfigure}{\linewidth}
\begin{lstlisting}[language=c,escapechar=+]
+{public void}+ Method () {
try {
.. Original code
.. After returning
}
catch (...) {
.. After throwing
}
finally {
.. After
}
}
\end{lstlisting}
\label{fig:after}
\end{subfigure}
\end{minipage}
\end{figure}

-

Here is a possible solution.

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{subcaption}
\usepackage{listings}

\begin{document}
\newsavebox{\temp}
\begin{lrbox}{\temp}
\begin{minipage}{\linewidth}
\begin{lstlisting}[language=c,escapechar=+]
+{public void}+ Method () {
try {
.. Original code
.. After returning
}
catch (...) {
.. After throwing
}
finally {
.. After
}
}
\end{lstlisting}
\end{minipage}
\end{lrbox}
\newlength{\tempd}
\newlength{\tempc}
\settoheight{\tempd}{\usebox{\temp}}
\settodepth{\tempc}{\usebox{\temp}}

\begin{figure}[h!]
\begin{minipage}[c]{0.49\linewidth}
\begin{minipage}[t][\tempd][c]{\linewidth}
\begin{lstlisting}[language=c,escapechar=+]
+{public void}+ Method () {
.. Before
.. Original code
}
\end{lstlisting}
\label{fig:before}

\vfill

\begin{lstlisting}[language=c,escapechar=+]
+{public void}+ Method () {
.. Around ( *(Original code) )
}
\end{lstlisting}
\end{minipage}
\label{fig:around}
\end{minipage}
\begin{minipage}[c][\tempd][c]{0.49\linewidth}%
\usebox{\temp}
\label{fig:after}
\end{minipage}


The idea is to calculate the total height of the longer listing and then set the height of the minipages wrapping the two listings on the left and the listing on the right to such measure. An issue is that we cannot use \caption outside a float, thus we have to use extra minipage as wrappers and to enclose the captions. The package subcaption is used to generate the captions for the subfigures.
@Matthias I have edited the solution. I have used the package subcaption to produce the captions for the subfigures. –  Guido Jan 5 at 6:44