37

The amsmath packages helpfully define a macro \DeclareMathOperator which does what it says on the tin: it declares a mathematical operator such as \sin or \Null or whatever and typesets it all nice and dandy.

If I want to do a one-shot operator, it seems a bit of a fuss to \DeclareMathOperator it if I'm only going to use it once. So:

Is there a command that typesets its contents in the same way that \DeclareMathOperator does? (And I'd like it to be exactly how \DeclareMathOperator does since I sometimes mess with fonts and colours and would like them consistent.)

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751

2 Answers2

32

You are looking for \mathop: http://www.giss.nasa.gov/tools/latex/stackrel.html , for example \mathop{\sum \sum}_{i,j=1}^{N} a_i a_j.

If you really want to do exactly what amsmath does then \operatorname is a better choice.

root
  • 418
cefstat
  • 763
9

You can go back to the TeXbook and do it the Knuth way, using \mathop

\def\limsup{\mathop{\overline{\rm lim}}}

\[\limsup\]

Edit

If you really only want a one shot, you can type:

\[\mathop{\overline{lim}}_{n=1}\limits\]

I would rather have something like:

\def\oneshot#1{\mathop{\mathrm{#1}}\limits}

\[\oneshot{Diag}_{n=1}^m\]

One can extend the command \oneshot to the \nolimits version for consistency. Why I prefer a two shot approach i.e, defining it first and then using it, is that good software practice dictates that you should try and re-use code. You could stick the command in your master style file, if you have one. It will also with a shorter name make reading the LaTeX source easier.

Would you really use an operator only once in a mathematics write-up?

yannisl
  • 117,160
  • 3
    if you're using latex, better to use \mathrm: \def\limsup{\mathop{\overline{\mathrm{lim}}}} or just look into the file amsopn.sty and see how \DeclareMathOperator is defined. it's a bit convoluted, but you're good enough at latex innards to figure it out. do take note of the use of `\limits'. – barbara beeton Mar 04 '11 at 13:19
  • 1
    @Yiannis: It doesn't really make sense to wrap this in a \def (or better \newcommand): As the title says, this is about a one-shot math operator. – Hendrik Vogt Mar 05 '11 at 08:39
  • 1
    @Hendrik Vogt For a one shot one can only use \overline{lim} if you are not too worried about side-effects, or \mathop{\overline{lim}}. I wouldn't use newcommand in this case as def can be overwritten much easier and is less typing. One-shot is not a recommended approach from a programming perspective, so I would recommend one takes two shots! – yannisl Mar 05 '11 at 10:41
  • @Yiannis: Sorry, but I find it really doesn't make sense what you're saying here. I have the feeling that sometimes you need to read the questions more carefully: "it seems a bit of a fuss to \DeclareMathOperator it if I'm only going to use it once". And what would be the advantage of your solution over \DeclareMathOperator?? – Hendrik Vogt Mar 05 '11 at 12:10
  • 1
    @Hendrik Vogt Only advantage is that you don't need amsmath. Another is that you do not need to define it as with the DeclareMathOperator in the preamble only. See also my edited answer for some more thoughts on the subject. – yannisl Mar 05 '11 at 16:17
  • 1
    @Yiannis: I like your edited answer much better: You explain why you chose to not make it a one-shot. Thanks a lot! – Hendrik Vogt Mar 05 '11 at 18:38
  • To answer your last question: the case in point was that I was explaining alternative notation, so I wanted to say something like \(\colspan(A)\) is also sometimes written \(\oneshot{col}(A)\), whereupon I would never refer to \col again but always use \colspan. – Andrew Stacey Mar 08 '11 at 14:08