9

I am trying to use pdflatex to type set some files that have underscores in the \label. Such as \label{abc_xyz} and have been unsuccessful. I keep getting errors such as:

! Missing \endcsname inserted.
<to be read again>
                   \protect

MWE:

\documentclass[twoside]{article}
\def\itt{\tt #1}
\def\litt#1{\tt #1\label{#1}}
\begin{document}
\itt{pqr_mno}
\litt{abc_xyz}
\litt{xyz_abc}
\end{document}

I have made several changes and all have been unsuccessful. Any help would be appreciated

A Feldman
  • 3,930

1 Answers1

19

Usually the underscore with its standard catcode "subscript" (8) does not cause problems, if used inside \label or \ref:

\documentclass{article}
\begin{document}
\section{Hello World}
\label{sec_hello}
See section \ref{sec_hello}.
\end{document}

Also shorthands of package babel are not a problem, because babel patches the \label/\ref system to add support for shorthands.

Active underscore

Probably you are using a package that makes the underscore active. Then it becomes more complicate. A workaround is \string to make the active underscore behave as normal character:

\label{sec\string_hello}
\ref{sec\string_hello}

Also the label name is written into the .aux file and read again at the end of document. Here the catcode should be restored:

\usepackage{atveryend}
\AfterLastShipout{\catcode`\_=12\relax}

If the unknown package makes the catcode of the underscore active before \begin{document}, then it should be inactive during the reading of the .aux file at the end of the preamble.

\ifnum\catcode`\_=\active
  \catcode`\_=12\relax
  \AtBeginDocument{\catcode`\_=\active}%
\fi

The complete example:

\documentclass{article}

\catcode`\_=\active
\def_{\textunderscore}

\usepackage{atveryend}
\AfterLastShipout{\catcode`\_=12\relax}
\ifnum\catcode`\_=\active
  \catcode`\_=12\relax
  \AtBeginDocument{\catcode`\_=\active}%
\fi
\begin{document}
\section{Hello World}
\label{sec\string_hello}
See section \ref{sec\string_hello}.
\end{document}

Package underscore

Depending on the package that makes the underscore active, the scope of the activeness and the definition of the underscore, there might be more comfortable ways. For example, package underscore makes the underscore active and that breaks the referencing system. But the package supports babel. The underscore behaves as shorthand and is supported, if babel is loaded:

\documentclass{article}

\usepackage[english]{babel}
\usepackage{underscore}

\begin{document}
\section{Hello World}
\label{sec_hello}
See section \ref{sec_hello}.
\end{document}
Heiko Oberdiek
  • 271,626
  • Thanks for your answer. Here is a simple set of source that shows the problem. \itt works but \litt does not. – user2207980 Jun 27 '13 at 19:40
  • \documentclass[twoside]{article} \begin{document}

    \def\itt{\tt #1}

    \itt{pqr_mno}

    \def\litt#1{\tt #1\label{#1}}

    \litt{abc_xyz}

    \litt{xyz_abc}

    \end{document}

    – user2207980 Jun 27 '13 at 19:40
  • 1
    @user2207980: That is a different matter, how to set the underscore in text mode. There are many ways: \itt{pqr\_mno}, \itt{pqr\textunderscore mno}; \verb|pqr_mno|; \newcommand*{\itt}{\texttt{\detokenize{#1}}}; package underscore etc. – Heiko Oberdiek Jun 27 '13 at 20:29