6

Is it possible to build a capitalized Control Sequence from an argument of lowercase string and to give it a definition? I tested two codes, but both didn't work well.

code1:

\def\makecs#1#2{%
     \expandafter\def\csname \uppercase{#1}\endcsname{#2}
}
\makecs{macroname}{definition}
\MACRONAME

code2:

\def\makeCS#1#2{%
    \expandafter\def\uppercase{\csname #1\endcsname}{#2}
}
\makeCS{macro}{def}
\MACRO

Now I understand why they didn't work well, but I have no ideas for alternative solution. What should I do to build a capitalized Control Sequence from an argument of lowercase string and to give it a definition?

agnagic
  • 535
  • I found the same question after I posted my question. I add the link of that: http://tex.stackexchange.com/questions/57551/create-a-capitalized-macro-token-using-csname – agnagic Jun 28 '15 at 04:30

1 Answers1

7

\uppercase is not expandable. Therefore it does not expand inside \csname, which then chokes on \uppercase in the name.

Also \expandafter\def\uppercase cannot expand \uppercase, thus \expandafter has no effect and the remaining \def\uppercase would change the definition of \uppercase.

There \uppercase should be given outside:

\def\makecs#1#2{%
     \uppercase{\expandafter\def\csname #1\endcsname}{#2}%          
}
\makecs{macroname}{definition}
\MACRONAME

First \uppercase is called, which converts the lowercase tokens in #1 to upper case (the other tokens are command tokens). Then \def is called with the upper case letters in place.

Step by step:

 \makecs{macroname}{definition}
 \uppercase{\expandafter\def\csname macroname\endcsname}{definition}
 \expandafter\def\csname MACRONAME\endcsname{definition}
 \def\MACRONAME{definition}
Heiko Oberdiek
  • 271,626
  • Thanks for hte useful answer. I have posted an extension of this question on http://tex.stackexchange.com/questions/349629/build-and-use-a-capitalized-cs-from-a-lowercase-string. In case you know how to solve it, I'ld love to hear it. – BartBog Jan 20 '17 at 13:08