Tx · LaTeX & TikZ

How to Fix the “Subfigure Undefined” Error in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Check the syntax

    Determine whether your document uses a subfigure environment or a subfigure command; they belong to different package interfaces.

  2. 2. Load the right package

    For the modern subfigure environment, load subcaption in the preamble.

  3. 3. Use matching commands

    Pair subcaption with its environment, or use subfig with its \subfloat command.

  4. 4. Check for conflicts

    Remove obsolete or competing subfigure packages and compile the complete document again.

The “undefined control sequence” error usually means LaTeX has encountered a command that its loaded packages do not define. An undefined subfigure environment is usually reported separately as Environment subfigure undefined. In either case, the fix depends on the syntax in your source: subfigure can refer to an environment used by the modern subcaption package, or to a command used by an older package.

For new documents, use subcaption and its subfigure environment. The complete example below compiles and places two images side by side.

Use subcaption for the subfigure environment

Load subcaption in the preamble, before \begin{document}. The package defines the environment subfigure, which takes a width as an argument. Each subfigure can have its own caption and label.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{mwe} % provides example-image-a and example-image-b

\begin{document}

\begin{figure}[htbp]
  \centering
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-a}
    \caption{First image}
    \label{fig:first}
  \end{subfigure}\hfill
  \begin{subfigure}[b]{0.48\textwidth}
    \centering
    \includegraphics[width=\linewidth]{example-image-b}
    \caption{Second image}
    \label{fig:second}
  \end{subfigure}
  \caption{Two subfigures in one figure.}
  \label{fig:pair}
\end{figure}

\end{document}

The graphicx package provides \includegraphics; subcaption provides the subfigure environment and subcaptions. The mwe package supplies the sample image files in this example, so you can test it without creating image files. In your document, replace example-image-a and example-image-b with your own image filenames.

The optional [b] aligns the subfigures along their bottoms. Each {0.48\textwidth} sets the width of a subfigure, and \hfill inserts flexible space between them. Inside a subfigure, \linewidth is its width, so width=\linewidth scales the image to fit.

If your source uses \subfigure

Check the exact error line. If it points to a command such as \subfigure[Caption]{...}, your code uses a different interface from the subfigure environment shown above. The modern subcaption package does not provide the old \subfigure[Caption]{...} command interface; its subfigure name is an environment with different arguments.

The subfig package provides the \subfloat command instead. For example, its syntax is \subfloat[Caption]{...}. Older documents may use the obsolete subfigure package, which also used a \subfigure command. For new work, prefer subcaption; if you keep an older document, use the syntax documented for the package it was written with.

Do not load subfigure, subfig, and subcaption together to try to make every command available. Their caption functionality can conflict. Choose one package and use its matching syntax throughout the document.

Common mistakes to check

  • The package is missing or loaded too late. Put \usepackage{subcaption} in the preamble, not inside the figure or after \begin{document}.
  • The command and package do not match. With subcaption, use \begin{subfigure} ... \end{subfigure}. With subfig, use \subfloat. Loading one does not necessarily define the other package’s interface.
  • An old package is still loaded. Remove \usepackage{subfigure} when moving to subcaption. Also check the document class or template instructions if it loads caption-related packages for you.
  • The image file cannot be found. That produces a separate graphics-file error, not an undefined-control-sequence error. Check the filename, extension, and relative path after resolving the package issue.
  • Only part of the document was compiled. Test the complete file, including its preamble. A code fragment may rely on a package loaded by a larger project.

If the error persists, reduce the document to the example structure, then add your class, packages, and figure content back one at a time. This helps identify whether the cause is a missing package, incompatible syntax, or a template-specific restriction.

References

  1. The subcaption package (CTAN)
  2. The subfig package (CTAN)

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