How to Fix an Undefined Control Sequence in LaTeX
At a glance
- 1. Find the command
Read the error and locate the control sequence LaTeX does not recognize.
- 2. Check its spelling
Compare the command with its documented name, including capitalization and required braces.
- 3. Load what it needs
Add the package or definition that provides the command before using it.
- 4. Compile a small test
Reduce the document to a minimal example and compile again.
An “Undefined control sequence” error means LaTeX found a command—often something beginning with a backslash—that has no definition available at that point in the document. The error can occur in math mode or ordinary text; being inside $...$ does not, by itself, make a command undefined.
The quickest fix is to identify the command LaTeX reports, then check its spelling, package requirements, and definition. The examples below show how to diagnose a common case and verify the repair.
What the error message means
A control sequence is a LaTeX command such as \frac, \alpha, or \emph. LaTeX recognizes commands built into its format and commands provided by the document class, packages, or your own definitions. If it encounters a command that has not been defined, it reports an error like:
! Undefined control sequence.
l.7 The set is $\mathbb{R}$.
Here, the command to investigate is \mathbb. The line number is a useful clue, but it is not always the exact source of the problem: a macro may expand to an undefined command, or the error may appear slightly after the underlying mistake. Read the command shown after the error marker and inspect its surrounding source.
Diagnose the cause
Check these possibilities in order:
- Typo or capitalization: LaTeX commands are case-sensitive. For example,
\alphaand\Alphaare different commands, and\fraccis not the fraction command\frac. - Missing package: Some commands are defined only after you load a package. For instance,
\mathbbis provided byamsfontsand also made available byamssymb. - Missing personal definition: If the command is one you invented, confirm that its
\newcommandor other definition appears before its first use and is spelled the same way. - Wrong engine or format: A command may depend on a particular compiler, document class, or package version. Check the command’s documentation and the compiler selected by your editor.
- A command used in the wrong context: Some commands are valid only in math mode or only in text mode. This usually produces a different error, but context can still matter when diagnosing a complicated macro.
Worked example: \mathbb is undefined
Suppose a document contains $\mathbb{R}$, but the preamble does not load a package that defines \mathbb. Add amssymb to the preamble:
\documentclass{article}
\usepackage{amssymb}
\begin{document}
The real numbers form the set $\mathbb{R}$.
\end{document}
This is a complete minimal example. The \documentclass selects the article format; \usepackage{amssymb} loads the AMS symbols, including \mathbb; and the final line uses that command inside math mode. You can also load amsfonts for \mathbb specifically. If the error remains, check that the package line is in the preamble—between \documentclass and \begin{document}—and that you are compiling the file you edited.
For a typo, correct the command instead of adding a package. For example, change \fracc{1}{2} to \frac{1}{2}. Loading more packages will not define a misspelled command unless a package happens to provide that exact name.
A reliable debugging workflow
- Use the first relevant error. Later errors may be consequences of the first one.
- Locate the reported command. Search the source for it, including in included files and custom macros.
- Check its documentation. Confirm its exact spelling, required package, and mode of use.
- Make one change at a time. Fix the typo, load the package, or add the definition, then compile again.
- Reduce the document if needed. Keep the preamble and the few lines that reproduce the error. A minimal example helps distinguish a package issue from a conflict or unrelated source of trouble.
If the command appears inside a macro, temporarily simplify that macro or test the command directly. For example, if \R is defined as \mathbb{R}, then \R will fail if its definition is missing, and using a definition that depends on an unavailable \mathbb can also cause trouble.
Common mistakes
- Assuming every error in math mode is caused by math mode. An unknown command remains unknown whether it appears in text or mathematics.
- Loading a package after
\begin{document}. Put package declarations in the preamble. - Changing the compiler without checking why. An engine change is appropriate only if the command or package requires it.
- Treating the line number as definitive. Macro expansion and earlier syntax errors can make the reported location misleading.
- Adding packages at random. First identify the specific command and the package or definition that provides it; unnecessary packages can introduce conflicts.
When the command is custom, define it before its first use. For a simple new command, for example, \newcommand{\R}{\mathbb{R}} defines \R, but it still requires a definition for \mathbb such as the one supplied by amssymb.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.