Documentation/Math & Equations/How to Define Custom Math Operators (\DeclareMathOperator)
Math & Equations

How to Define Custom Math Operators (\DeclareMathOperator)

LaTeX provides built-in operators like \sin, \cos, and \lim that are typeset in upright (Roman) font with correct spacing. When you need operators that aren't built in — like tr (trace), diag (diagonal), Var (variance), or sgn (sign) — you should declare them properly rather than typing them as italic variables. Bibby AI's template library pre-declares common operators for your field, so statistics papers come with Var, Cov, and E already defined — a head start you won't get in Overleaf.

Declare Custom Operators in the Preamble

Use \DeclareMathOperator for operators without limits (like sin, tr) and \DeclareMathOperator* for operators with limits below (like lim, argmin):

\documentclass{article}
\usepackage{amsmath}

% Without limits (subscripts stay to the side)
\DeclareMathOperator{\tr}{tr}
\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\sgn}{sgn}

% With limits (subscripts go below in display mode)
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\Var}{Var}

\begin{document}

$\tr(\mathbf{A}) = \sum_{i} a_{ii}$

\[
\hat{\theta} = \argmin_{\theta} \mathcal{L}(\theta)
\]

\end{document}

Inline Usage with \operatorname

For one-off operators that don't warrant a preamble declaration, use \operatorname (or \operatorname* for limits below):

% One-off operator
$\operatorname{Cov}(X, Y) = E[XY] - E[X]E[Y]$

% With limits below in display
\[
\operatorname*{ess\,sup}_{x \in \Omega} f(x)
\]

Operators with Multiple Words or Modifiers

Create operators that include subscripts, modifiers, or multi-word names:

% Probability and expectation
\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator{\Prob}{\mathbb{P}}
\DeclareMathOperator{\Cov}{Cov}

% Usage
\[
\E_{X \sim p}[f(X)] = \int f(x) \, p(x) \, dx
\]

\[
\Prob(A \mid B) = \frac{\Prob(B \mid A) \Prob(A)}{\Prob(B)}
\]

💡 Tips

  • Never write $tr(A)$ or $Var(X)$ — the letters render as italic variables multiplied together, not as an operator name.
  • \DeclareMathOperator provides correct spacing before and after the operator automatically.
  • Place all your \DeclareMathOperator declarations together in the preamble for easy maintenance.
  • Bibby AI's math-aware autocomplete suggests \DeclareMathOperator when it detects you're typing a multi-letter operator name.

Try This in Bibby AI

Write LaTeX faster with AI auto-complete and instant compilation.

Start Writing Free

Related Tutorials

How to Define Custom Math Operators (\DeclareMathOperator) | Bibby AI