[Tex/LaTex] Alignment of multiple figures in rows and columns

floatssubfloats

I got seven images I want to show in a single figure. Two large ones shall be aligned in the first row in two columns. The second row shall consist of four half size images below the first image (located in row=0 and col=0), where the half size image are aligned on a 2×2 grid, which only occupies one row though. The last image is of the same size as the first two and shall be aligned below the second image (located in row=0 and col=1). I already got this, which pretty much does what I need, but with some minor drawbacks:

\documentclass[12pt,a4paper,twoside, draft]{article}
\usepackage{graphicx}
\usepackage{subcaption} %to have subfigures available

\begin{document} 
\begin{figure}[h!]
     \centering
    \begin{subfigure}[t]{0.49\textwidth}
        \includegraphics[width=\textwidth]{img1.png}
        \caption{caption of first image}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.49\textwidth}
        \includegraphics[width=\textwidth]{img2.png}
        \caption{caption of second image\\second line}
    \end{subfigure}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%second row
    \begin{subfigure}[ht]{0.49\textwidth}
            \begin{subfigure}[t]{0.49\textwidth}
                \includegraphics[width=\textwidth]{img3.png}
            \end{subfigure}
            %
            \begin{subfigure}[t]{0.49\textwidth}
                \includegraphics[width=\textwidth]{img4.png}
            \end{subfigure}

            \begin{subfigure}[t]{0.49\textwidth}
                \includegraphics[width=\textwidth]{img5.png}
            \end{subfigure}
            %
            \begin{subfigure}[t]{0.49\textwidth}
                \includegraphics[width=\textwidth]{img6.png}
            \end{subfigure}
        \caption{caption of the four small images\\second line\\third line}
    \end{subfigure}
    \hfill
    \begin{subfigure}[ht]{0.49\textwidth}
        \includegraphics[width=\textwidth]{img7.png}
    \caption{caption of last image\\second line} 
    \end{subfigure}
    \caption{caption of main figure}
\end{figure}
\end{document}

With drawbacks I mean
1) the top border of the images in the second row is not nicely aligned. How can I fix this?
2) the four images in the 2×2 grid show space when being next to each other, but are too close above of each other. How can I get some space between first and second row in the 2×2 grid.

EDIT: I just noticed the problems might only partially appear here, since all captions fit in one line. If the captions of the first two images occupy a different amount of lines then 1) will appear.

Best Answer

The [t] option uses the baseline of the first line (same as minipage). You can see the baseline using \leavevmode\rlap{\rule{\textwidth}{1pt}}. So subfigures using [t] are actually aligned by the bottoms of the images, not the tops. To align the tops you would need \raisebox{-\height}{...} on every image.

\documentclass[12pt,a4paper,twoside, draft]{article}
\usepackage{graphicx}
\usepackage{subcaption} %to have subfigures available

\begin{document} 
\begin{figure}
     \centering
    \begin{subfigure}[t]{0.49\textwidth}
        \raisebox{-\height}{\includegraphics[width=\textwidth]{img1.png}}
        \caption{caption of first image}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.49\textwidth}
        \raisebox{-\height}{\includegraphics[width=\textwidth]{img2.png}}
        \caption{caption of second image\\second line}
    \end{subfigure}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%second row
    \begin{subfigure}[t]{0.49\textwidth}
        \raisebox{-\height}{\includegraphics[width=0.49\textwidth]{img3.png}}
        \raisebox{-\height}{\includegraphics[width=0.49\textwidth]{img4.png}}%
        \vspace{.6ex}
        \raisebox{-\height}{\includegraphics[width=0.49\textwidth]{img5.png}}
        \raisebox{-\height}{\includegraphics[width=0.49\textwidth]{img6.png}}
        \caption{caption of the four small images\\second line\\third line}
    \end{subfigure}
    \hfill
    \begin{subfigure}[t]{0.49\textwidth}
        \raisebox{-\height}{\includegraphics[width=\textwidth]{img7.png}}
    \caption{caption of last image\\second line} 
    \end{subfigure}
    \caption{caption of main figure}
\end{figure}
\end{document}

demo

Related Question