# Is it safe to set underscore to a non-active character?

I am looking for wisdom i.e. potential side-effects that I may not have considered.. Underscores are quite common and irritating for inexperienced LaTeX users to escape. Let's assume that I have no interest in subscript of math mode. Is it safe to set underscore to a non-active character?

By default _ is for subscript and you need to convert it to a literal character by prefixing it with a catcode 0 \.

Is there any danger of making life easier by putting this in my document?

# Category 12 Other Character

\catcode\_=12   %
\catcode_=12    %
\catcode"005F=12 % unicode decimal syntax
\catcode'137=12  % octal syntax
\catcode95=12    % decimal syntax


or

# Category 11 Alphabetic Character

\catcode\_=11   %
\catcode_=11    %
\catcode"005F=11 % unicode decimal syntax
\catcode'137=11  % octal syntax
\catcode95=11    % decimal syntax


My thought is that it would not be if done at the right time (after loading all packages).

# Notes

• I am using xelatex with fontspec, therefore I ensure that all of my documents are UTF-8 encoded.
• etoc adds stuff to the \contentsline which means that the catcode of underscore must be changed prior to definition time of these macros, otherwise when the .toc is read after \begin{document} on the following run, an underscore will be read as catcode 8 within the \contentsline macro even if you set it later, because the group protects any postdefinition changes.
• Provided you use T1 encoding. Otherwise a_b would produce a ̇b, rendering the _ as a raised dot. – egreg Mar 22 '17 at 12:24
• @egreg I am using UTF-8 with fontspec. – Jonathan Komar Mar 22 '17 at 12:25
• Then no problem: OpenType or TrueType fonts should have an underscore in the relative slot. – egreg Mar 22 '17 at 12:26
• The construction \catcode\<char>=12 is equivalent to \catcode<char>=12 (unless <char> has category code 0, 5, 9 or 15). – egreg Mar 22 '17 at 12:27
• @egreg just noticed and issue when the underscore is contained within a section heading and reread from the aux file. Somehow I need to change the catcode at the start of the \begin{document} macro. It is not enough to just set the catcode after \begin{document}. – Jonathan Komar Mar 22 '17 at 12:51

I see no problem in making _ a printable character (category code 12), except when the font encoding is OT1, because the fonts using this encoding may not have an underscore at slot "5F.

If you use fontspec, the output font encoding is automatically set to TU or, with older versions, to EU1 or EU2 (depending on XeLaTeX or LuaLaTeX). These are not OT1.

For the occasional math mode subscript you can use \sb instead of the usual underscore.

A rather minimal example:

\documentclass[a4paper]{book}

\usepackage{fontspec}

\catcode_=12

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\chapter{A title_with_underscores}\label{a_b}

\ref{a_b}

\end{document}


Note that the catcode change has to be made before any usage of _ as a printable character, in particular before \begin{document}, when LaTeX reads the .aux file.

About the syntax to use, any among

\catcode\_=12   %
\catcode_=12    %
\catcode"5F=12   % unicode decimal syntax
\catcode'137=12  % octal syntax
\catcode95=12    % decimal syntax


is good. The first two specify an alphabetic constant and are handier. Since the underscore starts up having category code 8, the backslash is not required, but it doesn't hurt either.

Don't set _ to category code 11. This may hurt in several circumstances; for instance

\macro_
`

would be interpreted as a single token.

• A detail for those interested: Regarding startup: I read that initex starts up the underscore with catcode 12. It is later set by Plain TeX or by LaTeX to catcode 8 at an early stage. – Jonathan Komar Mar 22 '17 at 14:00
• @macmadness86 Yes, that's right. – egreg Mar 22 '17 at 14:03