How to have a figure with multiple groups of image and each image with individual name?

I have several images and I must put their names below them. Moreover, I need to give the name of the group of that images too (for example, "first layer", "second layer" ect as show below). I need to have something as shown in the figure below.

IMPORTANT: In my case, a group of images may contain more than one row. I mean, the "(a) first layer" may contain more than 3 images and hence can be a group of images in more than one row.

Two options:

1. Using some tabulars for each layer and the subcaption package:

The code:

\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{document}

\begin{figure}
\begin{minipage}{\linewidth}
\centering
\begin{tabular}{ccc}
\includegraphics[width=.28\linewidth,height=1cm]{example-image-a}
& \includegraphics[width=.28\linewidth,height=1cm]{example-image-b}
& \includegraphics[width=.28\linewidth,height=1cm]{example-image-c} \\
First image & Second image & Third image \\
\includegraphics[width=.28\linewidth,height=1cm]{example-image-a}
& \includegraphics[width=.28\linewidth,height=1cm]{example-image-b} \\
Fourth image & Fifth image &
\end{tabular}
\subcaption{First layer}
\end{minipage}\par\bigskip
\begin{minipage}{\linewidth}
\centering
\begin{tabular}{ccc}
\includegraphics[width=.28\linewidth,height=1cm]{example-image-a}
& \includegraphics[width=.28\linewidth,height=1cm]{example-image-b}
& \includegraphics[width=.28\linewidth,height=1cm]{example-image-c} \\
First image & Second image & Third image \\
\end{tabular}
\subcaption{Second layer}
\end{minipage}
\caption{a figure with some layers of subfigures each with its own name and each layer with a subcaption}
\end{figure}

\end{document}


Above I used a standard tabular with three c type columns; if the text below the images should span more than one line, you could use tabularx from the tabularx package and centered X columns, as the option below illustrates.

2. Since the OP is using subfig, the subcaption package cannot be used; here's an approach using the same idea as before, but with tabularx and the subfig package:

\documentclass{article}
\usepackage[caption=false]{subfig}
\usepackage{graphicx}
\usepackage{tabularx}

\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document}

\begin{figure}
\subfloat[First layer]{%
\begin{minipage}{\linewidth}
\centering
\begin{tabularx}{\linewidth}{YYY}
\includegraphics[width=\linewidth,height=1cm]{example-image-a}
& \includegraphics[width=\linewidth,height=1cm]{example-image-b}
& \includegraphics[width=\linewidth,height=1cm]{example-image-c} \\
First image & Second image & Third image \\
\includegraphics[width=\linewidth,height=1cm]{example-image-a}
& \includegraphics[width=\linewidth,height=1cm]{example-image-b} \\
Fourth image & Fifth image &
\end{tabularx}
\end{minipage}}\par\medskip
\subfloat[Second layer]{%
\begin{minipage}{\linewidth}
\centering
\begin{tabularx}{\linewidth}{YYY}
\includegraphics[width=\linewidth,height=1cm]{example-image-a}
& \includegraphics[width=\linewidth,height=1cm]{example-image-b}
& \includegraphics[width=\linewidth,height=1cm]{example-image-c} \\
First image & Second image & Third image \\
\end{tabularx}
\end{minipage}}
\caption{a figure with some layers of subfigures each with its own name and each layer with a subcaption}
\end{figure}

\end{document}


• It throws an error Undefined control sequence. \subcaption because I am already using \usepackage[caption = false]{subfig} – skm Jul 27 '15 at 22:10
• @skm You should have mentioned that in the first place! subcaption and subfig are incompatible. In some minutes I'll update my answer using subfig instead. – Gonzalo Medina Jul 27 '15 at 22:12
• Actually, I am already using \usepackage[caption = false]{subfig} and hence I cannot use \usepackage{subcaption} in combination to it – skm Jul 27 '15 at 22:12
• @skm Please see the updated answer under the "Update" section. – Gonzalo Medina Jul 27 '15 at 22:16
• @skm If you want normal size for the subcaptions load the subfig package like this: \usepackage[caption=false,font=normalsize]{subfig}. The other option is to use something like \small First image & \small Second image... inside the table to reduce the font size for the image names. – Gonzalo Medina Jul 27 '15 at 22:36

You can have multiple rows, e.g. look at this example from ClassicThesis:

begin{figure}[bth]
\myfloatalign
\subfloat[Asia personas duo.]
\subfloat[Pan ma signo.]
{\label{fig:example-b}%
\includegraphics[width=.45\linewidth]{gfx/example_2}} \\
\subfloat[Methodicamente o uno.]
\subfloat[Titulo debitas.]
{\includegraphics[width=.45\linewidth]{gfx/example_4}}
\caption[Tu duo titulo debitas latente]{Tu duo titulo debitas
latente.}\label{fig:example}
\end{figure}

• it is giving me several error among which one is "undefined control sequence. \myfloatalign". Please tell me, which package should be included for that. – skm Jul 27 '15 at 21:51
• can you provide an screenshot of the figure which will be resulted with the above code. – skm Jul 27 '15 at 21:57
• I will try to create a working document tomorrow, it is 0h06 now.. ok? – rvaneijk Jul 27 '15 at 22:07
• can one say why the answer was downvoted? – touhami Jul 27 '15 at 23:20