161

Mathematical operators, such as function names, should be set in roman type, not italics. Latex already has commands for some operators, including \max, \min, and \log. How can I define additional such commands?

7 Answers 7

164

\DeclareMathOperator{\foo}{foo} and \DeclareMathOperator*{\hocolim}{hocolim} for sub- and superscripts in the limits position.

This requires

\usepackage{amsmath}

which is recommended for math documents anyway.

Minimal example:

\documentclass{article}
\usepackage{amsmath}

\DeclareMathOperator{\foo}{foo}
\DeclareMathOperator*{\hocolim}{hocolim}

\begin{document}

Example of $\foo(x)$ and $\foo x$.

Example of $\hocolim_{x\in X} f(x)$ and displayed
\begin{equation*}
\hocolim_{x\in X} f(x)
\end{equation*}

\end{document}

enter image description here

3
  • 1
    Great! I thought there was some way of handling the limits, but couldn't remember it. Commented Jul 30, 2010 at 18:56
  • Could someone clarify the one for sub/super-scripts? Would hocolim take sub/super-scripts, or would it appear in them? Commented Sep 27, 2015 at 21:01
  • 7
    Both \foo and \hocolim can take sub/superscripts, the difference is in how the subscripts and superscripts appear in display mode. The sub/superscripts to an operator defined with \DeclareMathOperator will always be placed after foo, while sub/superscripts to operators defined with \DeclareMathOperator* will be placed above and below the function name in display mode.
    – hugovdberg
    Commented Jan 19, 2018 at 9:26
53

As mentioned before, the amsmath command \DeclareMathOperator{\Det}{Det} is a good way to do this, but this is actually basically a wrapper for \newcommand{\Det}{\operatorname{Det}}. So if you only want to use the command once and don't want to define a symbol (especially useful if you are using an online tex editor), then just use \operatorname

And just like \DeclareMathOperator*, you can use \operatorname* to specify that underbraces should go underneath. This is useful for something like \operatorname*{minimize}. More info here

39

Alternatively, if you are using any of the packages from the AMS (amsart.cls or amsmath.sty) then there is a command \DeclareMathOperator which does what it says on the tin! For example,

\DeclareMathOperator{\Det}{Det}

I think that it can handle variants, but I don't recall off the top of my head.

1
  • Ah, you were faster :-)
    – Grigory M
    Commented Jul 30, 2010 at 18:57
15

If you're looking for something one-off, you can always use \mathrm in a math environment like so:

\mathrm{ultimatefunction}(x)

Which will display 'ultimatefunction' in a roman type.

2
  • 31
    Note that \mathrm differs from \operatorname or \DeclareMathOperator in terms of spacing, and that the latter two are preferable to the first in this respect (see this answer for more detailed information). Commented Nov 13, 2012 at 12:09
  • 1
    I ended up using \operatorname as mentioned by @AndrewUzzell, but I just wanted to point out that \mathrm and \operatorname are great for those working with the limited LaTeX subset available in Apple's Keynote equation editor.
    – nbering
    Commented Jul 9, 2018 at 17:27
5

Define the command \newoperator as follows:

\providecommand{\newoperator}[3]{%
  \newcommand*{#1}{\mathop{#2}#3}}

Here is an example that defines \FD as an operator:

\newoperator{\FD}{\mathrm{FD}}{\nolimits}
4

If you need use this new operator only one time, maybe you could considere use

\text{operator}

in the math formula.

For example:  $$ 3 \cdot \text{FoO} (x) $$
3
  • 2
    Welcome to TeX.SX. A tip: If you indent lines by 4 spaces, then they're marked as a code sample. You can also highlight the code and click the "code" button ({}) or hit Ctrl+K. You may want to have a look to Why is [ … ] preferable to $$ … $$? to better typeset equations. Commented Nov 27, 2013 at 15:13
  • 7
    this won't always set the specified string in roman type; if it's within an italicized theorem environment, it will come out in italics. also, it won't accommodate limits correctly, if such are needed, and the spacing won't be quite the same as for a "real" operator. Commented Nov 27, 2013 at 15:27
  • Thank you, @Claudio Fiandrino. I'm reading your link now. Commented Nov 27, 2013 at 18:54
4

A one-off operator that you don’t think is worth defining a new macro for can be typeset with \operatorname. It uses the same font as \mathrm, which by default is the main text font. However, unicode-math also defines a command \setoperatorfont to change this. (In LaTeX, it is possible to redefine \operator@font.) Additionally, fontspec allows you to \setmathrm.

\( \operatorname{foo}_a^b f \equiv
   \int_a^b \int_{-\infty}^{\infty} f(x) \,\mathrm{d}y \,\mathrm{d}x
\)

Sample using \operatorname

This also requires amsmath.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .