Log-like functions & mod

\mathrm{sin} and \sin look like exactly the same “sin” on the page. The first is still wrong, and the difference is 3.33325pt. Measure $a\operatorname{op}b$ in LaTeX and you get 23.46638pt; $a\mathrm{op}b$ gives 20.13313pt. The upright shape is identical, but \mathrm carries no space around it. Those 3.33325pt are not decoration: they are exactly two thin spaces, the ones TeX puts on either side of an Op atom. This page starts from that atom, then locates the 32 built-in function names in the LaTeX sources, explains why exactly 10 of them set their subscript underneath, shows what the star in \DeclareMathOperator changes, and separates the four modular forms \bmod, \pmod, \mod, and \pod by measured width.

Why \mathrm{sin} is wrong: the atom class decides the spacing

In math mode every letter is taken as the name of a variable and set in math italic, so typing sin x produces not “sine” but the product of four quantities s, i, n, x. That much is well known, and the fix many people reach for is \mathrm{sin}, because once it is upright the problem looks solved. But \mathrm changes only the font. As far as TeX is concerned the atom class is still Ord, an ordinary symbol.

TeX sorts the pieces of a formula into atom classes — Ord (ordinary), Op (operator), Bin (binary operator), Rel (relation) and a few more — and decides spacing purely from which classes sit next to each other. Between Ord and Ord there is no space. Between Op and Ord there is a thin space (\thinmuskip, 3mu). \sin is an Op; \mathrm{sin} is an Ord. Hence the difference. Measuring the following four in a 10pt document turns that mechanism straight into numbers.

measured on TeX Live 2024, 10pt article
\DeclareMathOperator{\myop}{op}
% \sbox0{$...$}\message{\the\wd0}

$a\operatorname{op}b$        23.46638pt
$a\myop b$                   23.46638pt   % identical to \operatorname
$a\mathop{\mathrm{op}}b$     23.46638pt   % same font as \mathrm, but Op class
$a\mathrm{op}b$              20.13313pt   % 3.33325pt narrower

$\mkern3mu$  1.66663pt        % one thin space; two of them = 3.33326pt

The third line settles it. \mathop{\mathrm{op}} uses exactly the same font as \mathrm yet matches \operatorname to within a hundred-thousandth of a point. What creates the difference is therefore not the font but the Op class that \mathop confers. And the 3.33325pt gap is precisely two thin spaces of 1.66663pt — one between a and “op”, one between “op” and b. The folklore that “\mathrm gets the spacing wrong” is thus accounted for to the last decimal. The practical conclusion is simple: always write function names with the dedicated command or with \operatorname, never with \mathrm.

The 32 built-in function names, and where they are written

The definitions of \log and \sin do not live in a package: they live in latex.ltx, the LaTeX format file itself. In TeX Live 2024 they occupy lines 12487 to 12518 — exactly 32 lines — each of the form \DeclareRobustCommand\log{\mathop{\operator@font log}\nolimits}. The list is older still: almost the same set sits in Knuth’s plain.tex at lines 1058–1085. So being able to write \sin is not thanks to amsmath; it works from the start with no package at all (only declaring your own needs amsmath — see below).

  • Trigonometric: \sin \cos \tan \cot \sec \csc, and the inverses \arcsin \arccos \arctan.
  • Hyperbolic: \sinh \cosh \tanh \coth — note that \sech and \csch are not included; declare them yourself if you need them.
  • Logarithms and exponential: \log (general), \ln (natural), \lg (base 2, common in information theory), \exp.
  • Algebra, geometry and friends: \deg (degree), \dim (dimension), \ker (kernel), \hom, \arg (argument), \det (determinant).
  • The 10 that set scripts underneath: \lim \limsup \liminf \max \min \sup \inf \det \Pr \gcd.

Bases and exponents attach through the ordinary script mechanism: \log_2 x puts a 2 at the lower right of “log”, and \sin^2\theta puts one at the upper right, giving the customary sin²θ. One detail you only notice by reading the source: \limsup is defined as \mathop{\operator@font lim\,sup}there is a hand-placed \, thin space between “lim” and “sup”. It is not one continuous word but two words deliberately held slightly apart.

latex
\[
  \sin^2\theta + \cos^2\theta = 1, \qquad
  \log_2 8 = 3, \qquad \ln e = 1.
\]

The 10 that put scripts underneath, and why \limits has no effect

Of the 32, twenty-two end their definition with \nolimits, so their scripts appear at the lower right. The remaining ten — \lim, \limsup, \liminf, \max, \min, \sup, \inf, \det, \Pr, \gcd — have no \nolimits. Since the default for \mathop in display style is “scripts underneath”, exactly those ten set x→0 below \lim in a display. The convention that a limit or a maximum should show “over what range” prominently is written straight into the definitions. In text style (inline), all ten fall back to the lower right.

latex
\[
  \lim_{x \to 0} \frac{\sin x}{x} = 1, \qquad
  \max_{1 \le i \le n} a_i .
\]

It is natural to think you could simply apply \limits, which works on \sum and \int, to a function name as well. But with amsmath loaded, \log\limits_{k} does nothing at all — no error, no warning, silently ignored. The reason is line 27 of amsopn.sty: \def\nolimits@{\@ifnextchar\limits{\nolimits\@gobble}{\nolimits}}. Every operator-name definition ends by calling this \nolimits@, and if the next token is \limits it emits \nolimits and then eats that \limits. Measurement confirms it.

measured on TeX Live 2024, 10pt article
% depth of the box tells us where the script went (bigger = below the operator)

% WITHOUT amsmath — \log is \mathop{...}\nolimits, and \limits overrides it
$\displaystyle\log_{k}x$          d = 2.44443pt
$\displaystyle\log\limits_{k}x$   d = 9.47220pt   % moved below

% WITH amsmath — the \limits is swallowed by \nolimits@
$\displaystyle\log_{k}x$          d = 2.44443pt
$\displaystyle\log\limits_{k}x$   d = 2.44443pt   % unchanged: nothing happened

So the general rule for \limits — that it applies to big operators such as \sum, as covered on “Sums, integrals & big operators” — does not extend to operator names under amsmath. The opposite direction passes through untouched, so \lim\nolimits_{k} really does push \lim’s script to the lower right. When you want a script underneath, the correct answer is not \limits but the starred declaration in the next section.

\DeclareMathOperator and \operatorname: what the star decides

For a name that is not on the list — the sign function sgn, the trace tr, the rank rank, ess sup, argmax — amsmath takes over. For a one-off, write \operatorname{sgn} x; for repeated use, declare it in the preamble with \DeclareMathOperator{\sgn}{sgn}. As the measurement at the top of this page shows, a declared \sgn is exactly as wide as \operatorname{sgn} — they call the same \qopname and are literally the same thing. The advantage of declaring is not quality but keeping it in one place: change your notation later and it is a single line in the preamble.

preamble
\usepackage{amsmath}
\DeclareMathOperator{\sgn}{sgn}            % scripts to the right, like \log
\DeclareMathOperator*{\argmax}{arg\,max}   % scripts underneath, like \lim

% in the body:
% \[ \sgn x, \qquad \argmax_{x \in S} f(x) \]

All the star decides is where the scripts go. Without it you get a member of the \log family (lower right); with it, a member of the \lim family (underneath, in displays). For a one-off, \operatorname*{…} does the same. This too can be checked by measurement: setting the same op in a display with _{n\to\infty} attached, the box depth is 2.44443pt without the star and 8.94444pt with it. The extra depth is the script hanging below the symbol. The \, inside the name (arg\,max) is there for the same reason as in the definition of \limsup: to keep a proper gap between the two words.

amsmath also adds vocabulary around upper and lower limits. \varlimsup and \varliminf are variants that put a bar over (or under) “lim”, more symbolic than the “lim sup” spelling of \limsup. \injlim and \projlim set direct and inverse limits as “inj lim” and “proj lim”, while \varinjlim and \varprojlim give the familiar category-theory form with an arrow → (or ←) laid under “lim”. All of them, like a starred declaration, put scripts underneath. Which to choose is a matter of your field’s convention; when in doubt, check whether your target journal’s style file already defines one.

\bmod, \pmod, \mod, \pod: telling the four apart by measurement

The reason the four modular forms are confusing is that all of them print “mod” and differ only in spacing and parentheses. \bmod and \pmod are in standard LaTeX; \mod and \pod come from amsmath. The design difference is plain in the definitions: \bmod alone is defined with \mathbin{…} — that is, as a Bin atom, the same class as + and - — while the other three merely insert a space and then set their contents, working as a trailing annotation.

amsmath.sty, lines 905-912
\DeclareRobustCommand{\bmod}{\nonscript\mskip-\medmuskip\mkern5mu\mathbin
  {\operator@font mod}\penalty900
  \mkern5mu\nonscript\mskip-\medmuskip}
\DeclareRobustCommand{\pod}[1]{\allowbreak
  \if@display\mkern18mu\else\mkern8mu\fi(#1)}
\DeclareRobustCommand{\pmod}[1]{\pod{{\operator@font mod}\mkern6mu#1}}
\DeclareRobustCommand{\mod}[1]{\allowbreak\if@display\mkern18mu
  \else\mkern12mu\fi{\operator@font mod}\,\,#1}

Look at \if@display. \pod and \pmod insert 18mu in a display and 8mu inline, and \mod inserts 18mu and 12mu. \bmod has no such branch — as a binary operator it simply places \mkern5mu symmetrically on both sides — which makes it the only one of the four whose width is the same in a display and inline. Measuring the same expression a … n at 10pt turns the design straight into numbers.

CommandOutputWidth inline → displayWhen to use
a \bmod na mod n36.01039pt → 36.01039pt (unchanged)When you mean the remainder as a value. A Bin atom, so it is symmetric. Standard LaTeX
a \pmod{n}a (mod n)46.01036pt → 51.56578ptThe standard tail of a congruence, x \equiv y \pmod{n}. Standard LaTeX
a \mod{n}a mod n40.45473pt → 43.78798ptFor congruences in styles that avoid parentheses; wider leading space than \bmod. Needs amsmath
a \pod{n}a (n)23.51038pt → 29.06580ptParentheses without the word “mod”, for when the modulus is obvious from context. Needs amsmath

Subtract the inline width from the display width and the definitions reappear exactly. For \pmod and \pod the difference is 5.55542pt — that is 18mu − 8mu = 10mu, and at 10pt 9.99976 − 4.44434 = 5.55542pt. For \mod it is 3.33325pt, which is 18mu − 12mu = 6mu. For \bmod it is zero. The choice between them is a question of meaning. Write \bmod when you mean the remainder as a value (5 \bmod 3 = 2), and \pmod when you assert that two numbers are congruent modulo n (17 \equiv 5 \pmod{12}). Confuse the two and the reader loses the distinction between a value and a relation.

latex
\[
  5 \bmod 3 = 2, \qquad
  17 \equiv 5 \pmod{12}.
\]