3

I've browsed every question I can find on \csname and I see lots of code that uses it inside a macro and passes the arguments inside to be part of the name, but only when defining a macro. I need to use it for calling a macro.

\documentclass{book}
\newcommand{\other}{pass}
\newcommand{\test}[1]{\csname #1 \endcsname}
\newcommand{\anothertest}[1]{\expandafter\csname #1 \endcsname}

\begin{document}

\test{other}
\anothertest{other}
I see nothing.

\end{document}

This should be calling \other twice, yes? But when I compile it with pdflatex, it isn't working. What am I doing wrong?

Alternatively, is there another way to accomplish the following: My code defines a series of \callme commands: \callmeFrank, \callmeAnne. Inside my text, I want to be able to dynamically call those from within another macro (that does a lot of other stuff too). \newcommand{\BigFunction}[1]{dostuff\csname\callme#1\endcsname}do more stuff}. Obviously, this isn't working for me, as shown by the MWE above.

  • 1
    The two commands do the same thing, because \expandafter in the second one just tries to expand the first token in #1, which in the example happens to be o (so nothing happens). You see nothing because when \csname ...\endcsname is used and the resulting command doesn't exist, it's defined to be the same as \relax, which is the case here because \other isn't defined. – egreg Sep 14 '14 at 14:47
  • You probably want \csname callme#1\endcsname, so when #1 is Frank you get the same as if you had typed \callmeFrank. – egreg Sep 14 '14 at 14:49
  • Except that the resulting command is defined. I defined \other. As for your second comment, I tried \csname callme#1 \endcsname and it was giving nothing. Thus, my original question. – JKreft Sep 14 '14 at 14:59
  • 2
    #1 \endcsname has a space, thus you are calling \other• where is a space in the macro name. – Manuel Sep 14 '14 at 15:16
6
\documentclass{book}
\newcommand{\other}{pass}
\newcommand{\test}[1]{\csname #1\endcsname}
\newcommand{\anothertest}[1]{\expandafter\csname #1\endcsname}

\begin{document}

\test{other}
\anothertest{other}
I see \emph{something}.

\end{document}

You have a space in #1 \endcsname which means that you are calling macros with a trailing space in the name. You defined \other, not \other•. Then it results in a \relax. If you remove the space it should compile fine.

|improve this answer|||||
  • 4
    the \expandafter isn't doing anything here – David Carlisle Sep 14 '14 at 15:40
  • Manuel: Thanks. I saw spaces in other examples, and emulated them. – JKreft Sep 14 '14 at 15:54
  • @JKreft As David says, the \expandafter is useless there, since everything between \csname and \endcsname is expanded before creating the token. What did you want to achieve? – Manuel Sep 14 '14 at 15:56

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.