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.

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?

share|improve this question
    
It's not at all clear why you need to expand the second argument. –  egreg Oct 23 '12 at 16:14
    
@egreg For some special purposes. See the last comment for Heiko Oberdiek answer in tex.stackexchange.com/questions/78653/…. –  Andrew Oct 23 '12 at 16:24
add comment

2 Answers

up vote 3 down vote accepted

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.
share|improve this answer
    
Note avoiding this (or at least doing all the necessary multiple expandafters and argument shuffling within the format and then hiding them) is the main motivation for latex3 xparse –  David Carlisle Oct 23 '12 at 15:06
    
Thank you very much. It works nice. Just a one more question. The first argument in \somecommand has the form eq\the\value{anothercounter}. For the correct work it should also be expanded. I was trying to put \expandafter in front of {#2} in your command \myexp. But it seems that it is to easy to be a correct solution :) –  Andrew Oct 23 '12 at 16:38
    
you mean you need the third token of the 1st argument to expand so stepping over the e and the q that would be tiresome:-) If on the other hand you know it's just of that form it is safe to put it in an edef and expand it fully not just once, I'll put it in the answer –  David Carlisle Oct 23 '12 at 16:42
    
I am, actually, playing with the function \IfRefList of the Heiko Oberdiek answer in tex.stackexchange.com/questions/78653/…. It works nice but the arguments should be expanded... –  Andrew Oct 23 '12 at 16:51
    
oh well that's easier then as you could just have fully expanded both arguments with \protected@edef no need to mess around carefully expanding #3 with multiple expandafters –  David Carlisle Oct 23 '12 at 16:58
show 1 more comment

The best thing to do would be defining the macro with switched arguments. If this isn't possible because it would require extensive changes to your documents, then use an indirect strategy:

\let\originalsomecommand\somecommand
\renewcommand\somecommand[2]{%
  \expandafter\somecommandaux\expandafter{#2}{#1}%
}
\newcommand\somecommandaux{\originalsomecommand{#2}{#1}}

so switching the arguments twice.

share|improve this answer
    
Thank you. I will play with this idea. –  Andrew Oct 23 '12 at 17:03
add comment

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.