Ceci est une ancienne révision du document !

# Comment utiliser un underscore dans un \label ?

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}`

3_composition/texte/renvois/underscore_dans_un_label.1500612743.txt.gz · Dernière modification: 2017/07/21 06:52 par jejust