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

\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
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
{\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
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
xspace -- see http://tex.stackexchange.com/questions/25820/no-space-following-macro-without-argument
– lockstep
Dec 11 '11 at 21:00
xspace, but another tool in a toolbox sometimes might be handy. And, of course, it is always a pleasure to study David's and Morten's code.
– Boris
Dec 11 '11 at 21:05
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\TeXthat 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 beenTeXignores spaces after control words.On the other hand, you can't simply put
\after\TeXin all contexts. For example, consider the phrasethe 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 ourP\'olyaexample 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.
\<nonalphabetic>, like\!). – egreg Dec 11 '11 at 20:39