Understanding macro expansion
I get the occasional e-mail asking me to explain who some complex macro works. I usually answer by trying to show how I go about understanding things. There are a few simple steps:
- Start from the definition of the first macro you want to understand.
- From the definition, work out what arguments it will take (easy if they are not delimited).
- Replace every parameter in the definition by what you picked up in (2). Don’t worry if things seem like they will change part way through: TeX has to fill in every
#2, etc. at the point of expansion.
- Now work through the expansion systematically. If you come to another macro, replace it by it’s expansion (remembering any arguments), if you hit a primitive, work out what it will do, etc.
- It’s important not to get confused by things that are redefined themselves during the expansion. TeX uses whatever the current meaning is when it expands/executes a token. So it’s often useful to keep notes on definitions, etc., and check against them.
Usually, with a bit of care and some notes, enlightenment will dawn.