The subcaption package
Places several images or tables in one float, each with its own caption (a), (b), through the environments subfigure and subtable.
Loading the package
\usepackage{subcaption}A subfigure is a box of a fixed width inside a figure, comparable to a minipage. Inside it, \caption prints the letter (a), (b), (c) instead of a figure number, and \label makes the part referable: \ref prints "1a".
Boxes that stand next to each other in the source stand next to each other on the page as long as their widths fit into the line. Inside a subfigure, \linewidth is the width of that box, so width=\linewidth fills it.
The package belongs to caption and loads it. Sub-captions are formatted with \captionsetup[sub]{...}.
Main commands
\begin{subfigure}[b]{0.45\textwidth} ... \end{subfigure} | Part of a figure with 45 % of the text width; [b] aligns the parts at their bottom edge. |
\caption{First image} | Inside a subfigure: caption with the letter (a). |
\hfill | Between two parts: pushes them to the left and right edge. |
\subref{fig:b} | Reference that prints the letter only, "b" instead of "1b". |
\subcaptionbox{Caption\label{fig:c}}{content} | Short form without an environment: a box as wide as its content with a caption below. |
\begin{subtable}{0.45\textwidth} ... \end{subtable} | The same for tables inside a table float. |
Complete example
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\begin{document}
\begin{figure}[ht]
\centering
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics[width=\linewidth]{image-a.png}
\caption{First image}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.45\textwidth}
\centering
\includegraphics[width=\linewidth]{image-b.png}
\caption{Second image}
\end{subfigure}
\caption{Two images side by side}
\end{figure}
\end{document}Compiled with pdfLaTeX from TeX Live 2023.
Pitfalls
- A blank line between two
subfigureenvironments starts a new paragraph, and the second image moves below the first. Put\hfillor%there instead. - The widths and the spaces between the boxes have to add up to less than
\textwidth. Two boxes of0.5\textwidthwith a space between them no longer fit into one line. - Do not load the older packages
subfigorsubfigurein the same document. Withsubfigloaded, thesubfigureenvironment is undefined.
Related pages
- How-to Subfigures in LaTeX
- How-to Place images side by side in LaTeX
- Package
caption - Package
graphicx - Error Environment align undefined