How to Fix “Undefined Control Sequence” in XeLaTeX
At a glance
- 1. Find the first error
Read the log at the first undefined-control-sequence message and identify the command named there.
- 2. Check the command
Look for a spelling error, a missing backslash, or a command that is not defined in your document.
- 3. Load what defines it
Add the required package or macro definition in the preamble, before \begin{document}.
- 4. Compile and recheck
Compile with XeLaTeX again and address the earliest remaining error before later ones.
An Undefined control sequence error means XeLaTeX encountered a command that has not been defined. The command may be misspelled, supplied by a package you have not loaded, or defined only in a file or template that is missing. The error is usually in the document source—not a special limitation of XeLaTeX.
Start with the first relevant error in the compiler log. Later errors may be consequences of the first one. Find the command XeLaTeX names, then check its spelling and where it is supposed to be defined.
What the error is telling you
In LaTeX, a control sequence is a command beginning with a backslash, such as \section or \textcolor. If TeX reads a command that has no definition at that point, it reports an undefined control sequence. A log might show something like:
! Undefined control sequence.
l.5 \textcolro{blue}{Hello}
Here, \textcolro is likely a typo for \textcolor. TeX cannot infer the intended command, so it reports the unknown one. Depending on the interaction mode, it may pause or continue after error recovery.
The line number shown in the log is a useful starting point, but the actual cause can be just before it. For example, a missing brace or a damaged command definition can make TeX interpret later text in an unexpected way.
A reliable troubleshooting sequence
- Find the first error in the log. Do not begin with the last message or a later warning. The first error is more likely to identify the original problem.
- Inspect the named command in your source. Check spelling, capitalization, and the backslash. LaTeX command names are case-sensitive:
\LaTeXand\latexare different. - Check whether a package provides the command. If so, load that package in the preamble, before
\begin{document}. - Check custom definitions and included files. A command may be defined in a separate file that was not included, or its definition may appear after its first use.
- Compile again after fixing the earliest problem. Recheck the first error; do not assume every later message needs a separate fix.
Worked example: a missing package
The command \textcolor is provided by the xcolor package. Without that package, this document can produce an undefined-control-sequence error at \textcolor:
\documentclass{article}
\begin{document}
\textcolor{blue}{Hello, XeLaTeX!}
\end{document}
Load xcolor in the preamble to define the command:
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\textcolor{blue}{Hello, XeLaTeX!}
\end{document}
Compile this with XeLaTeX. The package must be installed in your TeX distribution as well as loaded in the document. If TeX instead reports that xcolor.sty cannot be found, that is a separate package-installation problem.
Common causes in XeLaTeX documents
- A typo:
\textcolrois not the same command as\textcolor. - A package was not loaded: Commands such as
\textcolorneed the package that defines them; they are not built into XeLaTeX. - A command was copied from another source: It may require a package, a document class, or a custom preamble definition that was not copied along with it.
- A command was confused with Unicode text: XeLaTeX can process Unicode characters directly, but that does not make arbitrary LaTeX commands valid. Typing a character such as
éis different from using an undefined command intended to produce it. - The wrong engine was selected: This is especially relevant when a document uses commands or packages that require XeLaTeX. Engine mismatches can cause other errors too; check the command and package documentation rather than assuming every undefined command is fixed by switching engines.
For font selection, for example, XeLaTeX documents commonly use fontspec. Loading it makes commands such as \setmainfont available; it does not make unrelated commands available. Put package-loading commands in the preamble, not after \begin{document}.
Common mistakes
Treating every error as an engine problem. The message names an unknown command; first determine whether that command is spelled correctly and defined by the loaded class, packages, or your own macros.
Adding packages at random. A package only helps if it defines the missing command. Check the package documentation or a minimal example that uses the command.
Fixing a later message first. One undefined command can trigger a cascade of confusing follow-up errors. Fix the first error, compile again, and then assess what remains.
Ignoring custom macros. If your source uses a command such as \myresult, make sure it is defined with \newcommand or another suitable definition before it is used, or that the file containing its definition is included.
If the command appears to be spelled correctly and the expected package is loaded, reduce the document to a small example that still produces the error. This helps reveal whether the cause is a missing definition, a package or class conflict, or an issue in an included file.
References
- The LaTeX2e unofficial reference manual
- The fontspec package documentation
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.