# Which commands consisting of a non-letter eat spaces afterwards?

Command names consist of a sequence of letters (until and excluding the first non-letter) or of a single non-letter.

Command names ending in a letter (including of course those consisting of a single letter, such as \S and \P) eat all spaces after them. (Compile for example \P␣␣,H.)

What about command names consisting of a single non-letter: which of these eat subsequent spaces? H\$H and H\$␣H produce different output (and that's true for e.g. % and & too). While the space-producing command \, does not eat spaces, the space-producing command \␣ does seem to eat all subsequent spaces. (H\␣H and H\␣␣H and H\␣␣␣H all produce the same output. H \␣H is different; see next paragraph.)

Knowledge of TeX's behavior will explain why for example A\,B, A␣\,B/A\,␣B, A␣\,␣B produce different results (in text mode); if a user is not aware of what happens and naively (but understandably) assumes that such spacing commands eat all spaces around them, he or she might run into surprises. (Actually only few commands seem to eat preceding spaces, though such behavior is possible: have your macro start with \unskip.)

Guide to the answers:

• most concise summary (≈ "only \␣"): Joseph Wright's answer [if it weren't for Heiko's answer, I would have accepted this one]
• all the details (with an interesting detail about empty (!) command names): Heiko Oberdiek's answer
• apparent exceptions (the 7 standard one-letter, accent-producing commands and \\): Mico's answer
-
Re the edit: I don't think \! is a valid command in text mode: its definition is the same in Plain TeX and in LaTeX, that is, \mskip-\thinmuskip. You probably wanted \,. –  egreg Apr 30 at 20:47
@egreg I did mean \!, but (thanks to you drawing attention to this, I found out that) in order to have it be a valid text mode command one needs to load amsmath (which executes \renewcommand{\!}{\tmspace-\thinmuskip{.1667em}} and also \let\negthinspace\!) or use the class memoir (which defines \DeclareRobustCommand{\!}{\relax\ifmmode\mskip-\thinmuskip\else\negthinspace\fi‌​}). Still, I've replaced \! in the relevant paragraph ("Knowledge of ...") by \,. –  Lover of Structure May 5 at 23:06

## 4 Answers

From "The TeXbook":

If TeX sees an escape character (category 0) in any state, it scans the entire control sequence name as follows. (a) If there are no more characters in the line, the name is empty (like \csname\endcsname). Otherwise (b) if the next character is not of category 11 (letter), the name consists of that single symbol. Otherwise (c) the name consists of all letters beginning with the current one and ending just before the first nonletter, or at the end of the line. This name becomes a control sequence token. TeX goes into state S in case (c), or in case (b) with respect to a character of category 10 (space) [read: "in case (b) if the single symbol is of category 10 (space)"]; otherwise TeX goes into state M.

State S is beginning of line, there spaces are ignored; state M is middle of line.

If the name consists of letters entirely, the length does not matter, one or more letters. Then TeX ignores spaces as in the begin of a line. The same happens in case of the command \␣. The command itself sets a space, but following spaces are ignored.

Backslash at line end:

If TeX reads a line, it removes the end of line characters (carriage return and/or linefeed) and all space characters from the right end (i.e., any such characters occurring immediately before the end of line character). Then it inserts the character, configured by \endlinechar, unless it is suppressed (e.g. it has a negative value). From "The TeXbook":

TeX deletes any ⟨space⟩ characters (number 32) that occur at the right end of an input line. Then it inserts a ⟨return⟩ character (number 13) at the right end of the line, except that it places nothing additional at the end of a line that you inserted with I during error recovery. Note that ⟨return⟩ is considered to be an actual character that is part of the line; you can obtain special effects by changing this catcode.

...

The special character inserted at the end of each line needn't be ⟨return⟩; TeX actually inserts the current value of an integer parameter called \endlinechar, which normally equals 13 but it can be changed like any other parameter. If the value of \endlinechar is negative or greater than 255, no character is appended, and the effect is as if every line ends with % (i.e., with a comment character).

Note: LuaTeX restricts the values of \endlinechar. The upper limit is 127. Larger values cause the error ! Invalid \endlinechar.

In LaTeX the end of line character is ^^M (character code 13, 0x0D) and has category 5 (end of line). If TeX is in state M, this end-of-line character is converted to a space [this is the important part!], thus the backslash at the end of line usually becomes \␣.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\begin{document}

\expandafter\def\csname\endcsname{<empty>}
\def\ {<space>}
\def\@{<at>}

[\
]

\begingroup
\endlinechar=-1
[\
]
\endgroup

\begingroup
\endlinechar=@ %
[\
]%
\endgroup %

\end{document}


-
Is there anything one can do with the empty command name (⇔ \csname\endcsname)? –  Lover of Structure Apr 26 at 22:31
@LoverofStructure: You can define it and use it if you like. ;-) –  Heiko Oberdiek Apr 26 at 22:36
If I define \expandafter\def\csname\endcsname{TEST} and then write \csname\endcsname or \csname␣␣␣\endcsname in my document, the macro gets invoked. But if I use line-final \, the command is not invoked. What is happening? –  Lover of Structure Apr 26 at 23:14
@LoverofStructure: At the end of line TeX inserts the character that is defined by \endlinechar. Therefore it needs to be turned off before: \endlinechar=-1. –  Heiko Oberdiek Apr 26 at 23:16
Background to David's comment: What's new in TeX, version 3.14159265? –  Heiko Oberdiek Apr 27 at 1:52

The general TeX rule, which is explained extremely well in the other answers, is that whitespace after a macro that consists of a single non-letter (where a "nonletter" is anything with category code 11) is not "eaten" or ignored. Thus, \$50 and \$50 are typeset differently.

At first glance, there is an exception to this rule: The accent- and diacritic-producing macros that consist of a single nonletter, e.g., \' and \", eat the whitespace between the macro and the "accentee" (the letter that is to receive the accent, diacritic, etc), in the sense that it doesn't matter if they're followed directly by the accentee or one or more spaces and then the accentee. To wit, the program

\documentclass{article}
\begin{document}
\'e, \' e, \'  e, \'{e}, \' {e}
\end{document}


produces five identical é characters.

However, this apparent exception to the general rule mentioned above arises not because these macros constitute a true exception to the rule, but because something else is going on. As @egreg has pointed out in a comment, the accent-producing macros take an undelimited argument, and TeX always ignores spaces while it's scanning for such arguments. (Aside: An undelimited argument is a TeX concept, not a LaTeX concept: \def\abc#1{...} is a macro with one undelimited argument. In contrast, \def\xyz#1+{...} and \def\uvw&#1&{...} treat everything up to the first + symbol, and between the first and second & symbol, as the first argument, respectively. For much more on macros with delimited and undelimited arguments, see the double-dangerous-bend item at the bottom of p. 203 of the TeXbook.)

Clearly, one needs to distinguish between single-nonletter macros that do not take an argument (e.g, \#, \\$, \%, \&) and those that take undelimited arguments when talking about whitespace being eaten after the command.

Looking over my computer's keyboard, there appear to be (at least) seven single-nonletter accent-producing macros that take an undelimited argument:

\'  \  \^  \"  \~  \=  \.


Addendum: In addition to these accent-producing macros, there's the line-breaking macro, \\, which also consists of a single nonletter (the backslash character itself). It takes two types of optional arguments: (i) material delimited in square brackets, which indicates an amount of extra vertical space that should be inserted, and (ii) a single * symbol. Whether or not whitespace is eaten between \\ and one of these optional arguments depends on whether the amsmath package -- or the mathtools package, which fixes what many people consider to be a minor bug in amsmath's treatment of \\ -- is loaded.

-
That's for a different reason: the macros have an (undelimited) argument and TeX always disregards spaces when looking for an undelimited argument. –  egreg Apr 26 at 22:15
@egreg - Maybe I misunderstood the question, but I thought it was about whether there are any single-non-letter macros that ignore ("eat", if one prefers) trailing spaces. –  Mico Apr 26 at 22:23
I upvoted your answer; I just wanted to remark why they seem to eat spaces like control words. –  egreg Apr 26 at 22:31
This eating occurs at a different time: there is no space token in \foo a but there us a space token in \' a However the space token is removed if \' is expanded. –  David Carlisle Apr 27 at 0:11
@LoverofStructure the behaviour of \\  and white space differs depending whether amsmath is loaded, and there is an open question about what it should do in latex3 code, ams redefines it so white space is not ignored while looking for [ so that [ at the start of an array cell (if on a new line) is not taken as an option for the \\  ending the line above. –  David Carlisle Apr 27 at 1:33

That's easy: none of the control symbols eats spaces after it.

However, spaces are reduced to one before the control sequence name is formed in the case of \␣, which is coherent with the claim that any sequence of spaces in input is equivalent to one space.

Well, this might not be the complete truth, but it's a good rule of thumb.

-

TeX has two kinds of control sequence (the escape character followed by one or more characters):

• A control word: the escape character followed by one or more 'letters'
• A control symbol: the escape character followed by exactly one non-'letter'

where the escape character is any character with category code 0, a 'letter' is any character with category code 11 and a non-'letter' is any character with category code not 11.

After control words, TeX goes into state 'skipping spaces': informally, it 'eats the spaces'. However, it does not do with after a control symbol with one exception: the control space \␣.

Thus there are two cases:

• Control words and control space: spaces after are skipped
• Control symbols except the control space: no spaces are skipped

See TeX by Topic, section 2.5.2.

-