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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
1  
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
    
Sort of related: 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? – Loop Space Aug 17 '12 at 9:24
up vote 83 down vote accepted

\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{amsopn}
\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{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}
share|improve this answer
4  
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
    
Many thanks for the comments and the kind welcome. I have edited my answer to make the example slightly more realistic using \boldsymbol instead. – Andrew Swann Aug 17 '12 at 13:14
    
@egreg and Barbara Beeton: I have now put in a different example without this bold symbol issue. – Andrew Swann Aug 17 '12 at 18:58
1  
@barbarabeeton: the bm versus boldsymbol issue was tackled in tex.stackexchange.com/q/3238 – Philippe Goutet Aug 17 '12 at 19:48
    
It is worth to mention that there is also a starred version of \operatorname. – Rauni Nov 27 '14 at 1:51

I usually use

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

instead of

\DeclareMathOperator{\Ker}{Ker}

However, the second is much practical.

share|improve this answer
3  
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
1  
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.

share|improve this answer
    
Does anyone know why that is? – Tyson Williams Apr 12 '15 at 21:18
1  
All \Declare... commands share the property. See Dox's answer, though. – egreg Apr 12 '15 at 21:37
    
Thx for anwers... I also read lshort and I am using latex a lot but I didn't know the difference – Jozef Janočko Apr 20 at 20:14

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.