Create a capitalized macro token using \csname
You have to isolate the first token in #1
from the rest and uppercase it; the fact that \uppercase
doesn't expand anything and puts back the token list into the input stream after its operation can be exploited in the following way:
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\gdef\csname #1}#2\endcsname{the macro #1#2 expanded}%
}
\mycommand{abc}
\show\Abc
Of course an empty argument to \mycommand
will result in an error.
If you want to provide a definition, say
\mycommand{abc}{Something else}
to be equivalent to \def\Abc{Something else}
, just leave out the tokens after \endcsname
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\gdef\csname #1}#2\endcsname
}
\mycommand{abc}{Something else}
\show\Abc
The command \uppercase
is a bit stranger than other TeX primitives. Indeed the <general text>
it requires as argument first does a travel down TeX's stomach to be "regurgitated" after each character token has been transformed using the \uccode
vector: if a character has positive \uccode
, TeX will transform it into that character (the category code is unchanged). So, for instance, TeX is setup so that \uccode`a=`A
and so an a
becomes an A
after regurgitation. Non character token are left unchanged and no expansion is performed.
Here's the working in slow motion (first version).
\mycommand{abc}
Here#1
isabc
\mycommandaux abc\relax
Here#1
isa
and#2
isbc
(argument#2
is delimited by\relax
, while#1
is undelimited, so the only first token is grabbed)\uppercase{\expandafter\gdef\csname a}bc\endcsname {the macro abc expanded}
Tex executes the\uppercase
and puts back\expandafter\gdef\csname A
in the input stream.
\expandafter\gdef\csname Abc\endcsname{the macro abc expanded}
\gdef\Abc{the macro abc expanded}
Et voilà.
A variant with \newcommand
instead of \gdef
, that makes it easy to define macros with arguments:
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\newcommand\csname #1}#2\endcsname
}
\mycommand{abc}{Something else}
\show\Abc
\mycommand{uvw}[1]{Something else with #1}
\show\Uvw
With \gdef
the standard parameter text should be used.
\newcommand{\mycommand}[1]{\mycommandaux#1\relax}
\def\mycommandaux#1#2\relax{%
\uppercase{\expandafter\gdef\csname #1}#2\endcsname
}
\mycommand{abc}{Something else}
\show\Abc
\mycommand{uvw}#1{Something else with #1}
\show\Uvw
The problem is that \uppercase
is not expandable. You can't use it with \expandafter
. I would expand the argument first, as good measure, then read the first letter using a second, internal macro, then use \uppercase
to change that letter. Finally a second internal macro reads the new uppercase letter and the rest of the name and makes the definition. The original name can also be passed as a third argument if required:
\makeatletter
\newcommand{\mycommand}[1]{%
\begingroup
\edef\@tempa{#1}%
\expandafter\endgroup\expandafter\@mycommand\@tempa\@nnil{#1}%
}
\def\@mycommand#1{%
\uppercase{\@@mycommand{#1}}%
}
\def\@@mycommand#1#2\@nnil#3{%
\global\@namedef{#1#2}{the macro #3 expanded}%%
}
\makeatother
\mycommand{abc}
\show\Abc
This is a good example of why LuaTeX is useful.
\def\defineuppercase#1%
{\ctxcommand{defineuppercase("#1")}}
\startluacode
function commands.defineuppercase(s)
local upper = string.gsub(s, "^%l", string.upper)
context.setgvalue(upper)
end
\stopluacode
\defineuppercase{one}{Something}
\show\One
\end
I am using ConTeXt MkIV, but the code can be easily translated to LaTeX as well.