Many LaTeX “hacks” begin with \makeatletter and end with \makeatother. What do these commands do?
-
10As for a reference, http://www.tug.org/pipermail/tugindia/2002-January/000178.html – chl Jan 05 '11 at 20:53
-
334For the absolute beginners; the commands should be read as Make @ symbol a letter and Make @ symbol an "other".It's NOT Make when you encounter a letter and Make when you encounter others. – percusse Mar 14 '13 at 10:54
-
19@percusse Hah. I've been compiling things in LaTeX since 2005 and I didn't know that. – isomorphismes Jun 04 '14 at 03:26
-
71How bizarre and obscure. I love what LaTeX does, but the more I learn it, the more I realize what a crazy hack it is. – Dov Jul 14 '14 at 12:05
-
See also http://tex.stackexchange.com/questions/11750/how-bad-is-slack-makeatother-discipline-really – Charles Stewart Jul 01 '16 at 09:07
-
11hellish language. why do people say it's easy – Christian Chapman Aug 22 '19 at 04:57
-
1@percusse lol, that's the clearest answer, I got it as soon as I read this. – C.K. May 08 '20 at 02:59
-
@ChristianChapman, Who's been saying it's easy?? – Paul Wintz Nov 02 '22 at 05:44
-
Related question: macros - Why doesn't \makeatletter work inside \newcommand? - TeX - LaTeX Stack Exchange – user202729 Mar 04 '23 at 19:35
1 Answers
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?.
- 347
- 218,180
-
3Good explanation. Just one minor point: You can use
@in normal text, you just can't use it as part of a macro name. – Caramdir Jan 05 '11 at 21:03 -
18@Caramdir: Not quite true.
\@is a perfectly good macro. It's what Knuth calls a control symbol. If the category code of @ is 11, then\@becomes a control word. – TH. Jan 05 '11 at 21:11 -
@TH. is is okay to say „can't use it as part of a multi-letter macro name”? – Caramdir Jan 05 '11 at 21:16
-
-
1@Alan: The category numbers 0 to 15 result in 16 (!) catcodes. The TeXbook (p. 37) confirms this. ;-) – lockstep Jan 05 '11 at 21:38
-
6@lockstep: Damn computer scientists starting everything at 0. :-). Fixed. Thanks. – Alan Munn Jan 05 '11 at 22:15
-
2
-
20@Philipp: Given the audience for this question, surely multiletter macro name is a much more understandable term. – Alan Munn Jan 05 '11 at 22:39
-
6@Alan: We [computer scientists] do like to confuse things. =) In a sense, there are actually 17 catcodes. It's really not relevant for this question, but in the context of
\ifcat, control sequences (which includes control words, control symbols, and control spaces) have category code 16. See TeX by Topic, Section 13.2.2. – TH. Jan 06 '11 at 01:37 -
6@Alan: +1. Maybe give an explicit example like @title? Per http://www.tug.org/pipermail/tugindia/2002-January/000178.html. To summarize, a normal user is not supposed to do @title but a package author is allowed, so to speak, and \makeatletter makes @title 'legal'. – Faheem Mitha Mar 03 '11 at 06:00
-
72Thank you so much for that explanation. The way the command is phrased, I thought it was telling the compiler to make something execute at a specific letter. It's literally: "make the "at" symbol a letter. It's actually pretty funny. – Apr 03 '13 at 02:41
-
4
-
4@MickG In principle, the catcode of anything can be changed, but in practice, it's generally going to cause trouble unless you really know what you're doing. It's sometimes useful to change catcodes within an environment but again, without a good amount of knowledge of how TeX works, it's not recommended. – Alan Munn Dec 28 '13 at 17:29
-
1So I could do
\makebackslashletter, right? And how would I get back? Just for curiosity :). – MickG Dec 28 '13 at 18:44 -
14@MickG No, there is no general command
\make<foo>letter. The\makeatlettercommands are one off. – Alan Munn Dec 28 '13 at 18:49 -
2So it's like
sudo?Package writers have set things up such that users can't accidentally break things, but if you know that you really do want to do something (i.e. you really do need to change a macro that's internal to the workings of a package) then there's a way to say obey me?
– owjburnham Jul 07 '17 at 14:43 -
4@owjburnham Well at a very broad level yes, but in reality not really. The @ commands are merely a reserved namespace. At best it's security by obscurity. – Alan Munn Jul 07 '17 at 15:14
-
1There is a typo "cannnot", but I can't change this, because edits must be at least 6 characters. Can any administrator change this? – ollydbg23 Dec 30 '17 at 13:00
-
25Gosh. All this time, I thought that
\makeatlettermakes a tletter, and\makeatothermakes a tother. :) – Dec 30 '17 at 18:47 -
@AlanMunn Should
@usages in\@ne,\tw@,\sixt@@n,\m@ne,\insc@untor\e@ch@ckbe considered as indicators of “reserved namespace” :-D Certainly, the exception does prove the rule! – audeoudh Dec 06 '19 at 10:41 -
@ryolait I don't see why not. These are used in the LaTeX kernel and it makes sense that they are reserved. – Alan Munn Dec 06 '19 at 17:31
-
@AlanMunn , so could we use @title without \makeatletter ? I understood, we use the \makeatletter just when the macro be \title@title, is that true? – Mamoun Mohammed Oct 08 '20 at 08:54
-
1@مأمونعماد No. Any macro containing @ anywhere in its name cannot be used in a user document unless you use
\makeatletter. Since these commands are intended to be internal commands you shouldn’t change them unless you know what you are doing. – Alan Munn Oct 08 '20 at 11:36