Subfigures in LaTeX: Side-by-Side Figures with subcaption

Quick answer

  • Package: \usepackage{subcaption} with graphicx.
  • One panel: \begin{subfigure}[b]{0.45\linewidth} … \end{subfigure} inside a figure, with its own \caption and \label.
  • Side by side: widths that sum to less than \linewidth, separated by \hfill.
  • References: \ref{fig:a} prints 1a, \subref{fig:a} prints (a).
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.45\linewidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{Before}
    \label{fig:a}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\linewidth}
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{After}
    \label{fig:b}
  \end{subfigure}
  \caption{Two subfigures side by side.}
  \label{fig:both}
\end{figure}

Figure~\ref{fig:both} has two panels: \ref{fig:a} and \ref{fig:b}.
\end{document}

Subfigures are panels inside one figure, each with its own small caption, sharing one figure number and a main caption. The subcaption package is the modern way to make them in LaTeX. This guide covers two figures side by side, the options that control widths and alignment, a 2×2 grid, referencing single panels, customizing the subcaptions, the older subfig package and the errors that come up most. Every snippet compiles as shown; the example-image placeholders come with TeX Live, so replace them with your own file names.

Subfigures in LaTeX with subcaption

The subfigure environment works like a minipage: it takes a width, and everything inside, usually an \includegraphics, a \caption and a \label, is set in a box of that width. Its syntax is:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}
  \centering
  % \begin{subfigure}[position]{width}
  \begin{subfigure}[t]{0.3\linewidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{First}
  \end{subfigure}
  \begin{subfigure}[t]{0.3\linewidth}
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Second}
  \end{subfigure}
  \begin{subfigure}[t]{0.3\linewidth}
    \includegraphics[width=\linewidth]{example-image-c}
    \caption{Third}
  \end{subfigure}
  \caption{Three panels in one row.}
\end{figure}
\end{document}
PartMeaning
{0.45\linewidth}Width of the panel; the image inside should use width=\linewidth to fill it
[b]Align panels at their bottoms, so captions line up when images have different heights
[t]Align at the top line; with images, this aligns their bottom edges too, because an image sits on the baseline
[c]Align at the vertical centers (the default)
\caption insideSubcaption labelled (a), (b), …
\caption outsideThe main caption with the figure number

Side-by-Side Figures: Widths and Spacing

Panels go next to each other as long as their widths, plus any space between them, fit in \linewidth. Three ways to control the gap:

  • \hfill pushes the panels to the margins and puts all leftover space between them.
  • \hspace{1em} gives a fixed gap; use it with \centering.
  • A % at the end of a line removes the space a line break would add, so two panels of 0.5\linewidth fit exactly.
\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.4\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{A wide image}
    \label{fig:wide}
  \end{subfigure}%
  \hspace{2em}%
  \begin{subfigure}[b]{0.25\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-golden-upright}
    \caption{A tall image}
    \label{fig:tall}
  \end{subfigure}
  \caption{Panels of different sizes, aligned at the bottom.}
\end{figure}
\end{document}

Common widths: 0.48\linewidth for two panels, 0.31\linewidth for three and 0.23\linewidth for four. Inside a two-column document, \linewidth is the column width; use figure* to span both columns.

Subfigure Grid: 2×2 Example

A grid is several rows of side-by-side panels. A blank line (or \par) ends a row, and \medskip or \vspace{...} adds space between rows:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.48\linewidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{Top left}
    \label{fig:tl}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.48\linewidth}
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Top right}
    \label{fig:tr}
  \end{subfigure}

  \medskip
  \begin{subfigure}[b]{0.48\linewidth}
    \includegraphics[width=\linewidth]{example-image-c}
    \caption{Bottom left}
    \label{fig:bl}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.48\linewidth}
    \includegraphics[width=\linewidth]{example-image}
    \caption{Bottom right}
    \label{fig:br}
  \end{subfigure}
  \caption{A 2$\times$2 grid of subfigures.}
  \label{fig:grid}
\end{figure}

Panels \subref{fig:tl} and \subref{fig:br} of Figure~\ref{fig:grid}.
\end{document}

A grid of four large images may be taller than the page allows, in which case the figure moves to the end of the document. Reduce the widths or use \begin{figure}[p] for a page of its own. Placement options are explained in the tables guide, which uses the same float rules.

Lay out figures without trial and error

Upload your images to Octree and ask the AI for a 2×2 grid or two panels side by side. It writes the subcaption code, sizes the panels and shows the PDF next to it. Sign up free.

Open Octree →

Referencing Subfigures with \ref and \subref

Put each \label after the \caption it belongs to. A label inside a subfigure refers to the panel, a label after the main caption to the whole figure:

CommandOutputUse
\ref{fig:both}1The whole figure
\ref{fig:a}1aA panel, in running text
\subref{fig:a}(a)A panel, when the figure number is already clear

Many journals prefer “Figure 1(a)”. Changing the subfigure counter format gives that from \ref, and switching the caption label format to simple stops the parentheses from doubling in the subcaptions:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\renewcommand\thesubfigure{(\alph{subfigure})}
\captionsetup[sub]{labelformat=simple}

\begin{document}
\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.45\linewidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{Left}
    \label{fig:left}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\linewidth}
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Right}
    \label{fig:right}
  \end{subfigure}
  \caption{Panels referenced as 1(a) and 1(b).}
\end{figure}

See Figure~\ref{fig:left} and Figure~\ref{fig:right}.
\end{document}

With hyperref loaded, the references become links that jump to the panel; see hyperlinks in LaTeX.

Customizing Subcaptions

subcaption is part of the caption bundle, so \captionsetup controls the subcaptions with the sub (or subfigure) key:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\captionsetup[sub]{font=footnotesize, labelfont=bf, justification=centering}
\renewcommand\thesubfigure{\Alph{subfigure}}   % (A), (B) instead of (a), (b)

\begin{document}
\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.45\linewidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{Bold labels, small text}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.45\linewidth}
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{}   % label only, no text
  \end{subfigure}
  \caption{Customized subcaptions.}
\end{figure}
\end{document}

An empty \caption{} prints just the label, such as (B). Leave out \caption entirely for a panel without a label.

Side-by-Side Figures with Separate Numbers (minipage)

When two images are separate figures that should simply sit next to each other, with “Figure 1” and “Figure 2” rather than (a) and (b), use two minipage environments in one figure. No extra package is needed:

\documentclass{article}
\usepackage{graphicx}

\begin{document}
\begin{figure}[htbp]
  \begin{minipage}[b]{0.48\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{The first figure.}
    \label{fig:first}
  \end{minipage}
  \hfill
  \begin{minipage}[b]{0.48\linewidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{The second figure.}
    \label{fig:second}
  \end{minipage}
\end{figure}
\end{document}

The same trick puts a figure next to a table: use \captionof from the caption package inside the minipage, for example \captionof{table}{...}, to give each its own counter.

The Legacy subfig Package and \subfloat

Older templates use subfig and its \subfloat command, which takes the caption as an optional argument. It still works, but subcaption is the maintained choice for new documents, and the two cannot be loaded together. The even older subfigure package is obsolete.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}   % legacy: do not combine with subcaption

\begin{document}
\begin{figure}[htbp]
  \centering
  \subfloat[Left panel\label{fig:sf-a}]{%
    \includegraphics[width=0.45\linewidth]{example-image-a}}
  \hfill
  \subfloat[Right panel\label{fig:sf-b}]{%
    \includegraphics[width=0.45\linewidth]{example-image-b}}
  \caption{Two panels with subfig.}
\end{figure}
\end{document}

subcaption has a \subcaptionbox command that takes the same two pieces, caption and content, so porting old \subfloat code is easy: \subcaptionbox{Left panel\label{fig:a}}{\includegraphics[width=0.45\linewidth]{img}}.

Common Errors and Fixes

Message or symptomCauseFix
LaTeX Error: Environment subfigure undefinedsubcaption is not loaded\usepackage{subcaption} in the preamble
Package subcaption Error: This package can't be used in cooperation with the subfig package (or subfigure package)subcaption together with subfig or subfigure, often from a templateKeep one package; convert \subfloat to \subcaptionbox
Panels stacked vertically instead of side by sideWidths plus spaces exceed \linewidth, or a blank line between panelsSmaller widths (0.48\linewidth), \hfill between panels, % at line ends, no blank lines inside a row
Overfull \hbox, image sticks out of its panelImage wider than the subfigure, e.g. width=\textwidth inside a 0.45\linewidth panelUse width=\linewidth inside the subfigure; it refers to the panel width
\ref prints a wrong number (the section or an earlier figure), not the panel\label before the subfigure's \captionPut \label after \caption inside the subfigure
Captions not aligned when images differ in heightDefault center alignment\begin{subfigure}[b]{...} on every panel
File `image.png' not foundImage not uploaded or name differs in case or extensionCheck the file name, or set \graphicspath{{figures/}}

Frequently Asked Questions

How do I make subfigures in LaTeX?

Load \usepackage{subcaption} (and graphicx), then put one subfigure environment per panel inside a figure: \begin{subfigure}[b]{0.45\linewidth} ... \end{subfigure}. Each subfigure gets its own \caption and \label, and the figure gets a main \caption. Panels are labelled (a), (b) and so on automatically.

How do I put two figures side by side in LaTeX?

Place two subfigure environments in the same figure with widths that add up to less than the line width, for example 0.48\linewidth each, separated by \hfill. If both images should keep their own figure numbers instead of (a) and (b), use two minipage environments with a \caption in each.

How do I reference a subfigure?

Put \label after the subfigure’s \caption, then use \ref{fig:a}, which prints 1a, or \subref{fig:a}, which prints only (a). To get 1(a) from \ref, add \renewcommand\thesubfigure{(\alph{subfigure})} and \captionsetup[sub]{labelformat=simple} to the preamble.

Should I use subfigure, subfig or subcaption?

Use subcaption. The subfigure package is obsolete, and subfig has not been updated since 2005. subcaption is maintained together with the caption package. The packages cannot be combined: loading subcaption together with subfig or subfigure stops with an error.

Why are my subfigures stacked instead of side by side?

The widths plus the space between them are more than \linewidth, or there is a blank line between the subfigures, which starts a new paragraph. Reduce the widths (0.48\linewidth for two, 0.31\linewidth for three), remove blank lines, and end lines with % or separate the panels with \hfill.

How do I make a 2×2 grid of subfigures?

Put four subfigure environments of 0.48\linewidth in one figure, with \hfill between the two panels of each row and a blank line or \par\medskip between the rows. The blank line ends the first row, so the third and fourth panels start a new one.

Next Steps

Free LaTeX Tools

Arrange figures with AI

Tell Octree's AI how the panels should be arranged and it writes the subcaption code, fixes overflowing widths and broken references, with the PDF compiled next to your code.

Try Octree Free →