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

\def\C{foobar}
\def\A{}


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{}

\show\A

\makeatother
\end{document}

-
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

\documentclass{article}

\begin{document}
\makeatletter

\def\C{\D}
\def\D{foobar}
\def\A{}
\next
\show\A

\show\A
\makeatother
\end{document}


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

-
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
\def\C{foobar}
\def\A{}
\expandafter\noexpand\expandafter\B\expandafter{\C}}


or define

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

-

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.

-