# newcommand vs. DeclareMathOperator


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.

-
Run texdoc amsmath and see page 13f. – Marco Daniel Aug 17 '12 at 8: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 9:09

\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

instead of 2sin\theta

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

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

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

$$\End V$$

\end{document}




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

Similarly there is the starred variant \operatorname*.

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

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

\end{document}

-
Great answer, Andrew! :) By the way, I know you've been here for almost two months now, but I'd like to "officially" welcome you to our community. Make yourself at home! It's very rewarding to have a new TeX friend with us eager to improve knowledge and make this community even better. :) – Paulo Cereda Aug 17 '12 at 10:58
The answer is good, but the example isn't: \mathbf{\varphi} doesn't print a boldface phi. Either use a latin character (say \mathbf{u}) or \bm{\varphi} (loading the bm package). – egreg Aug 17 '12 at 12:37
@egreg -- if amsmath is loaded, \boldsymbol{\varphi} works. amsmath also incorporates amsopn so that isn't needed any longer as a separate package. – barbara beeton Aug 17 '12 at 13:06
@barbarabeeton While \boldsymbol works, the first lines in the documentation of amsbsy say that \bm is preferable to \boldsymbol. :) – egreg Aug 17 '12 at 15:19
@barbarabeeton: the bm versus boldsymbol issue was tackled in tex.stackexchange.com/q/3238 – Philippe Goutet Aug 17 '12 at 19:48

I usually use

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


\DeclareMathOperator{\Ker}{Ker}


However, the second is much practical.

-
Nice because it allows to do \renewcommand, and I don't know what is the equivalent for DeclareMathOperator. – Stéphane Laurent Jan 8 '14 at 10:02
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

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

-
Does anyone know why that is? – Tyson Williams Apr 12 '15 at 21:18
All \Declare... commands share the property. See Dox's answer, though. – egreg Apr 12 '15 at 21:37