Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
2  
BTW: the catcode of a is 11 (letter) not 10 (space). –  Martin Scharrer Jun 22 '11 at 13:22

2 Answers 2

up vote 14 down vote accepted

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.

share|improve this answer

As explained in an exercise of the TeXbook one can also write

\newcommand{\printcatcode}[1]{%
   \ifcase\catcode`#1\relax
      escape\or
      beginning of group\or
      end of group\or
      math shift\or
      tab\or
      end of line\or
      parameter\or
      superscript\or
      subscript\or
      ignored\or
      space\or
      letter\or
      otherchar\or
      active\or
      comment\or
      ignored\fi}

 The category code is `\printcatcode\%'

Just to point out that any of the "code tables" in TeX can be used to access the value (\catcode, \lccode, \uccode, \mathcode, \delcode, \sfcode).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.