Following on from this question, I'd like to ask a more general question:
What are category codes, and what can I achieve by changing them?
When TeX parses input, it assigns each character read a category code. How TeX subsequently interprets the input then depends on both the character and it's category code. There are 16 category codes that can be set by the programmer, plus one special internal one. The 16 standard ones number from 0 upward. Category code 0 is for escape characters, usually
Now when TeX reads input, each character is associated with a category code to generate tokens. So if the input reads
Category codes often become important when TeX is deciding on what is and is not a control sequence. With only the alphabet as 'letters', something like
is the control sequence
then TeX will look for a macro called
There are many effects that can be achieved using category codes. An obvious one is the non-breaking space
We can use different category codes to make 'private' code areas. For example, plain TeX and LaTeX2e us
Verbatim material is another area where category codes are vital (if complex!). The reason you can't nest verbatim material inside anything else is that once TeX has assigned category codes it is only partially reversible. Anything which is 'ignored' or 'comment' is thrown away: you can't get it back. (With e-TeX, you can reassign category codes, but anything that is already gone stays 'lost.)
(Note for the interested) The 'special' category code is 16, which is used in the
Rather than defining primitive commands for common tasks such as starting math mode or denoting superscripts and subscripts, Knuth decided to reserve some characters for these purposes. There are also other needs: grouping, denoting the macro parameters and, most important, escaping in order to express commands.
There are sixteen category codes:
Usually there's only one character having categories 0 to 8
(^^M denotes the invisible character that TeX puts at the end of all input lines, changing the system dependent one(s) that might be present); uniqueness is not required, but preferred: why should one want to have two different escape characters which would act just in the same way? (See later on.)
Category 10 is the space but also the
All letters have category 11 and punctuation characters such as
Category 9 and 15 were put into TeX because there are "dangerous" character (ASCII "null" and ASCII "delete") that could be misinterpreted by editors. Actually category 9 has other uses: in LaTeX3 style files the space is assigned category 9, to help programmers in avoiding the dreadful "spurious spaces".
Category 14 is the well-known
Category 13 is very special; Plain TeX and the LaTeX kernel use only one active character, namely
so that typing
When we want to typeset verbatim TeX code, many of the special characters are assigned category code 12; but when we type
This is a problem: when TeX is scanning the argument to a command, it freezes the category codes: when a character enters TeX it is transformed into a pair
The LaTeX commands
work? One might expect that when TeX expands
If we assign category code 0 to
This is not an easy topic and I can only refer you to the TeXBook for more details, but here is a short outline.
Every character that TeX reads from your file has two numbers associated with it. A "character code" and a "category code". TeX does not know glyphs - only numbers - and this is part of its strengths. You can think of font tables as look-up list. If you give it a number TeX will look at the list and print the glyph that happens to be in that position.
The second number is the "category code". This TeX uses it to intelligently parse the input. TeX needs to know for example if a curly left bracket
Consider that you may wish to replace curly brackets with square brackets
Try it out without the
In the last example in lieu of
There are great answers here already so let me just show a small example for what you can do by changing catcodes. Assume that you have numbers in a table like
Now you can use