LaTeX troubleshooting

How to Fix “Undefined Control Sequence” in LaTeX

Find the unknown command, check spelling and scope, load the correct package, and avoid common false fixes for LaTeX's undefined control sequence error.

Summary: Undefined control sequence means TeX has met a command it does not know at that point in the run. Check the exact command, its spelling, the package that defines it, its scope, and any earlier syntax error.

What the message is telling you

A control sequence is normally a backslash followed by a command name, such as \section or \includegraphics. The log often shows the source line and repeats the unknown command after an l. line number. Start with that token, but also inspect the lines before it.

! Undefined control sequence.
l.18 \textbff
             {Important result}

Here the extra f is the cause. The intended built-in command is \textbf{Important result}.

1. Check spelling and command boundaries

LaTeX command names are case-sensitive. \LaTeX and \latex are different. A command made only of letters continues until the first non-letter, so source such as \LaTeXdocument asks for one command named \LaTeXdocument.

% Broken
This \LaTeXdocument is ready.

% Fixed with an empty group that ends the command name
This \LaTeX{} document is ready.

Also check copied commands for a missing backslash, a visually similar Unicode character, or a line break inserted inside the name.

2. Load the package that defines the command

Many useful commands are not part of the LaTeX kernel. They become available only after their package is loaded in the preamble.

\documentclass{article}
\usepackage{graphicx} % defines \includegraphics
\usepackage{amsmath}  % provides extended maths environments and commands
\usepackage{amssymb}  % provides symbols including \mathbb

\begin{document}
\includegraphics[width=.5\textwidth]{figure.png}
$\mathbb{R}$
\end{document}

Do not load a package only because a forum answer mentioned it. Find the command in the package manual and check that the package is compatible with the rest of the document. CTAN provides maintained pages for packages such as amsmath and hyperref.

3. Define custom commands before using them

A document can define its own commands with \newcommand. The definition must be read before the first use, and it must be in scope.

% Broken: \dataset is used before it is defined
We evaluate on \dataset.
\newcommand{\dataset}{ExampleBench}

% Fixed
\newcommand{\dataset}{ExampleBench}
We evaluate on \dataset.

Definitions inside a group or environment can stop existing when that group ends. If a custom command belongs across the whole document, define it in the preamble or in a style file loaded by the root document.

4. Check whether the correct root document is compiling

A chapter file may rely on packages and commands loaded by main.tex. Compiling the chapter by itself can therefore produce undefined commands even though the complete project is correct.

% main.tex
\documentclass{report}
\usepackage{amsmath}
\newcommand{\projectname}{Signal Study}
\begin{document}
\input{chapters/methods}
\end{document}

Select main.tex as the project root, then compile the whole document. If a chapter truly needs to compile independently, it requires its own suitable preamble or a deliberate subdocument setup.

5. Look for an earlier error that damaged parsing

An unmatched brace, missing environment end, or incomplete command argument can change how later source is read. The command named by Undefined control sequence may be valid but appear in the wrong context because of an earlier problem.

Read upward to the previous structural boundary: the start of the paragraph, environment, section, or included file. Fix the first earlier error and compile again before editing the later command.

A quick diagnosis table

What you seeLikely causeUseful action
A familiar command with one wrong letterTyping errorCorrect the exact spelling and case
\includegraphics is unknowngraphicx is not loadedAdd the documented package in the preamble
A custom macro works in one section onlyDefinition is local to a groupMove a genuinely global definition to the preamble
Many valid commands suddenly failEarlier syntax or wrong root fileFix the earliest error and confirm the compile root
A template command is unknownMissing class/style file or wrong versionImport the required permitted source and read its documentation

Fixes that create new problems

  • Removing the backslash: this turns a command into printed text and usually loses intended formatting or behaviour.
  • Defining an empty replacement: \newcommand{\unknown}{} hides the error without restoring what the missing command should do.
  • Loading many packages at random: this makes the preamble slower and can introduce conflicts.
  • Changing compiler security: shell escape does not define an ordinary missing LaTeX command and should not be enabled as a repair.

Verify the repair

Compile from the correct root, confirm that this error has gone, and inspect the PDF where the command affects output. Then read the remaining log from the top. The official LaTeX help page points to core documentation and community support when a command or package remains unclear. For a broader process, use the common LaTeX compilation errors checklist.