How-to

How to Place Figures Side by Side in LaTeX

The fix

Use the subcaption package (the modern replacement for the old subfigure and subfig packages). Inside one figure environment, add several subfigure environments set to a fraction of \textwidth, each with its own \includegraphics, \caption, and \label. They are lettered (a), (b), (c) automatically, and the figure keeps one main caption.

Not sure it worked on your document? Paste it into LetX and compile it, free and in the browser.

Why it happens

A figure environment is one float with one number. subcaption lets you put several boxes inside it, each a fixed fraction of the line width with its own caption and label. LaTeX places those boxes the way it places words on a line, so their widths have to add up to less than \linewidth, and a blank line between two of them starts a new row.

Example

\usepackage{graphicx}
\usepackage{subcaption}
% in the document (example-image-a ships with TeX Live; use your own files):
\begin{figure}
  \centering
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{First}\label{fig:a}
  \end{subfigure}\hfill
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Second}\label{fig:b}
  \end{subfigure}
  \caption{Two results side by side}\label{fig:both}
\end{figure}

Widths, spacing and alignment

The widths, plus any space between the subfigures, must fit in \linewidth. Two at 0.48\textwidth with \hfill between them fit. Two at 0.5\textwidth do not, because the line break in your source between \end{subfigure} and the next \begin{subfigure} counts as a space: LaTeX reports an "Overfull \hbox" a few points wide, and the pair sticks into the margin. End the line with % or use smaller widths. Widths that add up to well over \linewidth do the same, with a bigger number.

The optional argument sets the vertical alignment when the images differ in height: [b] lines up the bottoms, so the captions sit level, [t] the tops, and the default [c] the centres. \centering inside each subfigure centres an image narrower than its box.

A 2x2 grid

For two rows, leave a blank line after the second subfigure, then add space between the rows with \medskip or \vspace{1em}. The letters continue across the rows: (a), (b), (c), (d).

\usepackage{graphicx}
\usepackage{subcaption}
% in the document:
\begin{figure}
  \centering
  \begin{subfigure}{0.45\textwidth}
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{Top left}
  \end{subfigure}\hfill
  \begin{subfigure}{0.45\textwidth}
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Top right}
  \end{subfigure}

  \medskip
  \begin{subfigure}{0.45\textwidth}
    \includegraphics[width=\linewidth]{example-image-c}
    \caption{Bottom left}
  \end{subfigure}\hfill
  \begin{subfigure}{0.45\textwidth}
    \includegraphics[width=\linewidth]{example-image}
    \caption{Bottom right}
  \end{subfigure}
  \caption{Four panels}
\end{figure}

Two separate figures side by side

If the images are separate figures, numbered Figure 1 and Figure 2 rather than 1a and 1b, put two minipage environments inside one figure, each with an ordinary \caption. No extra package is needed.

\usepackage{graphicx}
% in the document:
\begin{figure}
  \centering
  \begin{minipage}{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{Before}\label{fig:before}
  \end{minipage}\hfill
  \begin{minipage}{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{After}\label{fig:after}
  \end{minipage}
\end{figure}

References and the errors you will meet

\ref{fig:a} prints 1a, and \subref{fig:a} prints only the letter, a, for sentences such as "panels a and b". With cleveref, \cref{fig:a} prints "fig. 1a" and \Cref prints "Figure 1a".

"Environment subfigure undefined": subcaption is not loaded.

"Missing number, treated as zero" followed by "Illegal unit of measure (pt inserted)" on a \begin{subfigure} line: the width argument is missing, or the old subfigure package is loaded as well and has taken over the name. Load subcaption alone; subfig and subfigure are older packages that conflict with it.

Subfigures stacked one above the other: there is a blank line between them.

Frequently asked questions

subcaption, subfig, or subfigure?

Use subcaption. It is current and works with the caption package. subfigure is obsolete and subfig is no longer developed; never load either alongside subcaption.

How do I reference a subfigure?

Give each subfigure a \label and reference it with \ref{fig:a}, which prints "1a". With cleveref, \Cref{fig:a} prints "Figure 1a".

Why are my subfigures stacked vertically?

A blank line between two subfigures starts a new row. Remove it, and keep the widths plus the gap under \linewidth, for example two at 0.48\textwidth with \hfill between them.

How do I make a 2x2 grid of figures?

Use four subfigure environments at about 0.45\textwidth, with a blank line and \medskip after the second one to start the next row.

How do I reference only the letter of a subfigure?

Use \subref{fig:a}, which prints the subfigure letter without the figure number.

How do I give side-by-side images their own figure numbers?

Put two minipage environments in one figure, each with its own \caption. They are numbered as separate figures.

Related LaTeX fixes

Browse all LaTeX problems & fixes, or start with the LaTeX guide.

Learn the topic properly

Try the fix in LetX

Open the editor, paste your code, and compile in seconds to see the error clear. Free, in your browser.

Open LetX Free