[Tex/LaTex] How to expand an argument defined with \csname…\endcsname

expansionmacros

Similar to the situation described in Expand after all that stuff?, I have a macro looking like

\somecommand{Some Argument}{\secondarg}.

I also need to expand the argument \secondarg before using it in \somecommand. The solution with etextools,

\usepackage{etextools}
\expandnext{\somecommand{Some Argument}}{\secondarg},

proposed by Martin Scharrer, is fine for me.

However, my problem is that instead of \secondarg I have the construction defined as

\expandafter\newcommand\csname secondarg\the\value{mycounter}\endcsname{Second argument 1}.

If I simply replace \secondarg by \csname secondarg\the\value{mycounter}\endcsname, the result looks such as the argument is not expanded. Is there an easy way to expand it correctly, maybe by rewriting the example with etextools?

Best Answer

replace

\expandafter

by

\expandafter\expandafter\expandafter

(you usually need 2^n-1 \expandafter for some value of n )


To get the three \expandafter in the right place you could do:

\newcounter{mycounter}


\def\somecommand#1#2{%
\def\a{#1}%
\def\b{#2}%
\show\a
\show\b}

\expandafter\def\csname secondarg\the\value{mycounter}\endcsname{hello}

\def\myexp#1#2#3{%
\toks0{#1{#2}}%
\the\toks0\expandafter\expandafter\expandafter{#3}}

\myexp
\somecommand{Some Argument}{\csname secondarg\the\value{mycounter}\endcsname}

\stop

which produces

> \a=macro:
->Some Argument.
\somecommand ...>\def \a {#1}\def \b {#2}\show \a 
                                                  \show \b 
l.17 ... secondarg\the\value{mycounter}\endcsname}

? 
> \b=macro:
->hello.

showing that hello was passed as argument.


Or for the modified version requested in comments:

\newcounter{mycounter}
\newcounter{anothercounter}
\setcounter{anothercounter}{42}


\def\somecommand#1#2{%
\def\a{#1}%
\def\b{#2}%
\show\a
\show\b}

\expandafter\def\csname secondarg\the\value{mycounter}\endcsname{hello}

\def\myexp#1#2#3{%
\toks0\expandafter\expandafter\expandafter{#3}%
\edef\tmp{\noexpand#1{#2}{\the\toks0}}%
\tmp}

\myexp
\somecommand{eq\the\value{anothercounter}}{\csname secondarg\the\value{mycounter}\endcsname}

\stop

which fully expands #2 and expands #3 twice producing

> \a=macro:
->eq42.
\somecommand ...>\def \a {#1}\def \b {#2}\show \a 
                                                  \show \b 
l.20 ... secondarg\the\value{mycounter}\endcsname}

? 
> \b=macro:
->hello.
Related Question