I used to think that the behavior of underscores ('_') was as follows:

IF ('_') {
ELSE IF ('\_')

I was wrong.

%% Use underscore for subscript while not in math mode

% Use underscore in math mode to make a subscript
% No error
$ X_{subscript} $

% Escape Sequence (backslash) tells LaTeX we want underscore char
% instead of '_' means 'make a subscript'
% No error
X \_ \_ \_ Oh look! underscore characters.

% underscore in the label name
% No error
    x = y \label{LAB_BY}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% put a space after '_' so that after escape reads only '_' and not '_BY'
%    x = y \label{LAB\_ BY}

%% Do not want subscript functionality of '_'
%% Use backslash to put an underscore into the label name
%% fail to put a space char after '\_'
%    x = y \label{LAB\_BY}

When are underscores allowed and when are they not allowed?
What does '_' signify if you are not in math mode?


No special behaviour is assigned to any character in TeX, everything depends on the current catcode regime.

If \catcode95=11 (often written as \catcode`\_=11 then _ is a letter and you can use it anywhere you can use x so


a_b  \def\one_two_three{four}  \one_two_three

is all good and would typeset a_b four.

But normally _ has catcode 8 which means it has a subscript meaning if encountered in math mode, and an error if the character token would otherwise be typeset directly in text mode.

However other uses of the token, it is just a character token so for example

\newcommand\foo{a___jd_ \_ }

is legal and defines \foo to be that sequence of tokens (it may possibly generate an error if used, but not necessarily, depending on context).

Similarly in a \write or \csname (both constructs used by LaTeX's \label macro) any non active legal token just acts as itself so \csname one_two_three\endcsname constructs the control sequence with name one_two_three which is the same as the \one_two_three accessed above by use of catcode changes.

Note that \_ is just the control sequence with name _ it is not forced to produce an underscore. It does by default in latex, but just as \\ doesn't produce a backslash you could define \_ to do anything:

\def\_{zzzzz} \_

would produce zzzzz for example.

\_ is not predefined by TeX, laTeX defines it to be the macro:

| improve this answer | |

I'll deal with the standard LaTeX settings.

What's _?

The character _ is special; if found in math mode it signals that a subscript is wanted and will basically turn into a subscript the token or braced group that follows. Using _{1} is recommended even if the subscript is just a single character.

If _ is found outside math mode and TeX is typesetting, an error will be raised. On the other hand, TeX is not typesetting all the time. In some cases it absorbs tokens for processing them in different ways than printing something.

This is the case, for instance, when reading the argument to \label, \ref or \cite where _ can be used without worries.1. This is because here _ is used as a character for forming an internal command that is used for generating the required object (a label, a reference or a citation).

What's \_?

The command \_ is defined to print an underscore. While this might seem not a big deal, there are some complications due mainly to history: fonts need not have an underscore at position 95 (the ASCII decimal code for the underscore), so just telling TeX “please, typeset the character at position 95 in the current font” is not a good set of instruction in order to actually get an underscore in the printout.

In any case, when you want an underscore in a label, you don't want to print it, but just using it as a marker.2 So \_ is out of the question here.


1 Documents using not carefully written macros might break on \label{LAB_BY}, if they set _ as an active character. A prominent case is the syntax package.

2 See What are the valid names as labels? for what are the “legal” characters in a label.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.