0

I need to dynamically create the name of a macro, where part of the name is based on the argument which needs to be capitalized beforehand. The following MWE illustrates my intention:

\documentclass{article}

\usepackage{xstring}

\newcommand{\CommandOne}{bla}
\newcommand{\CommandTwo}{more bla}

\newcommand{\foo}[1]{
  \StrLeft{#1}{1}[\strHead]%
  \StrGobbleLeft{#1}{1}[\strTail]%
  \csname Command\MakeUppercase{\strHead}\strTail\endcsname%
}

\begin{document}

\foo{one}
\foo{two}

\end{document}

However, this produces

! Missing \endcsname inserted.

upon compilation. Why, and how do I fix it?

3
  • 1
    That's because \MakeUppercase isn't expandable, I guess. You could try \tl_upper_case:n from expl3 (I haven't tested your MWE and my suggestion though).
    – yudai-nkt
    Commented Jan 4, 2018 at 12:26
  • @yudai-nkt: Yes, that should work
    – user31729
    Commented Jan 4, 2018 at 13:36
  • the above comments show what you should do, the reason for the error is that the xstring commands are as if you had gone \csname\def\tmp{ZZZ}\tmp\endcsname which does not work, you can not do definitions inside csname, you need \def\tmp{ZZZ}\csname\tmp\endcsname Commented Jan 4, 2018 at 15:35

0

Browse other questions tagged .