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.

One sees all kind of versions in source files for defining something like a limit etc. So my question is which one serves what purpose. Is there any "best practice" in chosing one over the other?

share|improve this question
    
Related: How do I do a one-shot MathOperator? –  Lover of Structure Jul 4 '13 at 21:53
add comment

3 Answers

up vote 39 down vote accepted

When is it better to use \operatorname (or its wrapper \DeclareMathOperator) instead of \mathop? The answer is easy: always, unless you know precisely what's the behavior of \mathop.


First of all, one must of course recall that \operatorname and \DeclareMathOperator are provided by the amsopn package, which is automatically loaded by amsmath, but is also available standalone.

Let's see the main definitions:

\DeclareRobustCommand{\operatorname}{%
  \@ifstar{\qopname\newmcodes@ m}%
          {\qopname\newmcodes@ o}}%
\DeclareRobustCommand{\qopname}[3]{%
  \mathop{#1\kern\z@\operator@font#3}%
  \csname n#2limits@\endcsname}
\newcommand{\DeclareMathOperator}{%
  \@ifstar{\@declmathop m}{\@declmathop o}}
\long\def\@declmathop#1#2#3{%
  \@ifdefinable{#2}{%
    \DeclareRobustCommand{#2}{\qopname\newmcodes@#1{#3}}}}

The command \qopname is not really meant to be used in a document; the purpose of its first argument is to add some declarations such as \newmcodes@ before typesetting the operator name.

The \operatorname and \DeclareMathOperator each have a *-variant that passes to \qopname a second argument m instead of o. The latter command is just a wrapper:

\DeclareMathOperator{\xyz}{xyz}
\DeclareMathOperator*{\XYZ}{XYZ}

are pretty much equivalent to saying

\DeclareRobustCommand{\xyz}{\operatorname{xyz}}
\DeclareRobustCommand{\XYZ}{\operatorname*{XYZ}}

(but treated more efficiently) so what we need to examine is just \operatorname.

A call of \operatorname{xyz} translates into

\qopname\newmcodes@ o {xyz}

which becomes

  \mathop{\newmcodes@\kern\z@\operator@font xyz}\csname nolimits@\endcsname}
  1. The \newmcodes@ declaration takes care that some characters are treated differently as usual in math formulas (in particular the hyphen doesn't become a minus sign);

  2. \kern\z@ inserts an invisible object so that the entire contents of \mathop will never be a single character (see http://tex.stackexchange.com/a/41267/4427 for details);

  3. \operator@fonts chooses the predefined font for math operators, which usually is the upright text font;

  4. \nolimits@ is just an alias for \nolimits, so following subscripts and superscripts won't be typeset above and below the operator, unless countermanded by a \limits declaration.

For \operatorname* it's exactly the same, with the only difference that \nmlimits@ will be executed instead of \nolimits@. This macro is defined to be equivalent to \displaylimits, which makes the operator behave like the standard \lim or \min.

If an operator is used just a couple of times in a document, one can maybe dispense with defining a command with \DeclareMathOperator; but readability of the source is, in my opinion, enhanced if proper names for logical structures are used.

For \mathop one has to remember that it doesn't choose any particular font and it doesn't correct some glitches: so the output of

\mathop{\mathrm{abc-def}}

will be rather different from

\operatorname{abc-def}

and the latter is usually expected. In some rare cases \mathop is useful by itself:

\newcommand{\diff}{\mathop{}\!d}

is an example, where the empty \mathop is used to provide the correct spacing before the "d". Note that if one really wants to have an upright "d", it would be incorrect to define it as \operatorname{d} and the same as before with `\mathrm{d} should be used.

Also \stackrel and the better amsmath commands \overset and \underset are defined with an internal \mathop{...}\limits but wrapped up in some other construction.

share|improve this answer
add comment

\mathop is a plainTeX command, which declares a big operator like

\newcommand{\Res}{\mathop{\hbox{Res}}}

DeclareMathOperator is from AmsMath's amsopn subpackage, called as:

\DeclareMathOperator{\Res}{Res}

whereas operatorname is kind of an operator "on-the-fly". Input of

\operatorname{Res} 

has the same meaning as \Res defined with mathop or DeclareMathOperator

I think, if you loaded AmSMath (always a good idea) then I would use DeclareMathOperator and operatorame in cases this operator is seldom used in the file or to make some text in math-mode upright and with space-padding.

share|improve this answer
add comment

\mathop is the most primitive command, declaring the class of a character or subformula to be a unary math operator like \sum, for instance. This has consequences for spacing, compared to binary operators declared with \mathbin or binary relations declared with \mathrel, for instance.

Most math operators are written out as textual names. Some definitions from latex.ltx:

\def\max{\mathop{\operator@font max}}
\def\min{\mathop{\operator@font min}}
\def\sup{\mathop{\operator@font sup}}
\def\inf{\mathop{\operator@font inf}}
\def\arg{\mathop{\operator@font arg}\nolimits}
\def\ker{\mathop{\operator@font ker}\nolimits}
\def\dim{\mathop{\operator@font dim}\nolimits}
\def\hom{\mathop{\operator@font hom}\nolimits}

You see that a special font \operator@font is used; furthermore, one can see that an operator declared with \mathop is usually typeset with limits, that is, super- and subscripts are placed in a special position in displaymode. This can be turned off with \nolimits.

\operatorname from the AMSTeX bundle encapsulates this as a user command:

\DeclareRobustCommand{\operatorname}{%
  \@ifstar{\qopname\newmcodes@ m}%
          {\qopname\newmcodes@ o}}%
\DeclareRobustCommand{\qopname}[3]{%
  \mathop{#1\kern\z@\operator@font#3}%
  \csname n#2limits@\endcsname}

This basically means that \operatorname{dual} will typeset a math operator "dual" which looks like \max (i.e. with limits) and \operatorname*{dual} will typeset a math operator "dual" which looks like \arg (i.e. without limits).

\DeclareMathOperator is the corresponding declaration, i.e. \DeclareMathOperator\dual{dual} creates a new macro \dual which works like \max.

On usage:

  • With \mathop, anything can be declared a math operator, i.e. if you want to use * like a prefix operator (with limits), you can just say \mathop{*}.
  • \DeclareMathOperator should be used if you want to define a new operator like \max.
  • \operatorname should be used for special cases where you need a special operator name only once.
share|improve this answer
add comment

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.