How can I get the category code of a character that is the value of a control sequence?
If I do this
The catcode for A is \the\catcode`A.
I get
The catcode for A is 11.
If I do this
\let\abc=A
The catcode for \abc\ is \the\catcode\abc.
I get "Missing number, treated as zero."
I think I understand it like this:
\abc is replaced with the character code, category code pair <65, 11>, but TeX expects just a number after \the\catcode, so it inserts a 0, then leaves my A hanging around.
Best Answer
Sadly Knuth in his wisdom gave only few clues in the TeXBook for cases like this.
In order to understand the error we need to firstly understand the definition of
\the
, which is for the case ofcatcode
\the<codename><8-bit number>
, where<codename>
stands for either\catcode
,\mathcode
,\lccode
etc...So clearly in this case
catcode
TeX expects a number and hence the error generated in the example provided by the OP.All solutions provided by the other posts revolve around changing the definition one way or another to produce the required character code number and which I am demonstrating here with some different examples:
The example below will produce the right answer in both cases,
While comparing two tokens using
\ifcat
things become a bit more complicated, if you want to compare two active characters you have to say\noexpand
- Knuth says so somewhat obscurely in Exercise 20.11!Consider the following definitions
The following will result True since we comparing
[
with ']' both now being category 13Also
\ifcat[* True \else False \fi
is TrueSince now we have established two facts
\the\catcode
needs a number and if you use an active character, implicitly or explicitly we can understand why the following will all work!or Joseph's suggestion for comparisons: