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
the
!\let\abc=@
, you can access the catcode of@
by\expandafter\SomeMagic\meaning\abc
, where\SomeMagic
reads the meaning of\abc
(which isthe character @
), and converts it to the form you want. I see two things you might want: know the catcode of the@
that is inside\abc
, or the catcode that the character@
currently has. Just tell me which one you want and I can write an expandable command for that.\let
to\abc
?\long \def \getcharcode #1{\if \space \noexpand #1\expandafter 3\expandafter 2\else \expandafter \expandafter \expandafter \getcharcodeI \expandafter \meaning \expandafter #1\expandafter A\fi } \def \getcharcodeI #1A{\getcharcodeII #1A#1A#1AB} \def \getcharcodeII #1 #2 #3 #4 #5 #6 #7A#8B{\number `#7 }
should work (try\getcharcode {\abc}
), but it will choke horribly if given anything else than an explicit or implicit character token.