[Tex/LaTex] What do \makeatletter and \makeatother do

catcodesmacros

Many LaTeX “hacks” begin with \makeatletter and end with \makeatother. What do these commands do?

Best Answer

All characters in TeX are assigned a Category Code or "catcode". There are 16 catcodes in all, some containing just a single character, e.g. \ is (normally) catcode 0, {, catcode 1 etc. Normal characters are catcode 11; this category normally comprises all of the letter characters. The @ symbol is given the catcode of 12, which means it is not treated as a normal letter. The effects of this are that @ cannot normally be used in user document files as part of a multicharacter macro name. (All other non-letter characters are also forbidden in macro names: for example, \foo123, and \foo?! are not valid macro names.)

In LaTeX class and package files, however, @ is treated as a normal letter (catcode 11) and this allows package writers to make macro-names with @. The advantage of this is that such macro names are automatically protected from regular users: since they cannot use @ as a normal letter, there is no accidental way for a user to override or change a macro that is part of the internal workings of a package.

However, it is sometimes necessary in user documents to have access to such package-internal macros, and so the commands \makeatletter and \makeatother change the catcode of @ from 12 to 11 and 11 to 12, respectively.

In practical terms, if you need to modify a package internal macro that contains the @ symbol in its name, you will need to surround your modifications by these commands:

\makeatletter % changes the catcode of @ to 11
<your changes here>
\makeatother % changes the catcode of @ back to 12

The commands should not be used within .sty and .cls files themselves as they may conflict with the catcode changes that occurs when package and class files are loaded. For more information on this see Is it really bad to use \makeatletter and \makeatother in a package or class file?.

Related Question