This question was inspired by comment banter in this question. See title.
(Sorry, this gets more of an essay than an answer.)
TH claims that "There's not really a good way to do this that takes arguments." Well, I thought the same, but Philippe's answer to the original question of Yiannis proved me wrong. I've used his ingenious
The crucial feature of the
Explanation, starting in the end: The
to first expand
Here's the new implementation of
Only a short explanation: In the loop I construct macros
to be expanded 2n times, which in turn executes the desired n expansions. The result of the second step is just the control sequence
For further illustration, here's how Philippe's example looks like with
For completeness, here's my old inefficient implementation for 0-6 expansions:
|show 12 more comments|
(I apologize for the length of this answer.)
There's not really a good way to do this that takes arguments. There are two reasons for this. The first is that it adds more tokens that have to be skipped over. For example, consider
The second reason has to do with implementing this. Consider
This is the obvious first choice, but that doesn't really work; consider
You can sort of use
But it's not extremely easy to use or follow.
The first line expands
This can be simplified somewhat by not using
For example, guess what
[Again, not really answering the question but posing an alternative.]
Nowadays, rather than attempting to repeat
Here's an example:
As well as
TOC: Explanations ("Two user commands", "How it works"). Code ("Implementation", "Tests").
Two user commands
The first expansion of
These are especially useful when we want to expand several times a very specific token which is buried behind many others. Example: after
Note: if you really need
How it works
Now, as I said before,
This is in fact how the construction ends. Let us quickly look at the definition of
There is no limit on the number of expansions.
For comparison, the "Hello World!" example
now uses 32
Some experimentation tells me that the number of
|show 8 more comments|
We first give a direct answer to the question. Later we give another, in our opinion much nicer, way to reverse the expansion of tokens.
The direct answer is given by the following code. In the first macro we make an expandable countdown counter which is used in the macro
In this definition we use a trick described in Forcing full expansion. An expansion of
Now we define some macros to show the order of expansion (see also the answer by Hendrik Vogt of Jan 2, 2011)
This gives in the output window
exactly what we want. Note that for every extra token that we want to expand in reversed order, we need to increase the counter by 4. This seems not to much, but essentially, at every step the number of
For another way to reverse the order of expansion we use the same trick, but now directly. Note that for reversing the order of expansion of n tokens, we only need 4(n-1)
which again gives the desired result. As inserting the tokens
may obscure the TeX code, we can at the cost of more (namely 2n-5))
which again gives the desired result.