# What does “expanding” mean in (La)TeX?

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:
\long\def\mbox#1{\leavevmode\hbox{#1}}


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:
\def\leavevmode{\unhbox\voidb@x}


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

\unhbox\voidb@x\hbox{abc}


(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

\chapter{Title}


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. ;)


• 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
• @ollydbg23 “Anew” is an adverb meaning “once more, again”. – egreg Nov 27 '15 at 13:57