39

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}}
! Extra }, or forgotten $.
l.3 \DeclareMathOperator{\Upd}[1]{U^M_{#1}}

This is a kind of work-around, but not ideal.

\newcommand\UPD[1]{\mathop{\mathrm{U}^\mathrm{M}_\mathrm{#1}}}

Is there a way to make \DeclareMathOperator take arguments or am I stuck with the work around?

4
  • 2
    I think the intent of DeclareMathOperator is to make something like sin, 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.
    – qubyte
    Nov 14, 2011 at 16:33
  • 1
    Also, my favourite operator to define is \newcommand{\expup}[1]{mathrm{e}^{#1}}.
    – qubyte
    Nov 14, 2011 at 16:35
  • Typo. That should have been \newcommand{\expup}[1]{\mathrm{e}^{#1}}.
    – qubyte
    Nov 14, 2011 at 17:27
  • See my updated answer for the problems with the upright M.
    – egreg
    Nov 14, 2011 at 17:38

3 Answers 3

38

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.

2
  • So does \DeclareMathOperator do anything apart from apply \operatorname to its argument? That is, will I ever see a difference between the two outputs?
    – Seamus
    Nov 16, 2011 at 14:07
  • 8
    @Seamus \DeclareMathOperator{\xyz}{xyz} is only more efficient than \newcommand{\xyz}{\operatorname{xyz}}: the final result will be exactly the same.
    – egreg
    Nov 16, 2011 at 14:15
20

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:

operators with argument for subscript

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}}}
7
  • On the other hand, \Upd_{x} is not that harder to write, so \newcommand{\Upd}{\operatorname{U}^M} would be easier.
    – egreg
    Nov 14, 2011 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, 2011 at 17:00
  • @Seamus There's no grouping!
    – egreg
    Nov 14, 2011 at 17:03
  • @egreg there is grouping if you use \mathop which I want to use, for the spacing.
    – Seamus
    Nov 14, 2011 at 17:15
  • 1
    @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, 2011 at 17:17
5

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}

enter image description here

2
  • 10
    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, 2011 at 16:39
  • 1
    You might be right :)
    – Boris
    Nov 14, 2011 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .