6

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 2x2 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 2x2 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 2x2 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.

SemtexB
  • 259

3 Answers3

8

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

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • but in your code the 2x2 grid of small images is not in the same row as the img7?! – SemtexB Sep 02 '16 at 15:04
  • great, that's what I need! just one small thing: To fully accept your answer could you add \vspace{.6ex} behind \raisebox{-\height}{\includegraphics[width=0.49\textwidth]{img4.png}}? I know it's a minor change, but if someone comes across this question, I'd like to have it fully answered, if possible. – SemtexB Sep 03 '16 at 10:18
  • and just out of own interest: why do you need the origin=tl option in the first \includegraphics? – SemtexB Sep 03 '16 at 10:24
  • Oops, old experiment which didn't work. I will remove it. – John Kormylo Sep 03 '16 at 14:36
  • there's a little piece of magic here: the % comment on the line before the \vspace{.6ex}. Why is it needed? I note that the \vspace is ignored if it is on a new line – Ciprian Tomoiagă Feb 12 '19 at 01:38
  • 1
    @CiprianTomoiagă - \vspace{} is implemented at the end of the current line, so you want to be sure it is not accidentally pushed to the next line. Note that almost every time you end a line with } you are adding an extra \space. – John Kormylo Feb 12 '19 at 15:55
0

I typically use subfloat instead of subfig and don't nest them. Try this MWE code:

\usepackage{subfig}

%%%%%%%%%%%%

\begin{figure}[htb]
\caption{Caption of Figure}
\label{fig:figlabel}
\begin{center}
\subfloat[Subcaption a]      {
\includegraphics[width=.45\linewidth]{image1}
}\quad
\subfloat[Subcaption b]      {
\includegraphics[width=.45\linewidth]{image2}
}\\
\subfloat[Subcaption c]      {
\includegraphics[width=.45\linewidth]{image3}
}\quad
\subfloat[Subcaption d]      {
\includegraphics[width=.45\linewidth]{image4}
}
\end{center}

I typically use \\ and \quad to respectively do vertical and horizontal spacing. You can adjust the width of the graphic accordingly if you want 3 images per row, 1 image per row, etc.

Tyler R.
  • 161
  • 1
  • 5
  • There's also this related question. [http://tex.stackexchange.com/questions/111822/using-subfloat-to-place-figures-side-by-side] – Tyler R. Sep 02 '16 at 13:42
0

Your code is not compilable so I am not sure if I got your right. Do you want this?

% arara: pdflatex

\documentclass[12pt,a4paper,twoside]{article}
\usepackage{subcaption}
\usepackage[demo]{graphicx}

\begin{document} 
    \begin{figure}
        \centering
        \begin{subfigure}[t]{0.49\textwidth}
            \includegraphics[width=\textwidth]{img1}
            \caption{caption of first image}
        \end{subfigure}
        \hfill
        \begin{subfigure}[t]{0.49\textwidth}
            \includegraphics[width=\textwidth]{img2}
            \caption{caption of second image}
        \end{subfigure}

        \begin{subfigure}[t]{0.49\textwidth}
            \begin{subfigure}[t]{0.49\textwidth}
                \includegraphics[width=\textwidth]{img3}
            \end{subfigure}
            %
            \begin{subfigure}[t]{0.49\textwidth}
                \includegraphics[width=\textwidth]{img4}
            \end{subfigure}\vspace{.6ex}

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

enter image description here

Note that you have to leave a blank line if you want a line break. I removed your %%%%% for this.

Also I have never seen the [ht] qualifier for a subfigure. I am not sure but I guess, this does not exist.

LaRiFaRi
  • 43,807
  • close, but the four small images shall be "squeezed" into the second row and first column. in your output the second row of the small images is in the third row of the whole figure. – SemtexB Sep 02 '16 at 14:07
  • Nevertheless, vspace{.6ex} solved point 2) – SemtexB Sep 02 '16 at 14:15
  • and the [ht] brings the four small ones and the last one onto the same level. So [ht] is kind of crucial here?! – SemtexB Sep 02 '16 at 14:18