Subfigures in LaTeX (subcaption)

The subcaption package provides the subfigure environment. Each subfigure inside a figure gets its own caption with a letter, (a), (b), and the figure keeps one caption for all of them.

Preamble

The snippets on this page need these lines before \begin{document}:

\usepackage{graphicx}
\usepackage{subcaption}

Put two subfigures side by side

The argument of subfigure is its width. Inside, \linewidth is that width, so width=\linewidth fills the subfigure. \hfill pushes the two boxes to the left and right edge. There must be no blank line between them: a blank line starts a new row.

\begin{figure}
\centering
\begin{subfigure}{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{image-a.png}
\caption{Before}
\label{fig:before}
\end{subfigure}
\hfill
\begin{subfigure}{0.48\textwidth}
\centering
\includegraphics[width=\linewidth]{image-b.png}
\caption{After}
\label{fig:after}
\end{subfigure}
\caption{The sample before and after the treatment}
\label{fig:treatment}
\end{figure}
Result
The compiled example

Refer to a subfigure

A \label after the caption of a subfigure makes \ref print the figure number with the letter, for example 1a. \subref prints the letter alone. The label after the main caption gives the number of the whole figure.

\begin{figure}
\centering
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-a.png}
\caption{Before}
\label{fig:a}
\end{subfigure}
\hfill
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-b.png}
\caption{After}
\label{fig:b}
\end{subfigure}
\caption{The sample}
\label{fig:sample}
\end{figure}

Figure~\ref{fig:a} shows the sample before the treatment.
Parts \subref{fig:a} and \subref{fig:b} of Figure~\ref{fig:sample} use the same scale.
Result
The compiled example

Arrange four subfigures in a grid

A blank line ends a row. \medskip after it adds space between the rows.

\begin{figure}
\centering
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-a.png}
\caption{Spring}
\end{subfigure}
\hfill
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-b.png}
\caption{Summer}
\end{subfigure}

\medskip
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-b.png}
\caption{Autumn}
\end{subfigure}
\hfill
\begin{subfigure}{0.48\textwidth}
\includegraphics[width=\linewidth]{image-a.png}
\caption{Winter}
\end{subfigure}
\caption{The site in four seasons}
\end{figure}
Result
The compiled example

Line up images of different height

subfigure environments of different height are centered vertically against each other. \subcaptionbox{caption}[width]{content} lines up the bottom edges of the images and the first lines of the captions instead. The \label goes into the caption argument.

\begin{figure}
\centering
\subcaptionbox{Wide image\label{fig:wide}}[0.4\textwidth]{%
\includegraphics[width=0.4\textwidth]{image.png}}
\hfill
\subcaptionbox{Square image\label{fig:square}}[0.4\textwidth]{%
\includegraphics[width=0.4\textwidth]{logo.png}}
\caption{Two images with different proportions}
\end{figure}
Result
The compiled example

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages