Tx · LaTeX & TikZ

Undefined Control Sequence \subfloat When Using subcaption

Published By the 9to5Science editorial team

At a glance

  1. 1. Identify the command

    The subcaption package does not define \subfloat; that command belongs to a different package.

  2. 2. Choose a package

    Use subcaption with subfigure environments, or use subfig if you need to keep existing \subfloat commands.

  3. 3. Avoid conflicts

    Do not load subcaption and subfig together; convert the figure code to match the package you choose.

  4. 4. Compile and check

    Build the document again, then check that each subfigure caption and label appears as intended.

The error Undefined control sequence \subfloat means LaTeX encountered \subfloat but no loaded package defined it. The common cause is using \subfloat in a document that loads subcaption: subcaption does not provide that command.

Use the subfigure environment provided by subcaption, or keep the \subfloat syntax and use the separate subfig package. Choose one approach; do not load both packages in the same document.

With subcaption, put each panel inside a subfigure environment. Give each panel its own \caption; its lettered subcaption is then generated automatically. The enclosing figure has a separate caption for the entire group.

Here is a complete example. The mwe package supplies the sample images used below.

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

\begin{document}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{First panel}
    \label{fig:first-panel}
  \end{subfigure}\hfill
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Second panel}
    \label{fig:second-panel}
  \end{subfigure}
  \caption{A figure made of two panels.}
  \label{fig:two-panels}
\end{figure}

See Figure~\ref{fig:two-panels}; its panels are
Figures~\ref{fig:first-panel} and \ref{fig:second-panel}.

\end{document}

\usepackage{subcaption} enables the subfigure environment, while graphicx provides \includegraphics. The width 0.48\textwidth reserves room for two panels on one line; within each panel, \linewidth refers to that panel’s width. Replace example-image-a and example-image-b with your image filenames.

Put each panel’s \label after its \caption, so the label records the subfigure number rather than an earlier counter value. Similarly, put the main figure’s label after the main caption. The subcaptions will typically be numbered (a), (b), and so on, depending on the document’s caption settings.

If you want to keep \subfloat

If an existing document uses \subfloat, load subfig rather than subcaption:

\usepackage{graphicx}
\usepackage{subfig}

Then the package’s \subfloat command can be used in a figure, for example:

\begin{figure}[htbp]
  \centering
  \subfloat[First panel\label{fig:first-panel}]{%
    \includegraphics[width=0.45\textwidth]{example-image-a}}
  \hfill
  \subfloat[Second panel\label{fig:second-panel}]{%
    \includegraphics[width=0.45\textwidth]{example-image-b}}
  \caption{A figure made of two panels.}
  \label{fig:two-panels}
\end{figure}

This alternative is useful when you want to preserve the existing command syntax. If you choose it, remove \usepackage{subcaption}. Conversely, if you want to use subcaption, convert the \subfloat calls to subfigure environments as in the first example.

Common mistakes

  • Loading both packages: Avoid \usepackage{subcaption} and \usepackage{subfig} in the same document. Both affect subcaptions, and using them together can cause conflicts or inconsistent formatting.
  • Using the old subfigure package: The package named subfigure is obsolete. For new documents, prefer subcaption; use subfig when you specifically need the \subfloat command.
  • Putting \caption in the wrong place: Each subfigure needs its own caption if you want a letter and a reference for that panel. The outer figure caption describes the complete group.
  • Forgetting to recompile references: LaTeX may need more than one compilation pass to update figure numbers and references. An undefined-control-sequence error for \subfloat, however, is a package or command mismatch—not a stale reference.
  • Checking only the preamble: If the correct package is loaded and the error remains, check that the command is spelled exactly \subfloat and that it is not being used in a different file or document class with conflicting caption setup.

In short, subcaption and \subfloat are not a matching package-command pair. Use subcaption with subfigure, or use subfig with \subfloat.

References

  1. The subcaption package
  2. The subfig package

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