[Tex/LaTex] How to get the category code of a character that is the value of a control sequence

catcodestex-core

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 of catcode \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,

% results category 11
\makeatletter
\def\ABC{`@ }
\the\catcode\ABC

% results category 12
\makeatother
\def\ABC{`@ }
\the\catcode\ABC

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

\catcode`[=13 \catcode`]=13
\def[{*}

The following will result True since we comparing [ with ']' both now being category 13

\ifcat\noexpand[\noexpand]  True \else False \fi

Also \ifcat[* True \else False \fi is True

Since 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!

\def\abc{`A}
\chardef\abc=65

or Joseph's suggestion for comparisons:

\let\abc=A
   \ifcat\noexpand\abc A%
     \TRUE
   \else
     \FALSE
   \fi
Related Question