Keep TeX Catcode Changes Local to Avoid Silent Scope Leaks
TeX catcode assignments are global unless wrapped in a group. Learn how to scope @ and other catcode changes so they don't break unrelated macros.
24 Jun 2026, 10:01 UTC

The problem: a catcode change that never ends
You add a line to your LaTeX package so internal macros can use @ in their names:
\catcode`\@=11
\def\@my@internal{...}
You forget to switch @ back. For the rest of the job, @ is a letter. Another package expects @ to be ordinary punctuation. A macro name breaks, a comment swallows half a paragraph, or math parsing fails—and the error points at code far from your catcode line.
The useful takeaway: catcode assignments are global by default. Wrap them in a TeX group, and never assume a changed character stays changed only for the next macro.
What a catcode change actually does
TeX reads your source one character at a time and immediately turns each character into a token. The decision depends on the character's current category code, or catcode. A letter (catcode 11) becomes a letter token; the same character with catcode 12 becomes an 'other' token. The @ character is normally catcode 12 in LaTeX, so control sequence names cannot contain it. \makeatletter sets @ to catcode 11; \makeatother returns it to catcode 12.
This matters because tokenization happens at read time. Once TeX has turned @ into a token, that token keeps its classification forever. Changing the catcode later cannot rewrite tokens that already exist.
The scope leak
A catcode assignment persists until the end of the job unless it appears inside a TeX group (\begingroup ... \endgroup or { ... }). If you run \catcode`\@=11 at the top level, @ stays a letter for every macro and package loaded afterward. That is a scope leak: a local fix becomes a global condition.
The same leak applies to more dangerous characters. Setting % to catcode 12 stops it from starting comments. Setting } to catcode 12 can make TeX unable to find the end of a group. These changes are easy to make and hard to notice until unrelated code breaks.
Worked example: define an internal macro safely
Suppose you are writing a package file and need a helper macro named \@my@helper. You need @ to be catcode 11 while TeX reads the definition.
Run this in a file processed by pdftex or latex (no special permissions required; use a scratch directory):
\showthe\catcode`\@
\begingroup
\catcode`\@=11
\showthe\catcode`\@
\endgroup
\showthe\catcode`\@
\bye
If you use LaTeX instead of plain TeX, replace \bye with \end{document} and wrap the code in a document environment.
Expected check: the log should report the normal catcode, then 11 inside the group, then the normal catcode again after \endgroup. If the final value is still 11, the group was missing or misplaced.
Risk: omitting \endgroup leaves @ as a letter for the rest of the job. Experimenting with % or } instead of @ can make the input file unreadable if the group is omitted.
Limitation: grouping does not rewrite old tokens
Grouping restores the catcode assignment, but it does not retroactively change tokens already read. If a line containing % was tokenized while % was catcode 14 (comment), the comment token is already fixed. Changing % to catcode 12 afterward will not un-comment that line. The same rule applies to code generated inside a macro and read back later: it uses the tokens from the original read, not the current catcodes.
Actionable closing
Always write catcode changes as a scoped block:
\begingroup
\catcode`\CHAR=VALUE
% your code here
\endgroup
In LaTeX, prefer \makeatletter and \makeatother inside a group when you need package internals. After any catcode experiment, check \showthe\catcode`\CHAR to confirm the value is back where it belongs.
If you suspect a leak, bisect the document: comment out halves until the behavior returns, then inspect the catcode assignments in the remaining half.