[Tex/LaTex] Macro defining macro

macros

I have the following:

\newcommand{\makeWord}[1]{%
\newcommand{\#1}{\textrm{#1}}}

\makeWord{AdditiveGroup}
\newcommand{\AdditiveGroup}{\textrm{AdditiveGroup}}

Unfortunately, I did not properly define the makeWord macro. How do I fix this?

Best Answer

\newcommand{\makeWord}[1]{%
\newcommand{\#1}{\textrm{#1}}}

\#1 is the two tokens \# (which would typeset a #) and 1 so is a syntax error as the first argument of \newcommand.

Try

\newcommand{\makeWord}[1]{%
  \expandafter\newcommand\csname#1\endcsname{\textrm{#1}}}

Note this will make a mathord token if used in math. If you want to make an operator like \log it needs to be

\newcommand{\makeWord}[1]{%
  \expandafter\newcommand\csname#1\endcsname{\mathop{\mathrm{#1}}}}

or use the amsopn \DeclareMathOperator command.