Tx · LaTeX & TikZ

How to Make a TikZ Picture a Subfigure

Published By the 9to5Science editorial team

At a glance

  1. 1. Load the packages

    Use the subcaption package for subfigure environments, and TikZ for the drawings.

  2. 2. Wrap each drawing

    Put one tikzpicture inside each subfigure environment, setting each subfigure's width.

  3. 3. Add captions and labels

    Give each panel its own caption and label, then caption and label the overall figure.

To make a TikZ drawing one panel in a multi-panel figure, put its tikzpicture environment inside a subfigure environment. The subcaption package provides that environment, along with a caption and label for each panel.

The example below creates two TikZ drawings side by side. You can use the same structure for PGFPlots: place each \begin{axis}...\end{axis} inside a tikzpicture within its own subfigure.

A complete example

\documentclass{article}
\usepackage{tikz}
\usepackage{subcaption}

\begin{document}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[t]{0.48\linewidth}
    \centering
    \begin{tikzpicture}
      \draw[thick, blue] (0,0) circle (1cm);
      \node at (0,0) {A};
    \end{tikzpicture}
    \caption{A circular diagram}
    \label{fig:circle}
  \end{subfigure}
  \hfill
  \begin{subfigure}[t]{0.48\linewidth}
    \centering
    \begin{tikzpicture}
      \draw[thick, red] (0,0) rectangle (2,1.5);
      \node at (1,0.75) {B};
    \end{tikzpicture}
    \caption{A rectangular diagram}
    \label{fig:rectangle}
  \end{subfigure}
  \caption{Two TikZ diagrams}
  \label{fig:diagrams}
\end{figure}

Figure~\ref{fig:diagrams} contains panels~\subref{fig:circle}
and~\subref{fig:rectangle}.

\end{document}

Compile the document at least twice so LaTeX can resolve the references. The package list is minimal: tikz provides tikzpicture, and subcaption provides the subfigure environments and their captions.

How the layout works

The outer figure environment treats both drawings as one numbered figure and allows LaTeX to position the whole group. Each subfigure is a box with a specified width. Here, each is 0.48\linewidth wide, leaving a small gap for \hfill. The two widths must fit within the available line width, including any spacing between the boxes; otherwise, the second panel may wrap onto another line.

The optional [t] aligns the tops of the subfigure boxes. Use [b] for bottom alignment or [c] for vertical centering. Alignment can be noticeable when panels have different heights or caption lengths.

Each panel has its own \caption and \label. Put the label after the caption so it records the subfigure number. The outer figure also has a caption and label, which identify the complete group. \ref{fig:diagrams} refers to the figure number; \subref{fig:circle} refers to the panel marker, usually a letter in parentheses. If you want a full reference such as “Figure 2(a),” write Figure~\ref{fig:diagrams}\subref{fig:circle}.

Using PGFPlots

A PGFPlots axis belongs inside a TikZ picture, so the nesting is the same. For each panel, use this structure:

\begin{subfigure}[t]{0.48\linewidth}
  \centering
  \begin{tikzpicture}
    \begin{axis}
      % Plot commands go here
    \end{axis}
  \end{tikzpicture}
  \caption{A plot}
  \label{fig:plot}
\end{subfigure}

Load pgfplots as well as subcaption and TikZ when using an axis environment. You can adjust the axis dimensions with PGFPlots options such as width=... and height=... so the plot fits its subfigure box. The subfigure width controls the surrounding panel, but it does not automatically scale an oversized TikZ drawing to fit.

Common mistakes

  • Using subfigure without loading subcaption: The environment is not built into LaTeX. Load \usepackage{subcaption} in the preamble.
  • Loading conflicting subfigure packages: Do not load the obsolete subfigure package alongside subcaption. They both try to manage subcaptions and can conflict.
  • Forgetting the outer figure: A subfigure is intended to be used within a figure-like float. The outer environment supplies the main figure number and caption.
  • Putting a label before its caption: A label captures the most recently established counter value. Put each label immediately after the caption it should refer to.
  • Making the panels too wide: Two panels of width 0.5\linewidth leave no room for the space between them. Use slightly smaller widths, such as 0.48\linewidth, or put the panels on separate rows.
  • Expecting the drawing to scale with the panel: A TikZ picture may extend beyond its subfigure’s width. Reduce its dimensions or use an appropriate scaling option; setting the subfigure width alone does not resize the picture.

References

  1. The subcaption package
  2. The TikZ and PGF Packages Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.