7

I would like to have the following (it's a part of a large code block):

\def\C{foobar}
\def\A{}
\g@addto@macro\A{\B{\C}}

Now \show\A outputs \B{\C}

I would like to modify the 3rd line so that \show\A outputs \B{foobar}

I believe that some trick with \expandafter should help, but I'm not familiar with it. (Note: expl3 would surely help here, but I'd like to avoid it.)

Full MWE:

\documentclass{article}

\begin{document}
\makeatletter

\def\C{foobar}
\def\A{}
\g@addto@macro\A{\B{\C}}

\show\A

\makeatother
\end{document}
2
  • Are we allowed to use e-TeX? Do you want to fully expand \C or to expand it exactly once? Can we assume \C is fully-expandable inside an \edef?
    – Joseph Wright
    Commented Aug 16, 2012 at 7:50
  • @JosephWright e-TeX is fine, and \C is expandable. I should have specified it, sorry.
    – yo'
    Commented Aug 16, 2012 at 7:56

3 Answers 3

8
\documentclass{article}

\begin{document}
\makeatletter

\def\C{\D}
\def\D{foobar}
\def\A{}
\edef\next{\noexpand\g@addto@macro\noexpand\A{\noexpand\B{\C}}}
\next
\show\A

\expandafter\g@addto@macro\expandafter\A\expandafter{\expandafter\B\expandafter{\C}}
\show\A
\makeatother
\end{document}

The variants are different: the \edef will expand fully, the \expandafter only one level.

6
  • We don't know the internal structure of \C. I was about to write more or less the same answer, but would favour using a \toks or \unexpanded in the \edef case for this reason.
    – Joseph Wright
    Commented Aug 16, 2012 at 7:49
  • And there is \protected@edef if LaTeX's \protect mechanisms should be supported. Commented Aug 16, 2012 at 7:55
  • The first variant works perfectly. I should have specified that \C should be fully expanded, which is how it works. Thanks a lot! (Damn, I have to remember that trick with \noexpand.)
    – yo'
    Commented Aug 16, 2012 at 7:55
  • @JosephWright: There are lots of possible variants but the first step is to understand and decide about the expansion level. So I did not try to suppress full expansion in the first case. Commented Aug 16, 2012 at 7:58
  • @HeikoOberdiek If you do a single expansion level using a \toks or \unexpanded then \protected@edef should not be needed.
    – Joseph Wright
    Commented Aug 16, 2012 at 8:14
3

For reference, a LaTeX3 solution would look something like

\documentclass{article}
\usepackage{expl3}
\begin{document}
\ExplSyntaxOn
\tl_new:N \C
\tl_set:Nn \C { foobar }
\tl_new:N \A
\tl_gput_right:Nx \A { \exp_not:N \B { \exp_not:V \C } }
\ExplSyntaxOff

\show\A

\end{document}

(We are not told anything about \B, so I've assuming it might be expandable.) I have assumed \C is a variable, and have deliberately only expanded it to it's value: you could miss out the \exp_not:V and have x-type expansion.

2
\def\C{foobar}

\def\A{}
    
\expandafter\g@addto@macro\expandafter\A\expandafter{%
 \expandafter\noexpand\expandafter\B\expandafter{\C}}
    

or define

\def\C{foobar}
    
\def\A{}
    
\edef\X{\noexpand\B{\C}}

    \expandafter\g@addto@macro\expandafter\A\expandafter{\X}
    

You must log in to answer this question.

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