So I've read a few times about this, the “moving arguments” and all that. But what does it really mean? Could someone explain in a few words what is going on at the TeX level, what is the difference between these two kinds of commands? Furthermore, when do we need to use \protect and why?
|
|
||||
|
The key concept here is that, when TeX handles its input, it is doing two distinct things, called expanding and executing stuff. Normally, these activities are interleaved: TeX takes a token (ie, an elementary piece of input), expands it, then executes it (if possible). Then it does so with the next token. But in certain circumstances, most notably when writing to a file, TeX only expands things without executing them (the result will most probably be (re-expanded and) executed later when TeX reads the file back). Some macros, for proper operation, rely on something being properly executed before the next token is expanded. Those are called "fragile", since they work only in the normal (interleaved) mode, but not in expansion-only contexts (such as "moving arguments" which often means writing to a file). That's the general picture. Now let's give a few more details. The distinction between expansion and execution is somewhat arbitrary, but as a rule of thumb:
For example, macros are expandable (TeX is going to read their replacement text next), Now, consider a macro In expansion-only context, TeX will try to expand This was the easy (read: TeXnical, but well-defined) part of your question. Now, the hard part: when to use So, my advice is: when you see a weird error happening in or near a moving argument, try |
|||||||||||
|
|
This question is perhaps best answered by an example. Consider the fragile command
Now imagine that you include
As you can imagine (try it if you must), actually executing To find out how |
|||||
|
|
The key concept here is expansion. I'll take as an example a hypothetical function
When LaTeX processes the
The When you use The macro This is good, but a better method is e-TeX's
Macros defined in this way are not expanded inside an |
||||
|
|
\protected@edefwhich cause\protect\fooin the replacement text to expand to itself in contrast to\edefwhich would (normally) cause\protect\footo essentially expand to\relaxfollowed by the expansion of\foo. – TH. Oct 31 '10 at 8:27