# LaTeX Nesting \verb in \section

Why is

\section{\verb|mySec|}


invalid LaTeX code.

• Not valid: \begin{array}{cc} \textit{a & b} \end{array}. In general, scoping cannot cross another group. Aug 22 '18 at 0:06

That's because verbatim content always causes you trouble.

The \section command takes one argument, which in the first case, is \textit{mySec} and in the second it is \verb|mySec|.

Everything by now was read and tokenized by TeX with the due category codes. In the first case you have a control sequence \textit, a open brace with \catcode 1, 5 characters with \catcode 11, and a close brace with \catcode 2. So far so good.

The same goes for the second case, but this time you have a control sequence \verb and 7(!) characters with \catcode 11 (the | is \catcode 12, but that doesn't do much here).

The problem here is that the | are already tokenized by TeX, so their catcode can't change. But the first(ish) thing that \verb does is to make the \catcode of the | active, but it can't anymore, thus you get an error.

Basically, verbatim content can't appear as arguments to a macro, unless that macro is carefully built to allow it.

If you're interested in a workaround, though, you could use Bruno Le Floch's cprotect:

\documentclass{article}
\usepackage{cprotect}
\begin{document}
\section{\textit{mySec}}
\cprotect\section{\verb|mySec|}
\end{document}