TeX-Core – How to Get the Catcode of a Token

catcodestex-core

For debugging a complicated macro I would like to print out the catcode of a token. Optimally I would like to have a macro \getcatcode such that, for example, \getcatcode{a} would expand to 10. How can this be done?

I found lots of information about how to set/change catcodes, but nothing about how to read them.

Best Answer

Use \catcode together with \the to get the catcode of the token:

\the\catcode`a

Note: The ` turns the next character into its ASCII number which is required for \catcode.

As custom macro:

\newcommand{\getcatcode}[1]{\the\catcode`#1}

Special characters must be escaped with a backslash, e.g. % must be written as \%, # as \# etc. It doesn't hurt to write normal letters the same way, e.g. \getcatcode\a works as well.

Related Question