Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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 Aug 16 '12 at 7:50
    
@JosephWright e-TeX is fine, and \C is expandable. I should have specified it, sorry. –  yo' Aug 16 '12 at 7:56

3 Answers 3

up vote 7 down vote accepted
\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.

share|improve this answer
    
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 Aug 16 '12 at 7:49
    
And there is \protected@edef if LaTeX's \protect mechanisms should be supported. –  Heiko Oberdiek Aug 16 '12 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' Aug 16 '12 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. –  Ulrike Fischer Aug 16 '12 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 Aug 16 '12 at 8:14

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.

share|improve this answer
\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}
    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.