[Tex/LaTex] How to specify number of rows and columns for subfigures

floatssubfloats

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}

Best Answer

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