In OpTeX there is a macro \addto which adds some text to the definition of another macro. It is defined like this: \long\def \addto #1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}. Why are all those \expandafters needed and especially what does the last one exactly do? As far as I understand, the last \expandafter takes away the {, then it expands #1 and reinserts {<expansion of #1> into the token list. What about #2? How ist it expanded?

  • 1
    #2 is not expanded, it is simply appended to the content of #1. Commented May 26, 2022 at 16:03
  • after \def\foo{abc}, \addo\foo{xyz} is \expandafter\def\expandafter\foo\expandafter{\foo xyz} which is \def\foo{abcxyz} Commented May 26, 2022 at 16:10

1 Answer 1


The second argument is not expanded: let's follow


The first definition is performed, then TeX expands \addto and it gets

\expandafter\def\expandafter\aaa\expandafter{\aaa y}

(the braces are stripped off by rule and there's no space after \aaa). This becomes


as you wanted. If instead of {y} you had something like \bbb and you want to merge the contents of both \aaa and \bbb you need to do it like as follows


After this the macro \aaa would expand to xy.

  • 1
    The last example is usable for cases when we want only first expansion of \aaa and \bbb to be saved to \aaa. Another case should be: we accept full expansion of \aaa and \bbb saved to \aaa. Then we can use simply \edef\aaa{\aaa\bbb}. If there is \def\aaa{x} and \def\bbb{y} (as in the mentioned example) then the result is the same.
    – wipet
    Commented May 26, 2022 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .