LuaTeX category code tables

There are lots of very clever ideas in LuaTeX, and it’s easy to miss some of the good stuff there is. One that many people might miss is category code tables. As any TeX programmer rapidly becomes aware, category codes are central to TeX, and the construction

\catcode`\<char> = <number>\relax

is one you soon get used to. The problem comes when several people start altering the codes: there is no easy way to get back to a known position.

A good illustration of this is verbatim material. The way that something like LaTeX’s \verb macro works is by setting the category code for all of the ‘special’ characters to ‘other’:

\let\do\@makeother
\dospecials

where both \@makeother and \dospecials are provided by the LaTeX kernel. That works because \dospecials is defined as

\do \ \do \\\do \{\do \}\do \$\do \&\do \#\do \^\do \_\do \%\do \~

and so maps the function \@makeother to all of the ‘special’ characters. Using that, a (simplified) verbatim command looks like

\makeatletter
\newcommand*\stdverb{%
  \begingroup
    \let\do\@makeother
    \dospecials
    \@stdverb
}
\newcommand*\@stdverb[1]{%
  \catcode`#1=\active
  \lccode`\~=`#1%
  \lowercase{\let~}\endgroup
  \ttfamily
}
\makeatother

In most cases, this works fine. However, if someone makes another character ‘special’ then things go wrong:

\catcode`\+=\active
\newcommand+{oops}
\stdverb=#{+=

Of course, you could loop over every character, which would be slow for 8-bit input but with UTF-8 input that becomes impractical. Of course, you could add each active character to \dospecials, but this is dependent on everyone sticking to good practice.

This is where category code table come in. These are pre-set lists of category codes, which can be applied in one go. Heiko Oberdiek’s luatex package provides a LaTeX interface for these, meaning we can do:

\usepackage{luatex}
\makeatletter
\newcommand*\luaverb{%
  \begingroup
    \BeginCatcodeRegime\CatcodeTableOther
    \@stdverb
}
\makeatother

(reusing the same internal macro as before). now trying

\catcode`\+=\active
\newcommand+{oops}
\luaverb=#{+=

works as expected, as all of the category codes change in one go. Simple, clear and effective!

2 thoughts on “LuaTeX category code tables

  1. Sometimes you want to avoid an extra begingroup. ConTeXt provides two helper commands for such situations: pushcatcodetable and popcatcodetable, which save and restore the current catcode table. I don’t know if luatex package provides something similar.

  2. In the example, that would certainly work (there is an EndCatcodeRegime macro). In a ‘real’ case, there would normally be other stuff going on, such as font changes, and for those the group makes things a bit easier.

Leave a Reply