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.
DeclareMathOperator
is to make something likesin
,exp
, etc. As far as I can remember there are no math operators of this kind (roman with stuff in brackets to the right) that take arguments.\newcommand{\expup}[1]{mathrm{e}^{#1}}
.\newcommand{\expup}[1]{\mathrm{e}^{#1}}
.