# Why don't I get any space after \thepage?

The code \thepage of yields the output 3of.

I can fix it by writing \thepage~of, but what causes this in the first place? I can't imagine when it would be useful.

-
Spaces after control sequences are ignored (if they aren't of the form \<nonalphabetic>, like \!). – egreg Dec 11 '11 at 20:39
@egreg: Aha. What is the proper way of adding breakable spaces after control sequences? – Tim N Dec 11 '11 at 20:40

A TeX macro 'eats' the white space after it, so \TeX nician is typeset as TeXnician, and \thepage of is typeset as 3of. You can either type \thepage~of, or \thepage\ of, or \thepage{} of. In the first case the space is unbreakable, in the others it is not.

Update: if you need space after macros for any reason, try xspace package:

\documentclass{standalone}
\usepackage{xspace}
\newcommand{\Thepage}{\thepage\xspace}
\begin{document}
\Thepage of one.  The page is \Thepage.
\end{document}


-
The space is stretchable in all cases. Non stretchable space is achieved only with \kern. With the default meaning of \thepage the results of \thepage\ of and \thepage{} of are exactly the same under all circumstances (if the space factor of digits and letters isn't changed). They may be different if the expansion of \thepage ends with a period, say, or another character having space factor different from 1000. (Note: there is actually a very small difference if \thepage is defined to give \Alph{page}, as uppercase letters have space factor 999.) – egreg Dec 11 '11 at 20:51
thanks - corrected – Boris Dec 11 '11 at 20:52
@egreg: Many thanks, I was already wondering if there was a difference. – lockstep Dec 11 '11 at 20:52

Suppose spaces after control sequences wouldn't be ignored. In that case, how would you achieve the output 3of if you so desired? You can't write \thepageof because that would be, well, a control sequence with different name. :-)

That's why spaces after (alphabetic) control sequences are ignored. If you want a space, write \thepage\ of or \thepage{} of. (\thepage~of is only correct if you don't want a line break after \thepage.)

-
\thepage{}of :) But I see the benefit now that I know it goes for all control sequences. – Tim N Dec 11 '11 at 20:48
Or \thepage\unskip of – Boris Dec 11 '11 at 20:54
Some people also write {\thepage} of which also works, but it IMHO strange. Then you could also use \thepage\space of, which is useful in expandable context where you don't like {} or any macro like \  or ~, like if you write to files. – Martin Scharrer Dec 11 '11 at 20:56
I think we all agree that Knuth chose the most practical solution. ;-) – lockstep Dec 11 '11 at 20:58
Actually, xspace package allows one to define macros that have spaces after them if needed. Try, for example \usepackage{xspace} \newcommand{\Thepage}{\thepage\xspace}, and then typeset \Thepage of one. The page is \Thepage.. – Boris Dec 11 '11 at 20:59

For reference (and DEK entertainment), from the TeX Book (chapter 3 Controlling TeX, p 8):

It is usually unnecessary for you to use “control space,” since control sequences aren't often needed at the ends of words. But here's an example that might shed some light on the matter: This manual itself has been typeset by TeX, and one of the things that occurs fairly often is the tricky logo \TeX, which requires backspacing and lowering the E. There's a special control word

\TeX


that produces the half-dozen or so instructions necessary to typeset \TeX. When a phrase like \TeX\ ignores spaces after control words. is desired, the manuscript renders it as follows:

TeX ignores spaces after control words.

Notice the extra \ following \TeX; this produces the control space that is necessary because TeX ignores spaces after control words. Without this extra \, the result would have been

TeXignores spaces after control words.

On the other hand, you can't simply put \ after \TeX in all contexts. For example, consider the phrase

the logo TeX'.

In this case an extra backslash doesn't work at all; in fact, you get a curious result if you type

the logo \TeX\'.


Can you guess what happens? Answer: The \' is a control sequence denoting an acute accent, as in our P\'olya example above; the effect is therefore to put an accent over the next nonblank character, which happens to be a period. In other words, you get an accented period,...

Computers are good at following instructions, but not at reading your mind.

-