I want to group images and assign the same sub-figure number in a figure consisting of several such groups. Please have a look at the following image to understand my question properly.
[Tex/LaTex] how to group images in a figure
floats
Related Solutions
Two options:
Using some
tabular
s for each layer and thesubcaption
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 threec
type columns; if the text below the images should span more than one line, you could usetabularx
from thetabularx
package and centeredX
columns, as the option below illustrates.Since the OP is using
subfig
, thesubcaption
package cannot be used; here's an approach using the same idea as before, but withtabularx
and thesubfig
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}
My suggestion would be to clip the image contents within LaTeX; graphicx
allows for that:
\documentclass{article}
\usepackage{graphicx,subcaption}
\begin{document}
\begin{figure}
\centering
\setbox1=\hbox{\includegraphics{example-image-a}}%
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=.5\wd1 0 \wd1 \ht1,clip]{example-image-a}}%
\qquad
\setbox1=\hbox{\includegraphics{example-image-b}}%
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=0 0 .5\wd1 \ht1,clip]{example-image-b}}
\caption{This is main figure}
\end{figure}
\end{document}
I've used example-image-a
and example-image-b
(from mwe
) above, although you will use the same image. As such, you'll only need to capture the image in box 1 once. Note how the image is split in half (at the .5\wd1
mark) with the viewport
and clip
combination.
Additionally, I've used subcaption
, but you can manage the same with subfig
.
Here is an example of breaking up a single image into six equal parts (perhaps synonymous to your situation):
\documentclass{article}
\usepackage{graphicx,subcaption}
\begin{document}
\begin{figure}
\centering
\setbox1=\hbox{\includegraphics{example-image}}%
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=0 0 .16667\wd1 \ht1,clip]{example-image}}%
\quad
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=.16667\wd1 0 .3333\wd1 \ht1,clip]{example-image}}%
\quad
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=.3333\wd1 0 .5\wd1 \ht1,clip]{example-image}}%
\quad
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=.5\wd1 0 .66667\wd1 \ht1,clip]{example-image}}%
\quad
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=.6667\wd1 0 .8333\wd1 \ht1,clip]{example-image}}%
\quad
\subcaptionbox{}{\centering\includegraphics[scale=0.5,viewport=.8333\wd1 0 \wd1 \ht1,clip]{example-image}}%
\caption{This is main figure}
\end{figure}
\end{document}
Each component is extracted as a sixth of the total width \wd1
, one step at a time.
Best Answer
Two options, but the general idea is the same: to use one
subfigure
environment (requiressubcaption
) or\subfloat
command (requiressubfig
) for each group.Here's one option using the powerful
subcaption
package:The output:
And now with the help of the
subfig
package:The output:
Another option might be to use the
floatrow
package.