7

I'm trying to use \csname to execute a command whose name depends on a variable. If I use \csname mygametitle\tmp \endcsname it works, but if I use instead of \tmp the argument #1 it fails:

enter image description here

\documentclass{article}

\begin{document}

\def\mygametitleABC{Title for ABC} \def\test#1{% \csname mygametitle#1 \endcsname% } \def\testb#1{% {% \def\tmp{#1}% \csname mygametitle\tmp \endcsname% }% }

Version 1: \test{ABC}

Version 2: \testb{ABC}

\end{document}

tobiasBora
  • 8,684

1 Answers1

13

In

\def\testb#1{%
  {%
    \def\tmp{#1}%
    \csname mygametitle\tmp \endcsname%
  }%                      ^^^ irrelevant
}

the space after \tmp is gobbled by the usual rules, and \csname mygametitle\tmp \endcsname is equivalent to \csname mygametitle\tmp\endcsname. But in

\def\test#1{%
  \csname mygametitle#1 \endcsname%
}%                    ^^^ very very relevant

you're adding an extra space. You should use

\def\test#1{%
  \csname mygametitle#1\endcsname
}%  

Note that the % after the \endcsname are also not necessary.

campa
  • 31,130
  • Ouuu that's tricky, thanks! – tobiasBora Nov 19 '21 at 15:10
  • 2
    @tobiasBora Spaces are ignored after numbers only when TeX is scanning for further digits, so e.g. \csname foo\the\count0 \endcsname and \csname foo\the\count0\endcsname are indeed the same. But since there are only 9 possible parameters TeX does not have to scan for further digits after #1, so a space after that is not ignored. – campa Nov 19 '21 at 15:25