\DeclareMathOperator won't take arguments
The better way to use such a construction is to say
\newcommand{\Upd}{\operatorname{U}^{\mathrm{M}}}
Then one can use \Upd_{1}
which is not harder to write than \Upd{1}
that would be allowed by \newcommand{\Upd}{\operatorname{U}^{\mathrm{M}}_{#1}}
.
Of course, putting in \ensuremath
will break things, but it's unnecessary. Besides
$\Upd_{1}$
is clearer an input than \Upd{1}
.
The command \operatorname{U}
is just a wrapper around \mathop{<font setup>U}\nolimits
, where <font setup>
are some instructions that are irrelevant for this discussion. When one says
\mathop{<f>}\nolimits^{<a>}_{<b>}
where <f>
, <a>
and <b>
are any subformulas, TeX treats the whole block as a math operator, as far a spacing is concerned. So, with my recommended definition, the input \Upd_{1}
will eventually become
\mathop{<font setup>U}\nolimits^{\mathrm{M}}_{1}
that will work as a math operator. There is no need to push the superscript or the subscript inside \mathop
. This is common: when one writes $a=_{t}b$
, the whole =_{t}
will be treated as a relation symbol, with the correct spacing around it.
Technically, \mathop{<f>}
builds the subformula <f>
and assigns it as the main field of a MathOp atom; each math atom has two other fields, for the superscript and the subscript, which, if present, don't affect the type of the atom.
Finally, \DeclareMathOperator
and \operatorname
should not be thought as ways to get upright letters; an upright letter is obtained by \mathrm
; it's true that those two commands write their argument in upright letters, but the main thing they do is to prepare for correct spacing.
The \nolimits
command is omitted when \operatorname*
is used, but this is a different topic.
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}}}
Unlike \newcommand
, \DeclareMathOperator
does not have arguments. If you need arguments, use \newcommand
. If you want U in your example to look like an operator, you can use a handy command \operatorname
provided by amsmath
. Also, it is a good idea to use \ensuremath
in the definition, so your operator can work inside text too.
This leads to the following solution:
\documentclass{article}
\pagestyle{empty}
\usepackage{amsmath}
\newcommand{\Upd}[1]{\ensuremath{\operatorname{U_M^{#1}}}}
\begin{document}
$\Upd{a} (x)$
\end{document}