The “official” syntax for \newcommand
is
\newcommand{<macro name>}[<args>]{<text>}
or
\newcommand{<macro name>}[<args>][<default>]{<text>}
but it turns out that the braces around the <macro name>
are optional, so long as a single token appears. The braces are good for avoiding mistakes, but some self-control usually suffices.
With
\expandafter\newcommand\csname hello\endcsname{world}
the token \hello
is built before \newcommand
starts acting and absorbing its first argument (the story is much longer, actually, but this short explanation is precise enough).
On the other hand,
\expandafter\newcommand{\csname hello\endcsname}{world}
will try to expand the brace: \expandafter
just acts on a single token, triggering its expansion, if the token is expandable, otherwise doing nothing. You can have the braces, if you prefer, but you need a further \expandafter
:
\expandafter\newcommand\expandafter{\csname hello\endcsname}{world}
However this is too much for such a simple construction, isn't it?
If you find yourself in the position of having to do several of these definitions, it's “easy” to build a wrapper
\makeatletter
\newcommand\newnamecommand{\@star@or@long\new@name@command}
\newcommand\new@name@command[1]{\expandafter\new@command\csname #1\endcsname}
\newcommand\renewnamecommand{\@star@or@long\renew@name@command}
\newcommand\renew@name@command[1]{\expandafter\renew@command\csname #1\endcsname}
\newcommand\providenamecommand{\@star@or@long\provide@name@command}
\newcommand\provide@name@command[1]{\expandafter\provide@command\csname #1\endcsname}
\makeatother
\newnamecommand{hello}{Hello World}
\newnamecommand*{helloarg}[1]{Hello #1}
\renewnamecommand{hello}{Whatever}
\providenamecommand{foo}{FOO}
\expandafter
or to remove the optional braces.