Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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 '11 at 16:33
1  
Also, my favourite operator to define is \newcommand{\expup}[1]{mathrm{e}^{#1}}. –  qubyte Nov 14 '11 at 16:35
    
Typo. That should have been \newcommand{\expup}[1]{\mathrm{e}^{#1}}. –  qubyte Nov 14 '11 at 17:27
    
See my updated answer for the problems with the upright M. –  egreg Nov 14 '11 at 17:38
add comment

3 Answers

up vote 16 down vote accepted

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}, where <font setup> are some instructions that are irrelevant for this discussion. When one says

\mathop{<f>}^{<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}^{\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.

share|improve this answer
    
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 '11 at 14:07
4  
@Seamus \DeclareMathOperator{\xyz}{xyz} is only more efficient than \newcommand{\xyz}{\operatorname{xyz}}: the final result will be exactly the same. –  egreg Nov 16 '11 at 14:15
add comment

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

share|improve this answer
6  
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
add comment

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}}}
share|improve this answer
    
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
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 '11 at 17:17
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.