1

I'm trying to create a command with which to write

\declaremathoperator{ABC}

to get

\def\ABC{\operatorname{ABC}}

I tried to implement it like

\def\declaremathoperator#1
{ \def\csname #1\endcsname{\operatorname{#1}} }

but it doesn't work: Use of \csname doesn't match its definition.

It seems to me that the solution, if it exists, should be based on writing \#1, but at the same time so that TeX first makes a substitution of #1, and only then would begin to consider \ as the beginning of the command name.

1 Answers1

4

Your gain with respect to

\DeclareMathOperator{\ABC}{ABC}

is very little. And the proposed definition is very dangerous. If it worked, you'd be able to say

\declaremathoperator{span}

and destroy your documents. You can do

\newcommand{\newmathoperator}[1]{%
  \expandafter\DeclareMathOperator\csname #1\endcsname{#1}%
}
\newmathoperator{ABC}
\newmathoperator{span}% <--- would give error

which would warn you in case the proposed command already has a definition. The \expandafter is necessary so \DeclareMathOperator sees the control sequence you want to define, rather than \csname.

egreg
  • 1,121,712