212

As far as I understand, whatever can be done using DeclareMathOperator can also be achieved using newcommand, but not the other way around. If this is the case, what are the circumstances where DeclareMathOperator is the better choice? When should I take solely newcommand?

I found a question regarding arguments and DeclareMathOperator, and I wonder what other aspects should be taken into consideration when it comes to choosing which method to use when defining math operators.

Dror
  • 22,613
  • 3
    Run texdoc amsmath and see page 13f. – Marco Daniel Aug 17 '12 at 08:54
  • \DeclareMathOperator is a very special case of \newcommand, so the question as it stands is too generic to receive an answer. – egreg Aug 17 '12 at 09:09
  • Sort of related: http://tex.stackexchange.com/q/655/86 I'd go for the "horses for courses" argument: \DeclareMathOperator says what it's going to do and does it, so makes it easier for you to remember 6months later what it was meant for. Also, if you want a consistent look you're going to effectively reimplement \DeclareMathOperator (this \newcommand should produce the same effect as that \newcommand but not the other \newcommand so we'd better have a "helper" macro for this and that but not the other) so why not take advantage of the work others have already done? – Andrew Stacey Aug 17 '12 at 09:24
  • 5
    Consider \newcommand{\im}{\text{im}} with \DeclareMathOperator{\im}{im}. Now try and use these commands in the standard theorem environment. In the former "im" will be italicized, but in the latter "im" won't be. – user2154420 Apr 05 '18 at 16:06
  • is this in the amsmath package? – Lucas May 17 '23 at 23:12
  • \ DeclareMathOperator is defined in amsopn which is loaded by default when including amsmath. – Dror May 19 '23 at 07:04
  • @user2154420 I've defined operators in bold and when I type them in proofs they're not italicized but in theorem environment they are, any idea on how to never have them in italic? – Jolia Feb 22 '24 at 09:55

4 Answers4

236

\DeclareMathOperator is designed to create commands that should typeset operator names such as sin and lim. Some of these are already defined in base TeX or LaTeX so one writes 2\sin\theta

op sine

instead of 2sin\theta

nop sine

giving correct spacing and font. If you need an operator of this type that is not predefined, then you create it with \DeclareMathOperator, e.g. the space of endomorphisms of a vector space is written \End V

enter image description here

but you need to make the definition \DeclareMathOperator{\End}{End} first: a minimal working example is

\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\End}{End}
\begin{document}

\( \End V \)

\end{document}

\newcommand is much more general and can be used to define direct short cuts or more complicated macros. So for example if you find youself writing \left.\frac d{dt}\right|_{t=0} many times in your document you can package this up as a single command \dtzero via \newcommand{\dtzero}{\left.\frac d{dt}\right|_{t=0}} and just type \dtzero each time instead.

I would usually recommend reserving \DeclareMathOperator for the use described above and using \newcommand in most other situations. To get the effect of \DeclareMathOperator in a one-off situation, or inside a \newcommand, you use \operatorname; so you can write \operatorname{End}V for the above example.

Finally, one should note that there is a starred version \DeclareMathOperator*. This is used for defining operators that have limits typeset beneath them instead of to the right (at least when in a display). For example

Sample output

Similarly there is the starred variant \operatorname*.

\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\End}{End}
\DeclareMathOperator*{\Max}{Max}
\begin{document}

\begin{displaymath}
  \Max_{x\in A} f(x) \qquad  \End_R V 
\end{displaymath}

\end{document}

Remark. The above code samples load the amsmath package. Strictly speaking all you need is the amsopn package, which amsmath reads in automatically. Alternatively, one can load mathtools which is a modern package building further on amsmath.

Andrew Swann
  • 95,762
43

I usually use

\newcommand{\Ker}{\operatorname{Ker}}

instead of

\DeclareMathOperator{\Ker}{Ker}

However, the second is much practical.

Dox
  • 5,729
  • 16
    Nice because it allows to do \renewcommand, and I don't know what is the equivalent for DeclareMathOperator. – Stéphane Laurent Jan 08 '14 at 10:02
  • 12
    You can first "undefine" the command by \letting it be equal to \relax, e.g., \let\div\relax \DeclareMathOperator{\div}{div}. – MSC May 15 '15 at 12:47
  • 2
    \operatorname is still superior because you may \providecommand for conditional \renewcommand, of course you can use an \if \fi construct, but that somewhat defeats the purpose of LaTeX vs TeX. – Oskar Limka Mar 17 '19 at 10:09
  • 7
    @MSC Why does everything in $\LaTeX$ have to be so unbelievably hacky… – Lukas Juhrich Dec 29 '19 at 13:33
11

Another difference is that \DeclareMathOperator can only be used in the preamble while \newcommand has no such restriction.

2

Another advantage that I have seen in using \newcommand instead of \DeclareMathOperator is that by adding \ensuremath{} to \newcommand, we can use it easily in between text.

E.g. \newcommand{\w}{\ensuremath{\mathbf{w}}\xspace}. You can simply use \w in text mode without adding $ on either side to get a math bold w. \xspace is added to add space after the use of the command, as required.