Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
2  
As for a reference, tug.org/pipermail/tugindia/2002-January/000178.html –  chl Jan 5 '11 at 20:53
    
See also stackoverflow.com/q/2498542/321973 –  Tobias Kienzler Oct 15 '11 at 7:36
40  
For 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
1  
@percusse Hah. I've been compiling things in LaTeX since 2005 and I didn't know that. –  isomorphismes Jun 4 at 3:26
1  
@isomorphismes You are right, my tone was a bit cocky indeed. But glad that the message is still covering some distance :) –  percusse Jun 5 at 19:53

1 Answer 1

up vote 243 down vote accepted

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

share|improve this answer
3  
@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 5 '11 at 21:11
5  
@Philipp: Given the audience for this question, surely multiletter macro name is a much more understandable term. –  Alan Munn Jan 5 '11 at 22:39
3  
@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 6 '11 at 1:37
3  
@Alan: +1. Maybe give an explicit example like \@title? Per 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 3 '11 at 6:00
2  
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. –  user28374 Apr 3 '13 at 2:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.