You can often read about expanding a macro or when a macro does expand, sometimes about expansion. But what does this mean - in English?


TeX is based on macros and macro expansion. When you type something like \mbox{abc}, you're telling TeX to perform a possibly very complicated sequence of commands.

The case of \mbox is not really so complicated, because the definition is

% latex.ltx, line 4573:

which basically means

  1. When you find \mbox, look for an argument (which can contain \par, because of \long)

  2. Replace \mbox{<argument>} with \leavevmode\hbox{<argument>}

It's not ending here: also \leavevmode is a macro

% latex.ltx, line 492:

so TeX duly replaces \leavevmode with \unhbox\voidb@x and the final input is equivalent to having written


(in the case of \mbox{abc}). The road stops here, because \unhbox and \hbox are primitive commands that don't expand, but are passed to the next stage; \voidb@x is just an alias for a number (precisely a box register index and that register contains a void box).

The user is not concerned with these details about primitive commands: it's the programmer's responsibility to provide the user with a suite of commands for the common typesetting tasks.

A macro such as \chapter does much more than this. It checks for a possible * (unnumbered chapter) or for an optional argument (the header entry) and absorbs the mandatory argument (the chapter title). Then it executes a \cleardoublepage command for starting from a fresh page (an odd numbered one if twoside is in force), leaves some vertical space, steps the counter for the chapter number, typesets the chapter title, makes the annotation for the table of contents, leaves some other vertical space and sets things up so that the next paragraph is not indented. It does something more than this, actually, but all with a simple


input. The big advantage is that a programmer can change every detail of the action of \chapter, so that different chapter styles can be produced without changing the user's syntax.

Every time TeX finds some command defined with \def, it performs this expansion, that is, absorbing the arguments as declared in the definition and replacing the tokens so found with the replacement text of the macro. Then it starts anew trying to expand the first token it is presented with. If not a macro (or one of the expandable primitives) it performs some typesetting/bookkeeping action with it: typeset a word, step a counter, write a note in an auxiliary file and so on.

One could write a book about macro expansion and TeX/LaTeX macros. I did, actually. ;)

You never saw \def? Well, pretty normal! You probably have been told that a personal command should be defined with \newcommand and it's true! However, \newcommand is itself a macro that performs quite a lot of other tasks before, eventually, reducing the input to some well formed list of tokens for \def.

  • There is a typo in this answer, "anew" should be "a new", but since it only changes one character, I can't save the changes. – ollydbg23 Nov 27 '15 at 13:49
  • 3
    @ollydbg23 “Anew” is an adverb meaning “once more, again”. – egreg Nov 27 '15 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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