[Tex/LaTex] How to build and combine sophisticated layouts in figures

captionsfloatrowfloatspositioningsubfloats

How can I get a layout like the one below in my document?

                enter image description here

With this question, I am looking for:

  1. An answer for the above layout
  2. General advice on which combination of packages is recommended to build sophisticated layouts. By sophisticated layouts I mean layouts where:

    • I can tile figures and subfigures with different sizes but still have them vertically or horizontally aligned.
    • I can have the ability to reference subfigures from the text with links, e.g. "In Fig. 2(a) we have.."

In addressing this problem, I think Mico's answer in this thread: Vertically align subfloats at the top while having subcaptions vertically aligned below the subfloats is releveant, where he said:

According to the latest edition of the l2tabu document, both the
subfigure and subfig packages should no longer be used; instead, one
should use the subcaption package (from the same author of the caption
package).

With this in mind, I thought a good combination of packages to build sophisticated layouts would be:

\usepackage{floatrow}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{calc}
\usepackage{graphicx}

so I used them to try building my layout below, but it didn't work as expected. Below is the code that shows my attempt:

\begin{figure}[t!]%
\begin{floatrow}[2]%
\ffigbox[\FBwidth]% Width of the subfloat:
{%
   \includegraphics[width=0.20\textwidth]{1-1.png}%
   \includegraphics[width=0.20\textwidth]{1-2.png}%
   \includegraphics[width=0.20\textwidth]{1-3.png}%
   \includegraphics[width=0.20\textwidth]{1-4.png}%    
}{\caption{Foo}\label{fig:1}}%
\ffigbox[\Xhsize]%
{%
    \begin{subfloatrow}%
    \ffigbox[\FBwidth]% Width of the subfloat:
    {%
    \includegraphics[width=0.20\textwidth]{2-A.png}%
    }
    {%
      \subcaption{}\label{fig:2-A}%
    }
    \ffigbox[\FBwidth]% Width of the subfloat:
    {%
    \includegraphics[width=0.20\textwidth]{2-B.png}%
    }
    {%
      \subcaption{}\label{fig:2-B}%
    }
    \end{subfloatrow}
}%
{\caption{Results. subref{fig:2-A}: Foo. \subref{fig:1b}: Bar}}
\end{floatrow}%
\end{figure}

Best Answer

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage[labelformat=simple]{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})} % see subcaption doc

\begin{document}

\begin{figure}
\captionsetup{singlelinecheck=off}
\captionsetup[subfigure]{singlelinecheck=on}
%
\captionbox{Foo\label{fig:1}}[0.42\textwidth][l]{%
   \includegraphics[width=0.20\textwidth]{1-1.png}%
   \phantomsubcaption\label{fig:1a}% Bug: Produces an unwanted space
   \includegraphics[width=0.20\textwidth]{1-2.png}%
   \phantomsubcaption\label{fig:1b}%
\\
   \includegraphics[width=0.20\textwidth]{1-3.png}%
   \phantomsubcaption\label{fig:1c}% Bug: Produces an unwanted space
   \includegraphics[width=0.20\textwidth]{1-4.png}%
   \phantomsubcaption\label{fig:1d}%
}
\captionbox{Results. \subref{fig:2-A}: Foo. \subref{fig:1b}: Bar}{%
  \subcaptionbox{\label{fig:2-A}}{%
    \includegraphics[width=0.20\textwidth]{2-A.png}}
  \subcaptionbox{\label{fig:2-B}}{%
    \includegraphics[width=0.20\textwidth]{2-B.png}}
}%
\end{figure}

\end{document}

Note that we need to use the optional argument of \captionbox here so \\ will work inside it. Furthermore there is a bug in the current version of the caption package so the combination of \phantomsubcaption and \label will produce an unwanted space. This will be fixed in the very next version 3.3 of the caption package. Until then one can place the \phantomcaptions at the end of the box as workaround:

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{caption}
\usepackage[labelformat=simple]{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})} % see subcaption doc

\begin{document}

\begin{figure}
\captionsetup{singlelinecheck=off}
\captionsetup[subfigure]{singlelinecheck=on}
%
\captionbox{Foo\label{fig:1}}[0.40\textwidth][l]{%
   \includegraphics[width=0.20\textwidth]{1-1.png}%
   \includegraphics[width=0.20\textwidth]{1-2.png}\\
   \includegraphics[width=0.20\textwidth]{1-3.png}%
   \includegraphics[width=0.20\textwidth]{1-4.png}%
   \phantomsubcaption\label{fig:1a}%
   \phantomsubcaption\label{fig:1b}%
   \phantomsubcaption\label{fig:1c}%
   \phantomsubcaption\label{fig:1d}%
}
\captionbox{Results. \subref{fig:2-A}: Foo. \subref{fig:1b}: Bar}{%
  \subcaptionbox{\label{fig:2-A}}{%
    \includegraphics[width=0.20\textwidth]{2-A.png}}
  \subcaptionbox{\label{fig:2-B}}{%
    \includegraphics[width=0.20\textwidth]{2-B.png}}
}%
\end{figure}

\end{document}

(\captionbox is not documented yet but is available since version 3.2 of the caption package; its syntax is equal to \subcaptionbox but will produce a regular caption instead.)