8

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

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}
  • 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. – yo' Jan 27 '12 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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