I want to define a command, let's call it \commander
, for defining starred and non-starred versions of a new command. \commander
would be called as follows:
\commander[<options>]{\thenewcommand}
This creates the commands \thenewcommand
and \thenewcommand*
, with slightly different instructions. (The specific definitions of those commands are of no importance for this question. I've already worked them out.) The way I tried to define \commander
depends on a one-argument command called \commandname
, which returns the string of the name of a command, but without its backslash. For example:
\commandname{\empty}
returns the stringempty
\commandname{\color}
returns the stringcolor
\commandname{\thenewcommand}
returns the stringthenewcommand
- And so forth...
I don't think that there's any problem with \commandname
since it has worked perfectly up until my attempt of defining \commander
. But just in case, here is its definition:
\newcommand{\commandname}[1]{% \ifcat\relax\noexpand#1% \expandafter\expandafter\expandafter% \@gobble\expandafter\string\fi#1}
(This was poposed by Ashok.)
Any suggestion for improving or simplifying this \commandname
is welcome.
\commander
is then defined roughly as follows:
\newcommand{\commander}[2][]{% \expandafter\newcommand{#2}{% \@ifstar% \csname star\commandname{#2}\endcsname% \csname nostar\commandname{#2}\endcsname}% % %DEFINE THE #2 COMMAND \expandafter\newcommand{\csname nostar\commandname{#2}\endcsname}% {<code for #2>} % %DEFINE THE #2* COMMAND \expandafter\newcommand{\csname star\commandname{#2}\endcsname}% {<code for #2*>}%}
But whenever I attempt to call \commander
, it returns the following error messages:
Command \csname already defined. \commander{\thenewcommand}Extra \endcsname. \commander{\thenewcommand}
I have previously used a similar structure for recursively defining commands, so I suppose the problem is in the simultaneous use of \@ifstar
and \csname
.
I would strongly prefer that the solution be as TeX primitive as possible because I would like to use it in several projects. In my current project, though, I am using the packages everypage
and pgf/tikz
. So a solution depending on those packages is also welcome.
Thanks in advance for your help.