What's the difference between \mathrm and \operatorname?

There are many differences. The main one is in the fact that \mathrm{xyz} behaves like an ordinary letter, while \operatorname{xyz} behaves like function names such as \sin. Here's an illustration

$\sin x + \sin(x+y) + a\sin z$

$\mathrm{sin} x + \mathrm{sin}(x+y) + a\mathrm{sin}z$

enter image description here

where it's clear that the second line is typeset wrong. Even if your "operator" requires parentheses after it, it should be \operatorname, as the third summand shows, where a thin space separates the coefficient from the operator.

Another subtle difference is in how some characters are interpreted in \mathrm and in \operatorname. Suppose you have an operator to be called "pre-norm", with a hyphen. Here's the example

$\operatorname{pre-norm}(\mathbf{v})$

$\mathrm{pre-norm}(\mathbf{v})$

enter image description here

and now it's clear what is to be used. Indeed \operatorname (and the same holds for macros defined with \DeclareMathOperator) treats punctuation symbols in a special way; \mathrm, instead, treats them as math symbols.


\operatorname:

The argument is written in upright mode but with some additional space before and behind.

\mathrm:

It is like math mode (no spaces), but in upright mode. The font size isn't changed.

The following example shows the differences:

\documentclass[]{article}
\usepackage{amsmath}
\begin{document}
\[x\operatorname{foo}y\]
\[x\mathrm{foo}y\]
\end{document}

enter image description here


In other LaTeX environments \mathrm{xyz} is useful. In MathJax v 2.0

$ i \\, \mathrm{sinh} \; $

gave the equivalent output as $ i \sinh x $. MathJax did not recognize \operatorname.