Math Operators Best-Practices – Difference Between \mathop, \operatorname, and \DeclareMathOperator

best practicesmath-operators

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?

Best Answer

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 https://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} instead of d should be used: explicitly, the alternative is

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

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