LaTeX \makeatletter and \makeatother and TeX Catcode

Jan. 02, 2025

A description of the command pair \makeatletter and \makeatother shows as follows1:

Synopsis:

1
2
3
\makeatletter
#   ... definition of commands with @ in their name ..
\makeatother

Use this pair when you redefine LaTeX commands that are named with an at-sign character @. The \makeatletter declaration makes the at-sign character have the category code of a letter, code 11. The \makeatother declaration sets the category code of the at-sign to code 12, its default value.

As TeX reads characters, it assigns each one a category code, or catcode. For instance, it assigns the backslash character \ the catcode 0. Command names consist of a category 0 character, ordinarily backslash, followed by letters, category 11 characters (except that a command name can also consist of a category 0 character followed by a single non-letter symbol).

LaTeX’s source code has the convention that some commands use @ in their name. These commands are mainly intended for package or class writers. The convention prevents authors who are just using a package or class from accidentally replacing such a command with one of their own, because by default the at-sign has catcode 12.

Use the pair \makeatletter and \makeatother inside a .tex file, typically in the preamble, when you are defining or redefining commands named with @, by having them surround your definition. Don’t use these inside .sty or .cls files since the \usepackage and \documentclass commands already arrange that the at-sign has the character code of a letter, catcode 11.

For a comprehensive list of macros with an at-sign in their names see2.

In this example the class file has a command \thesis@universityname that the user wants to change. These three lines should go in the preamble, before the \begin{document}.

1
2
3
\makeatletter
\renewcommand{\thesis@universityname}{Saint Michael's College}
\makeatother

Simply speaking, the command \makeatletter (“make”+“at”+“letter”) is to make symbol @ as a normal “letter”, whose TeX catcode is 11, and the command \makeatother (“make”+“at”+“other”) is to make @ as an “other character”, whose catcode is 123.

image-20250124133200610

By using \makeatletter and \makeatother, we can access or modify those LaTeX internal macros2 in the LaTeX document. This reason why we should use these tow commands to make LaTeX internal macros available in the LaTeX document is that, the symbol @ included in the LaTeX internal macros’ name is viewed as a “letter”, while in the LaTeX document @ is treated as an “other character”. As a result, if we’d like to utilize LaTeX internal macros in the LaTeX document, we should firstly convert @ into “letter”, and after use, convert it to “other character” again. A simple example of using internal macro \g@addto@macro in the LaTeX document can be found in my previous blog4.


As for TeX catcode, every input character is assigned a catcode at the first step of compilation phase, and this catcode determines how the character will be handled in the following stages5.

TeX commands commonly start with a backslash and are grouped with curly braces. Almost all of TeX’s syntactic properties can be changed on the fly, which makes TeX input hard to parse by anything but TeX itself. TeX is a macro- and token-based language: many commands, including most user-defined ones, are expanded on the fly until only unexpandable tokens remain, which are then executed. Expansion itself is practically free from side effects. Tail recursion of macros takes no memory, and if-then-else constructs are available. This makes TeX a Turing-complete language even at the expansion level. The system can be divided into four levels: in the first, characters are read from the input file and assigned a category code (sometimes called “catcode”, for short). Combinations of a backslash (actually, any character of category zero) followed by letters (characters of category 11) or a single other character are replaced by a control-sequence token. In this sense, this stage is like lexical analysis, although it does not form numbers from digits. In the next stage, expandable control sequences (such as conditionals or defined macros) are replaced by their replacement text. The input for the third stage is then a stream of characters (including the ones with special meaning) and unexpandable control sequences (typically assignments and visual commands). Here, the characters get assembled into a paragraph, and TeX’s paragraph breaking algorithm works by optimizing breakpoints over the whole paragraph. The fourth stage breaks the vertical list of lines and other material into pages.

References