961

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

yo'
  • 51,322
Caramdir
  • 89,023
  • 26
  • 255
  • 291

1 Answers1

905

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?.

Alan Munn
  • 218,180
  • 3
    Good 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
  • @TH @Caramdir: Right. I'll fix that in the answer. Thanks. – Alan Munn Jan 05 '11 at 21:18
  • 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
    The TeXnical term for "multiletter macro name" is "control word." – Philipp Jan 05 '11 at 22:18
  • 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
  • 72
    Thank 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
    Are there other possible catcode changes? – MickG Dec 28 '13 at 14:58
  • 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
  • 1
    So 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 \makeatletter commands are one off. – Alan Munn Dec 28 '13 at 18:49
  • 2
    So 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
  • 1
    There 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
  • 25
    Gosh. All this time, I thought that \makeatletter makes a tletter, and \makeatother makes a tother. :) –  Dec 30 '17 at 18:47
  • @AlanMunn Should @ usages in \@ne, \tw@, \sixt@@n, \m@ne, \insc@unt or \e@ch@ck be 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