Is there a non-expl3 expandable function for reversing the order of expansion of an arbitrary number of tokens or macros? For example,

  \reverseexpansion<n>\a\b\c\d\e ... \m

where <n> is the number of leading tokens whose expansions are to be reversed.

Also, an expandable version of \expandallonce, which expands all the macros in its argument precisely only once, will be useful:

  \expandallonce{\a\b\c\d\e ... \n}

I should add that the tokens may contain balanced braces/arguments. Say,

\reverseexpansion 3\a{\b\c}\d

The braces aren't expandable, but should be preserved after the run.

Actually, I already have awkward unexpandable solutions for the two tasks. Rather than spend time finding expandable solutions, I thought someone would have worked them out in the past.

  • Can you be more clear about \reverseexpansion? How does it know when to stop reading arguments? And what do you mean ‘reverse the order of expansion’? A more concrete example would help. – Will Robertson Feb 16 '11 at 7:23
  • 1
    You have a point. I left out the <number> of arguments to reverse. The number of arguments will be specified as the first argument. So we may call \reverseexpansion2\a\b\c to reverse the expansion of \a and \b, leaving \c untouched in that run. I hope this helps. – Ahmed Musa Feb 16 '11 at 7:37
  • @Ahmed So \reverseexpansion2\a\b\c is equivalent to \expandafter\a\b\c and \reverseexpansion3\a\b\c is \ea\ea\ea\a\ea\b\c ? – Will Robertson Feb 16 '11 at 7:49
  • @Will: Yes, but the count can get messy after seven \expandafter's. I know there is a formula relating the number of \expandafter's to the order of reversing, but listing out all the needed \expandafter's is inefficient. – Ahmed Musa Feb 16 '11 at 7:57
  • @Ahmed And what is \reverseexpansion4\a{\b\c}\d supposed to result in? – Will Robertson Feb 16 '11 at 7:57

EDIT: At the end of this post, I include the relevant part of ULcase.sty so that this post is self contained.

In the code below I use ULcase. I think that the comments in the code should be enough to understand it.

The easiest is \expandallonce, although what do you want when a token takes an argument? (Currently, it will break.) We simply define a capitalization table which by default expands once, and we read through the tokens.

% Code based on the extended Upper- and Lower-casing code found
% in the ULcase package.
\input ULcase.sty\relax

% ============ Table |expandallonce|
% Just as uppercasing is changing "a->A", "b->B" etc, and applying
% |\donothing| to all other tokens, we define the table of case change
% |expandallonce| to expand the token once before outputting it. This 
% will fail in case the token takes an argument (I don't know what the 
% expected behaviour would be, anyways.)

% The default action is to output the given token expanded once.

% Braces do nothing special, just as in the ULnil table.

% ===== Tests

\long\gdef\a{\expandallonce{\0{{} {\2} {\3}\0\2}\2}}

For \reverseexpand, we first insert code from this question on building a multi-\expandafter before every token. The next step of expansion will trigger the multi-expansion, which does what you want. Note that the macros we expand can happily take all sorts of arguments, because we did not leave anything in the input stream after the token we are currently expanding.

% Code based on the extended Upper- and Lower-casing code found
% in the ULcase package.
\input ULcase.sty\relax

% ============ Table |reverseexpand|
% With the definition below, one step of expansion on
% |\romannumeral\eatwice| is the same as two steps of |\expandafter|.

% We insert |\romannumeral\eatwice| in front of every token,
% and add |\empty\empty| at the end, to stop the  extra |\expandafter|.
% The full expansion requires 5 steps.

% The default action is to output the given token plus the extra text.

% The chain of |\expandafter| triggers the full expansion of 
% |\romannumeral\UL_to_case_aux:nn{#1}{#2}|, which adds the 
% tokens |\romannumeral\eatwice| before each token in the group.

% ===== Tests


\long\def\a{\reverseexpand{\double\foo{ \4\6}\8}}

The relevant part of ULcase (should be put at the top of the other pieces of code, but that's not supposed to be the key point).


% ======================== Generic macros
% Note on LaTeX3's naming convention: letters after ":" in macro names
% indicate the arguments that the command takes. 
%   "n" = braced
%   "N" = single token
%   "w" = weird
% and plenty of others.
% A few standard commands to manipulate arguments

% What expl3 calls "quarks", useful for |\ifx| comparisons.

% Two tests 


% ======================== Main command: |\UL_to_case:nn|
% Usage:       |\UL_to_case:nn{<table>}{<text>}|
% Expands in:  2 steps.
\long\gdef\UL_to_case_aux:nn#1#2{-`\0% almost stops \romannumeral
  \UL_brace_check:nw{#1}#2{\q_mark} \q_stop\UL_to_case_end:n{}}%

% |\UL_to_case_output:n| appends its argument to the argument of
% |\UL_to_case_end:n|.
\long\gdef\UL_to_case_end:n#1{ #1}
% And |\UL_to_case_end:n| expands to 
% - a space, which stops the expansion of |\romannumeral-`\0|,
% - followed by its argument, which is the result we want.

% First, we check whether the next token is a brace. 
% If there is a brace, we might have reached {\q_mark}.
  \expandafter\UL_if_detok_qmark:wTF \detokenize{#2 \q_mark}\q_stop{% 
    % Note the space before \q_mark!
  }{% Otherwise, we have a brace group, and we can act on it.
    \csname UL_table_#1_braces\endcsname{#1}{#2}%

% Then check whether the next token is a space.
\long\gdef\UL_space_check:nw#1#2 {%
  {\UL_convert_token:nn{#1}{ }}%
  {\UL_convert_token:nn{#1}#2 }% put the space back!

  \ifcsname UL_table_#1_\detokenize{#2}\endcsname%
  {\csname UL_table_#1_\detokenize{#2}\endcsname}%
  {\csname UL_table_#1_default\endcsname{#2}}%
  \UL_brace_check:nw{#1}% Do the next token.

% For tests.
\long\gdef\expandonce#1{% redefines #1 as #1 expanded once.
  • I'm slightly wary of a solution using package code which is not on CTAN. (More generally, I wonder if tex-core questions should always have fully stand-alone answers: one for meta.) – Joseph Wright Feb 16 '11 at 17:54
  • See my meta post: meta.tex.stackexchange.com/questions/958 (I have no problem with either answer, by the way) – Joseph Wright Feb 16 '11 at 18:01
  • @Joseph: yes, I hesitated, but I really don't feel like rewriting the same code. I can extract the 60 or so lines that are required, and put them here if it is better. – Bruno Le Floch Feb 16 '11 at 18:57
  • @Bruno: Many thanks for your effort. I will study your solution. It contains something I can build on. – Ahmed Musa Feb 16 '11 at 19:02
  • @Bruno: Please can you provide us with a web link for your solution? It will be easier to copy from there. – Ahmed Musa Feb 16 '11 at 19:11


I'm going to pass on this since I think what you're asking would be approximately as difficult or perhaps harder than the answers given to this question, and it's not clear to me if any of these expansion abstractions are actually useful in practise. (And I'm out of time.)


The idea is to map over the list token by token and apply \unexpanded\expandafter{#1} to each. But you get an expl3 solution anyway, because I'm tired of re-programming the same code over and over :)

  \tl_map_function:nN {#1} \tl_nested_exp_not:n
\cs_set:Nn \tl_nested_exp_not:n {
  \tl_if_single:nTF {#1}
    { \exp_not:o #1 }
    { { \expandallonce {#1} } }

This is not completely robust if you want to put arbitrary TeX code inside \expandallonce, but I assume that this is not a problem. (E.g., you can't put conditionals inside.) Note that expl3 currently doesn't have a mapping function that nests through braces so (in the updated answer) I've achieved a similar effect with recursion.

Note this nested mapping does respect braces unless the braces surround a single token, such as in abc{d}efg. I'm not aware of any way around this problem and have the code remain expandable. (Perhaps with \detokenize and \scantokens it's possible, but that's too fragile for my liking.)

  • Joseph Wright has ever suggested this 'non-robust' expl3 solution to me, but I am not yet a convert to LaTeX3. I am not sure why I am cold to LaTeX3? As Joseph ever said here, I do think something like etoolbox or etextools would have been preferred to LaTeX3 by some of us. Please no one should resume another discussion on LaTeX3 here. :) The question isn't about LaTeX3. – Ahmed Musa Feb 16 '11 at 7:53
  • 3
    @Ahmed — I'm posting expl3 code because it's quick for me to write and demonstrates the algorithm. It's not too hard to translate if you want to use a different programming layer. – Will Robertson Feb 16 '11 at 7:56

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.