# \DeclareMathOperator won't take arguments

I'd like to define a family of math operators that have the same superscript, but different subscripts. My first attempt was:

\DeclareMathOperator{\Upd}[1]{U^M_{#1}}


But the log tells me that:

l.3 \DeclareMathOperator{\Upd}[1
]{U^M_{#1}}
! Missing $inserted. <inserted text>$
l.3 \DeclareMathOperator{\Upd}[1]{U^
M_{#1}}
! You can't use macro parameter character #' in math mode.
l.3 \DeclareMathOperator{\Upd}[1]{U^M_{#
1}}
\end{document}


-
It's not a good idea, in my opinion, to add \ensuremath; why should it be? Disclaimer: this is part of my \ensuremath-only-when-really-needed campaign. :) –  egreg Nov 14 '11 at 16:39
You might be right :) –  Boris Nov 14 '11 at 16:41

To introduce an argument, you could combine defining the operator and your command:

\DeclareMathOperator{\UpdOp}{U}
\newcommand\Upd[1]{\UpdOp^M_{#1}}


That's also possible with the starred version instead

\DeclareMathOperator*{\UpdOp}{U}


resulting in \Upd having the superscript and subscript on the right in inline math mode, and above and below in displayed math mode.

I assume, you would like to have M in operatorfont as well. So, why not defining operators and your command:

\DeclareMathOperator{\UpdOp}{U}
\DeclareMathOperator{\MOp}{M}
\newcommand\Upd[1]{\UpdOp^{\MOp}_{#1}}


Also here you can used the starred version, here the output how it can look inline and displayed:

Shorter, as discussed in the comments:

\newcommand{\Upd}{\operatorname{U}^{\operatorname{M}}}


\operatorname{U^M} instead would result in wrong subscript spacing.

With the subscript in operator font too, this would get longer but Boris showed a short working version, here slightly modified:

\newcommand{\Upd}[1]{\operatorname{U^M_{#1}}}


Or combine starred \DeclareMathOperator with operator font super- and subscript and automatically adjusted limits for the main operator:

\DeclareMathOperator*{\UpdOp}{U}
\newcommand\Upd[1]{\UpdOp^{\operatorname{M}}_{\operatorname{#1}}}

-
On the other hand, \Upd_{x} is not that harder to write, so \newcommand{\Upd}{\operatorname{U}^M} would be easier. –  egreg Nov 14 '11 at 16:41
@egreg but if \Upd has a superscript, then having the subscript outside messes with the spacing because of grouping. It ends up looking like: {U^M}_X which doesn't look good. That's why I wanted an operator. –  Seamus Nov 14 '11 at 17:00
@Seamus There's no grouping! –  egreg Nov 14 '11 at 17:03
@egreg there is grouping if you use \mathop which I want to use, for the spacing. –  Seamus Nov 14 '11 at 17:15
@Seamus \mathop{...}^{...}` still retains its math operator status: the subscripts and superscripts are attached as fields to the math atom they are next. –  egreg Nov 14 '11 at 17:17