2

I am currently studying the answers to this question about subfigures for use in my writing. The results are impressive especially for the last answer posted for the question. However, I didn't find how the number of rows and columns of the graphs i.e subfigures can be specified. I have tried using "subfig" in the last answer. This works fine. Besides, it automatically arranges the sub-figures. However, when I try to increase the size of the sub-figures, it exceeds the bounds of the page. I hope someone could share how the number of rows and columns for the graphs to be specified, or how the whole figure (including all sub-figures) can be scaled. I didn't repeat the code to avoid monotony and redundancy.

Edit: I include a MWE

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

\begin{document}
\begin{figure}[htb]
\centering
\subfloat[A subfigure]{%
    \includegraphics[width=.24\textwidth]{somefig_1}}
\subfloat[A subfigure]{%
    \includegraphics[width=.24\textwidth]{somefig_2}}
\subfloat[A subfigure]{%
    \includegraphics[width=.24\textwidth]{somefig_3}}
\subfloat[A subfigure]{%
    \includegraphics[width=.24\textwidth]{somefig_4}}
\caption{Arranged sub-figures}\label{fig:1}
\end{figure}
\end{document}
  • 1
    Stick with the subcaption approach from the answer given in the question you linked. You can divide the horizontal space in fractions of \linewidth like \begin{subfigure}{.5\linewidth}. If you absolutely must, enclose your subfigures inside a \makebox[\linewidth]{} to make them overlap the margins. – Christoph90 Apr 09 '18 at 07:59
  • Thanks I just included a sample MWE! I guess it would be easier for others to benefit from the question and response better that way. If you wouldn't mind I will appreciate an answer using the MWE as template. Much thanks – Abdulhameed Apr 09 '18 at 08:02

2 Answers2

4

The subcaption package provides the subfigure environment. subfloat is from the subfig package. Try this:

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

\begin{document}
\begin{figure}[htb]
\centering
\begin{subfigure}{0.24\linewidth}
    \includegraphics[width=\linewidth]{somefig_1}
\end{subfigure}
\begin{subfigure}{0.24\linewidth}
    \includegraphics[width=\linewidth]{somefig_2}
\end{subfigure}
\begin{subfigure}{0.24\linewidth}
    \includegraphics[width=\linewidth]{somefig_3}
\end{subfigure}
% Put a blank line here to divide into two rows
\begin{subfigure}{0.24\linewidth}
    \includegraphics[width=\linewidth]{somefig_4}
\end{subfigure}
\caption{Arranged sub-figures}\label{fig:1}
\end{figure}
\end{document}

enter image description here

As BambOo noted, you can squeeze them a little more if you don't need any horizontal space between the figures by addin %s after the \end{subfigure} and increasing the widths like

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

\begin{document}
\begin{figure}[htb]
\centering
\begin{subfigure}{0.25\linewidth}
    \includegraphics[width=\linewidth]{somefig_1}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
    \includegraphics[width=\linewidth]{somefig_2}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
    \includegraphics[width=\linewidth]{somefig_3}
\end{subfigure}%
\begin{subfigure}{0.25\linewidth}
    \includegraphics[width=\linewidth]{somefig_4}
\end{subfigure}%
\caption{Arranged sub-figures}\label{fig:1}
\end{figure}
\end{document}

enter image description here

Andrew Swann
  • 95,762
Christoph90
  • 1,020
  • Thanks! I haven't gotten the difference in this case based on your answer. Specifically, when I increase the size using linewidth the last two sub-figures exceed the margin!. How can I scale the the whole picture? – Abdulhameed Apr 09 '18 at 08:17
  • Make sure you really use width=\linewidth and for the subfigure 0.24\linewidth. It should then fit no matter what sizes your actual pictures have. – Christoph90 Apr 09 '18 at 08:24
  • To complete @christop90's answer add a % after each \end{subfigure}. This way you can use 0.25\linewidth for the subfigure width. – BambOo Apr 09 '18 at 08:25
  • Of course, when you increase width=\linewidth' in the\includegraphics`command, it won't fit, since there's simply no more space available. – Christoph90 Apr 09 '18 at 08:26
  • You're welcome, it's a little trick I was taught not so long ago ^^ – BambOo Apr 09 '18 at 08:43
  • My question is, can I specify it that I want the sub-figures to be in two rows and two columns for example? , since increasing the \linewidth could cause the picture to exceed the margin. Otherwise, for the sub-figures in Christoph90's answer can they be scaled? Then, I used \subfloat[ ] in the MWE, since it gives me the best output – Abdulhameed Apr 09 '18 at 08:54
  • 1
    just add blank line between pair of subfigures. – Zarko Apr 09 '18 at 08:55
  • Using a blank line between pair of subfigures breaks it into rows and columns! Thanks everyone! – Abdulhameed Apr 09 '18 at 09:17
3

As an alternative you can arrange all subfigures with a tcbraster from tcolorbox. A tcbraster is used to distribute a list of boxes on an array structure with a fixed number of columns. By default, the raster is \linewidth wide and it can break between pages if boxes don't fit in one page. Although this option is not interesting for you because the figure cannot be broken.

Width of boxes is automatically adjusted according the number of columns and the items to be arranges are automatically distributed along needed rows.

The code shows an example with 3 or 4 columns. Command \mysubfig has been defined to save some typing.

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{subfig}

\newcommand{\mysubfig}[3][width=\linewidth]{
    \tcbitem\subfloat[#2]{\includegraphics[#1]{#3}}}

\begin{document}
\begin{figure}
\begin{tcbitemize}[raster equal height=rows,
raster columns=3, raster halign=center,
raster every box/.style=blankest]
\mysubfig{Test}{example-image-a}
\mysubfig{Test}{example-image-b}
\mysubfig{Test}{example-image-c}
\mysubfig{Test}{example-image}
\mysubfig{Test}{example-image-a}
\mysubfig{Test}{example-image-b}
\mysubfig{Test}{example-image-c}
\mysubfig{Test}{example-image}
\mysubfig{Test}{example-image-a}
\mysubfig{Test}{example-image-b}
\mysubfig{Test}{example-image-c}
\end{tcbitemize}
\caption{A bunch of subfigures composed with tcbraster}
\end{figure}

\begin{figure}
\begin{tcbitemize}[raster equal height=rows,
raster columns=4, raster halign=center,
raster every box/.style=blankest]
\mysubfig{Test}{example-image-a}
\mysubfig{Test}{example-image-b}
\mysubfig{Test}{example-image-c}
\mysubfig{Test}{example-image}
\mysubfig{Test}{example-image-a}
\mysubfig{Test}{example-image-b}
\mysubfig{Test}{example-image-c}
\mysubfig{Test}{example-image}
\mysubfig{Test}{example-image-a}
\mysubfig{Test}{example-image-b}
\mysubfig{Test}{example-image-c}
\end{tcbitemize}
\caption{A bunch of subfigures composed with tcbraster}
\end{figure}
\end{document}

enter image description hereenter image description here

Ignasi
  • 136,588