The manmac
format used for processing the TeXbook defines ^
in a very special way.
Under the standard setup, ^
alone is the marker for superscripts and also has a very peculiar behavior when followed by itself, see What is the role of an unescaped circumflex or hat character " ^ "?
However, manmac.tex
makes ^
into an active character and Knuth uses it for indexing:
% Indexing macros
\newif\ifproofmode
\proofmodetrue % this should be false when making camera-ready copy
\newwrite\inx
\immediate\openout\inx=index % file for index reminders
\newif\ifsilent
\def\specialhat{\ifmmode\def\next{^}\else\let\next=\beginxref\fi\next}
\def\beginxref{\futurelet\next\beginxrefswitch}
\def\beginxrefswitch{\ifx\next\specialhat\let\next=\silentxref
\else\silentfalse\let\next=\xref\fi \next}
\catcode`\^=\active \let ^=\specialhat
\def\silentxref^{\silenttrue\xref}
Let's look at some calls from the source of the TeXbook.
Another noteworthy characteristic of this manual is that it doesn't
always tell the ^{truth}.
Let's keep the name \TeX\
for the language described here, since it is so much better, and since
it is not going to change any more. ^^{TeX78}
medium that doesn't allow lowering of the `E', is to type `^|TeX|'. Then
The solution is to type \thinspace|'\thinspace''|, which
produces '\thinspace'' as desired.^^|\thinspace|
Snippet 1 has a word that is both indexed and printed; snippet 2 has a word that is indexed but not printed. Snippets 3 and 4 are similar, but the term is stored verbatim in the index auxiliary file.
Note that in the TeXbook, |...|
is always used as a shorthand for printing a term verbatim.
The character ^
is made active and its meaning is made identical to \specialhat
.
If the character is found in math mode, a standard category code ^
is delivered to the main input stream, thus enabling usage of superscripts with the standard notation.
In text mode it is different, because \beginxref
is delivered.
The \beginxref
macro checks the next token with \futurelet
. If the next token is ^
, \silentxref
is delivered, otherwise \ifsilent
is set to false and \xref
is delivered.
As you see, \silentxref
gobbles the next ^
and does \silenttrue\xref
.
Describing \xref
would be very long and not relevant to the description you were looking for.
A subtle point to be noted. The ^
in the replacement text for \specialhat
appears before the change in category code, so it is the normal “superscript” character (tokens in the replacement text are “frozen” at definition time). The ^
in the parameter text of \silentxref
appears after the category code change, so it should have category code 13 when \silentxref
is expanded (which is normally already ensured by the fact that \silentxref
has been called).
Just to play around, here's an expl3
version of the code (just the macros, not the setup for writing to the auxiliary index file)
\bool_new:N \l_manmac_silent_bool
\cs_new_protected:Nn \manmac_specialhat:
{
\mode_if_math:TF { ^ } { \manmac_beginxref: }
}
\cs_new_protected:Nn \manmac_beginxref:
{
\peek_charcode_remove:NTF ^
{
\bool_set_true:N \l_manmac_silent_bool \manmac_xref:
}
{
\bool_set_false:N \l_manmac_silent_bool \manmac_xref:
}
}
\char_set_active_eq:NN ^ \manmac_specialhat:
\char_set_catcode_active:N ^
I believe this code makes clearer the actions described above, because details like the several assignments to \next
can be avoided.
Here's a \let\next
free version of the same macros
\newif\ifsilent
\def\specialhat{%
\ifmmode
\expandafter^%
\else
\expandafter\beginxref
\fi
}
\def\beginxref{\futurelet\next\beginxrefswitch}
\def\beginxrefswitch{%
\ifx\next\specialhat
\expandafter\silentxref
\else
\silentfalse\expandafter\xref
\fi
}
\catcode`\^=\active \let ^=\specialhat
\def\silentxref^{\silenttrue\xref}
^^{TeX78}
and^^|\copyright|
?