12.11 \protect

All LaTeX commands are either fragile or robust. A fragile command can break when it is used in the argument to certain other commands, typically those that write material to the table of contents, the cross-reference file, etc. To prevent fragile commands from causing errors, one solution is to precede them with the command \protect.

For example, when LaTeX runs the \section{section name} command it writes the section name text to the .aux auxiliary file, moving it there for use elsewhere in the document such as in the table of contents. Such an argument that is used in multiple places is referred to as a moving argument. A command is fragile if it can expand during this process into invalid TeX code. Some examples of moving arguments are those that appear in the \caption{...} command (see figure), in the \thanks{...} command (see \maketitle), and in @-expressions in the tabular and array environments (see tabular).

If you get strange errors from commands used in moving arguments, try preceding it with \protect. Each fragile command must be protected with their own \protect.

Although usually a \protect command doesn’t hurt, length commands such as \parindent should not be preceded by a \protect command (see Lengths. Nor can a \protect command be used in the argument to \addtocounter or \setcounter command (see \setcounter and \addtocounter. These commands are already robust.

As of the October 2019 release of LaTeX (https://www.latex-project.org/news/latex2e-news/ltnews30.pdf), most commands that had been previously fragile were fixed to be robust. For example, any command taking an optional argument, such as \root or \raisebox, was fragile, but is now robust. Similarly, \(...\) math was fragile and is now robust ($...$ has always been robust).

Perhaps the most commonly used remaining fragile command is \verb; for example,

  \caption{This \verb|\command| causes an error.}

Adding \protect does not help here. It’s usually feasible to rewrite the caption (or section heading or whatever) to use \texttt, often the simplest solution.

Alternatively, to use \verb, you can apply the \cprotect command from cprotect package (https://ctan.org/pkg/cprotect) to the \caption:

\cprotect\caption{This \verb|\command| is ok with \verb|\cprotect|.}

\cprotect also allows use of \begin...\end environments in moving arguments, where they are normally not allowed, via a similar prefix command \cprotEnv.

Unofficial LaTeX2e reference manual