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 have a macro \mymacro@i that I want to "feed" with the expansion of another \test. With \expandafter\mymacro@i\test, it works.

Now, the \test macro can be defined by using \csname, say a /test macro. I can't find the combination of \expandafter to make it work the same way as with \test. How can I do?

\documentclass{standalone}
\makeatletter
\expandafter\def\csname /test\endcsname{%
  \foo{bar}}
\def\mymacro#1{%
  \expandafter\mymacro@i\csname/#1\endcsname}
\def\mymacro@i\foo#1{%
  \def\temp{#1}%
  \show\temp}
\mymacro{test}
% Error: Use of \mymacro@i doesn't match its definition.
%
\def\test{\foo{bar}}
\def\mymacro#1{%
  \expandafter\mymacro@i\test}
\def\mymacro@i\foo#1{%
  \def\temp{#1}%
  \show\temp}
\mymacro{test}
\begin{document}
\end{document}
share|improve this question

1 Answer 1

up vote 9 down vote accepted

You need to expand it twice now, once \csname /#1\endcsname to \/#1 and the expand that macro. You need to add therefore two more \expandafter. The first one will expand the third \expandafter before the second, i.e. allows the third to expand the \csname construct, before the second one expands the resulting macro before \mymacro@i.

\def\mymacro#1{%
  \expandafter\expandafter
  \expandafter\mymacro@i\csname/#1\endcsname}
share|improve this answer
    
Thanks Martin! I'm quite sure I did try that. There must have been something wrong inside the csname that expanded to relax causing the mismatch definition. –  cjorssen Jan 26 '12 at 22:55
1  
I consider the construction with \let to be clearer. I think in this case it would be \expandafter\let\expandafter\reserved@a\csname/#1\endcsname \expandafter\mymacro@i\reserved@a or something different. I always get very confused with nested \expandaafter's. –  tohecz Jan 27 '12 at 0:15

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.