Tx · LaTeX & TikZ

How to Fix an “Unrecognized Command” Error in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Find the command

    Read the first relevant error in the compiler log and identify the command named after “Undefined control sequence.”

  2. 2. Check spelling

    Compare the command with its exact LaTeX spelling, including the backslash, capitalization, and braces.

  3. 3. Load the package

    Add the package that defines the command, such as graphicx for includegraphics or caption for captionsetup.

  4. 4. Check the context

    Use caption inside a figure or table, and use captionof only when its defining package is loaded.

An “unrecognized command” message is usually LaTeX’s way of saying that it does not know a command in your document. In a document with figures and captions, the cause is often a missing package, a spelling mistake, or a caption command used in the wrong place.

The wording depends on the editor or compiler. In a standard LaTeX log, look for Undefined control sequence; the command that follows is the one to investigate. Fix that command first, then compile again, since later errors may only be consequences of the first one.

Identify the command LaTeX cannot find

Read the log around the first error rather than relying only on a short pop-up message. For example:

! Undefined control sequence.
l.12 \includegraphics

This points to \includegraphics. The command may be misspelled, or the package that defines it may be missing. If the log points to \captionsetup, the likely issue is that the caption package has not been loaded.

Check the command character by character. LaTeX distinguishes uppercase and lowercase letters, and commands need a backslash: \caption is a command, while caption is ordinary text. Also check that braces and optional arguments are balanced.

Load the package for graphics or caption commands

The graphicx package defines \includegraphics. The caption package provides commands such as \captionsetup and \captionof. A basic figure caption using \caption does not require the caption package, but the \caption command belongs inside a figure or table float.

Here is a complete example that compiles without an external image file. The framed rule stands in for an image; replace it with an \includegraphics line when you have an image file.

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}

\begin{document}

\begin{figure}[htbp]
  \centering
  \fbox{\rule{0pt}{25mm}\rule{50mm}{0pt}}
  % Replace the framed placeholder above with, for example:
  % \includegraphics[width=0.6\linewidth]{my-image}
  \caption{A sample figure caption.}
  \label{fig:sample}
\end{figure}

\end{document}

The packages are in the preamble, between \documentclass and \begin{document}. The graphicx line enables \includegraphics; the caption line enables extra caption commands and settings. The standard \caption inside the figure creates the caption and figure number. The \label records the figure number for references such as Figure~\ref{fig:sample}.

If the caption is outside a figure

A \caption command used directly in ordinary text can produce a caption-related error because it is not inside a figure or table. If you intentionally need a caption outside a float, load caption and use \captionof:

\usepackage{caption} % in the preamble

\captionof{figure}{A caption outside a figure environment.}

This is a separate issue from an undefined-command error: loading the package makes \captionof recognizable, while choosing the right command and context makes the caption valid.

Common mistakes

  • Loading the wrong package or loading none. \includegraphics comes from graphicx; \captionsetup and \captionof come from caption.
  • Using a command from an online example without its setup. Copy the package line as well as the command, and put the package line in the preamble.
  • Using \caption outside a float. Put it inside figure or table, or use \captionof with the caption package when appropriate.
  • Treating every caption problem as an undefined command. A missing image file, an unmatched brace, or an invalid float structure produces a different error. Use the specific message in the log to distinguish these cases.
  • Using commands unsupported by the chosen format or engine. If the command comes from a class file, template, or specialized package, confirm that your document is using the required class, package, and compiler.

If the message comes from a visual editor rather than the LaTeX compiler, check whether the editor understands the command you entered. Some editors flag custom commands even when LaTeX will compile them, especially if the required package is loaded only in the document preamble. When in doubt, compile and inspect the log for the first actual LaTeX error.

References

  1. The LaTeX Graphics Companion, 2nd edition, Michel Goossens et al.
  2. The caption package documentation

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