Stage 3 · Mathematics 24 / 48 ≈ 17 min read

Math Spacing in LaTeX: thin spaces and function names

Every space inside a formula comes from the class of the atoms around it. This lesson covers the mu scale, the spacing commands, why a bare function name sets as a product of variables, and the colon, bar and unit idioms that make maths readable.

    Typed spaces do nothing; atom classes do everything

    Inside maths mode the blanks you type are discarded, and that is not a limitation but the design. TeX builds a formula as a list of atoms in eight classes — ordinary, operator, binary, relation, opening, closing, punctuation and inner — and the space between two neighbours is a lookup in a table keyed by their classes. a = b and a = b are the same line because the relation supplies its own glue; ab and a b are the same because ordinary symbols take no space between them, which is exactly why Profit sets as a product.

    The three widths are registers: \thinmuskip is 3mu, \medmuskip 4mu and \thickmuskip 5mu, and the table uses them as relation gets thick glue on both sides, a binary sign gets medium glue on both sides, punctuation gets thin glue after it only, an opening or closing fence gets nothing on its outer side, and an inner atom gets thin glue on both sides. One consequence explains a lot of small irritations: those registers are glue, with stretch and shrink components, so the same + has slightly different air near a line break than at the middle of a display.

    So when a formula looks wrong, the diagnosis is nearly always a class assignment, not a missing space. sin x is italic and cramped because four ordinary atoms sit side by side. f : A \to B is tight because : is punctuation. -x at the start of a wrapped line loses its air because the minus has become a binary operator with nothing on its left. Adding \; over the symptom leaves the cause in place and shows up again on the next line — the fixes below address the class.

    a\,b \quad a\:b \quad a\;b \quad a\quad b \quad a\qquad b \quad a\!b
    a\,b \quad a\:b \quad a\;b \quad a\quad b \quad a\qquad b \quad a\!b
    x_{\max} \qquad x_{\text{max}} \qquad x_{\mathrm{max}}
    x_{\max} \qquad x_{\text{max}} \qquad x_{\mathrm{max}}
    Math atom classes decide every automatic space: relation, binary operator, punctuation, fence and operator, plus the ord-versus-op difference that limits reveal Every gap below was inserted by TeX from the class of the atoms on either side a ord = rel · 5mu each side b ord + bin · 4mu each side c , punct · 3mu after only d e comma then space in source is one punctuation atom: you type nothing extra a = b + c, d same source, no explicit space at all A binary minus at the start of a line loses its spacing: {} - x \qquad (-x) \qquad \bigl(-x\bigr) an empty group or a fence re-classes it as unary The class difference you can actually see: ordinary versus operator \mathrm{max}_{x \in S} f  (ordinary atom) max x∈S f the subscript is glued to the side in every style; it never stacks, and \limits after it is an error: ! Limit controls must follow a math operator \operatorname*{max}_{x \in S} f  (operator) max x∈S f the star marks it as a display-limits operator, so the condition centres underneath in display maths and moves to the side inline — exactly like \sum and \lim
    Every gap in a formula comes from the atom classes on either side of it, and only you can change them.

    The spacing commands, measured

    The manual commands are the same registers used by hand: a\,b adds 3mu, a\:b 4mu, a\;b 5mu, a\quad b 18mu — which is one em, so it scales with the surrounding size — and a\qquad b 36mu. \! is the only negative one at minus 3mu, and \ (backslash-space, a control space) inserts an actual text-mode word space, which is what makes it useful inside \text{…} for forcing a visible gap where a space would be trimmed.

    For anything more explicit, TeX gives \mkern and \mskip, which take mu units and therefore scale with the maths style; \hskip and \hspace{6pt} take text units, which is why they are the wrong tool inside a formula — they do not scale in a subscript, they do not stretch at a line break, and a pt value you chose for body size is too tight in a display and too loose in a footnote.

    A rough allocation of the six: \, for a differential, a unit, and between the parts of a two-word function name; \; to set off a trailing condition or to separate quantifiers; \quad between two statements sharing one displayed line and before a \text{if} clause; \qquad between genuinely independent cases; \! only to tighten something the automatic classes made too airy. If you find yourself adding space around a relation or a binary sign, stop — that glue is already there, and doubling it is the classic sign of spacing by eye instead of by structure.

    \text{if } x \ge 0 \qquad \text{for all } x \ne 0 \qquad \text{, }
    \text{if } x \ge 0 \qquad \text{for all } x \ne 0 \qquad \text{, }
    The maths spacing ruler in mu units: thin, medium and thick spaces, quad and qquad, the negative thin space, and the three glue registers TeX uses automatically 1 mu = one eighteenth of an em — every width below scales with the surrounding style, not with points \, 3mu thin — before a differential, before a unit, inside a function name x y \: 4mu medium — the glue TeX already puts around a binary sign; use it sparingly x y \; 5mu thick — separates a formula from its trailing \text{if} / \text{for} x​y \quad 18mu = 1em — two statements on one displayed line, one case apart x       y \qquad 36mu = 2em — the reading gap between two cases, or a formula and its name \! minus 3mu — the only negative space you should reach for routinely f (x) 0 1 em 2 em em in the current size widths are drawn to scale at 13px per em What TeX already does, and the three registers behind it \thinmuskip=3mu \medmuskip=4mu \thickmuskip=5mu \, \: \; are those registers used by hand, so a \; next to an = buys you 10mu of dead air The registers are glue: they shrink at a line break, which is why two identical formulas can have different air around them on different lines never fight that with \mkern or \hskip inside maths
    The mu scale, drawn to size, and the three registers the automatic glue comes from.

    Why a bare function name is wrong, and what fixes it

    The failure is not cosmetic: sin x in maths mode is four italic variables multiplied together, with your space deleted and no upright roman anywhere. The kernel therefore pre-declares the standard names — \sin, \cos, \tan, \log, \ln, \exp, \det, \dim, \ker, \deg, \gcd, \max, \min, \sup, \inf, \lim, \Pr and friends — each as an operator in upright roman: the right shape, the right class, and the right limit behaviour.

    Names outside that list are yours to declare, and there are three levels of commitment. Inline and one-off: \operatorname{rank} A — amsmath takes your argument, sets it upright in the maths font, and gives it the operator class, so spacing around it is automatically correct and a space inside the argument is preserved as a word space. Document-wide: \DeclareMathOperator{\rank}{rank} in the preamble, which is one definition instead of forty retyped calls and survives a future rename. Need the condition to drop underneath in a display? Add the star: \DeclareMathOperator*{\argmin}{arg\,min}, or write \operatorname*{arg\,min}_{x \in C} f(x) for a single use.

    The near-miss that generates most of these questions is \mathrm{max}. It looks identical and behaves differently: \mathrm produces an ordinary atom, so a following subscript is stuck at the side in a display, the spacing rules for an operator never apply, and \limits after it is a hard error — TeX reports that limit controls must follow a math operator. \text{max} is a third thing again: a word in the text font, which is correct for a label like x_{\text{max}} and wrong for anything a reader has to parse as an operator.

    sin x \qquad \sin x
    sin x \qquad \sin x
    \max_{x \in C} f(x) \qquad \mathrm{max}_{x \in C} f(x)
    \max_{x \in C} f(x) \qquad \mathrm{max}_{x \in C} f(x)
    \documentclass[11pt,a4paper]{article}
    \usepackage[T1]{fontenc}
    \usepackage{amsmath,amssymb,booktabs}
    
    \title{Three romans, three colons}
    \author{A. Student}
    \date{\today}
    
    \begin{document}
    \maketitle
    
    \section{Upright material: what each command is for}
    \begin{center}
    \begin{tabular}{@{}llp{7.2cm}@{}}
    \toprule
    command & class & use it for \\
    \midrule
    \verb|\max| & operator & names already declared in the kernel --- upright,
      takes limits, no braces needed \\
    \verb|\operatorname{rank}| & operator & a one-off function name inside a
      formula; \verb|\operatorname*{arg\,min}| if it must take limits below \\
    \verb|\mathrm{kg}| & ordinary & a symbol that is not a product of variables:
      units, labels, \verb|\mathrm{d}x| \\
    \verb|\text{if }| & text & real words: keeps its spaces, follows the
      surrounding text font --- which is why inside a theorem it comes out
      italic \\
    \bottomrule
    \end{tabular}
    \end{center}
    
    \section{Colons and bars}
    \[
      f \colon A \to B,\quad
      \{x \in A : x \gt 0\},\quad
      \{x \in A \mid x \gt 0\},\quad
      \Pr(A \mid B),\quad
      a \mid b
    \]
    The first is a map (small space before, wider after); \verb|\mid| is a
    relation, so it is spaced on both sides and is the right choice in set
    builders and conditioning; the plain \verb|:| is punctuation and hugs the
    symbol before it. \verb|\middle| is for a bar that has to scale with a
    \verb|\left| \verb|\right| pair.
    
    \end{document}

    Two-word names, and the thin space inside them

    Proper multi-word operator names are not one word with a space in it. The convention is a thin space between the parts — \operatorname{arg\,min}, \operatorname{ess\,sup}, \DeclareMathOperator*{\limsupinj}{lim\,sup\,inj} for the exotic cases — because a word space inside an operator name renders as a gap sized for running prose, which looks detached at maths size and disappears entirely when the name lands in a subscript.

    This is also where the star earns its keep. \operatorname{arg\,min}_{x} f(x) is legal and puts the condition at the side even in a display; the starred form takes it underneath, which is how "the argument that minimises" reads as a single object with a scope rather than as a name with a stray subscript.

    One more consequence worth knowing: because a declared operator is an operator, it is the thing a subscript can attach to as a limit. That is why \rank_{\mathbb{F}}(V) behaves like \sum_{i} rather than like x_{i}, and why the same construct on a \mathrm{…} name looks subtly wrong on the page even though the source looks equivalent.

    \operatorname{rank} A \qquad \operatorname{rank}_{\mathbb{F}}(V) \qquad \operatorname{im} T
    \operatorname{rank} A \qquad \operatorname{rank}_{\mathbb{F}}(V) \qquad \operatorname{im} T
    \Pr(X \gt t) \qquad \operatorname{Var}(X) \qquad \mathbb{E}[X \mid \mathcal{F}] \qquad \operatorname{supp} f
    \Pr(X \gt t) \qquad \operatorname{Var}(X) \qquad \mathbb{E}[X \mid \mathcal{F}] \qquad \operatorname{supp} f

    Units, differentials and the thin-space idiom

    A number and its unit are two atoms, and the space between them must be a maths space: 5\,\mathrm{kg}, not 5 kg (whose blank is discarded and whose letters are variables multiplied). The unit goes upright for the same reason a function name does — an italic kg reads as k times g. Compound units are a product of upright atoms, so either join them with a thin space and negative exponents, \mathrm{kg}\,\mathrm{m}^{-3}, or use a slash inside one upright group, \mathrm{m/s}. Percent needs escaping (5\,\%), degrees are 30^{\circ}, and a temperature is \qty{20}{\celsius} once siunitx is loaded.

    The differential gets the same treatment: \int_{0}^{1} x^{2}\,\mathrm{d}x. The \, is what tells the reader that \mathrm{d}x is the operator being applied and not a factor of the integrand, and the roman d stops the pair reading as a product of two variables. A bare \mathrm{d} is an ordinary atom, so the small defect is that a preceding minus or relation does not space around it properly; the one-line fix in a preamble is \newcommand{\dd}{\mathop{}\!\mathrm{d}}, which makes the d behave like a prefix operator and then cancels the space that classification adds.

    If the document contains measurements rather than the occasional unit, siunitx is the right tool: \qty{1000}{\kg\per\m\cubed} (v3; \SI{}{} in older releases) handles the thin space, the upright font, the product and per signs, thousands separators and exponent notation from one declaration, so a unit change never means hunting for hand-typed \,s. The spacing decision is then made once, in the preamble, which is the pattern for every idiom on this page.

    m = 5\,\mathrm{kg} \qquad \rho = 1000\,\mathrm{kg}\,\mathrm{m}^{-3} \qquad \alpha = 30^{\circ}
    m = 5\,\mathrm{kg} \qquad \rho = 1000\,\mathrm{kg}\,\mathrm{m}^{-3} \qquad \alpha = 30^{\circ}
    \int_{0}^{1} x^{2}\,\mathrm{d}x \qquad \int_{0}^{1} x^{2}\mathrm{d}x
    \int_{0}^{1} x^{2}\,\mathrm{d}x \qquad \int_{0}^{1} x^{2}\mathrm{d}x

    Colons, bars and the separator family

    Three characters look alike and space completely differently. A plain : is punctuation, so TeX puts no space before it and thin glue after it: f : A \to B sets the colon against the f, which is not what a mapping statement wants to say. \colon exists for exactly that construction — it is defined as an unbreakable small space, a colon, and a wider space after, so the name stays bound to its domain and the reader sees the boundary. And \mid is a relation with thick glue on both sides, which is the correct shape for a separator that is not part of either side: \{x \in \mathbb{R} \mid x \gt 0\}, \Pr(A \mid B), \mathbb{E}[X \mid \mathcal{F}].

    Two neighbours of that family are easy to confuse. a \mid b is divisibility, also a relation, and if both uses appear in one document it is worth a sentence of notation to say which is which. \middle is not a spacing command at all — it is the auto-sizing counterpart of \left and \right for a bar in the middle of a construct: \left\{ x \;\middle\vert\; x \gt 0 \right\} scales the bar with the braces, and the \; on either side is what keeps it from touching them.

    For definition symbols, the colon-equals family is now usually set with \coloneqq, \eqqcolon and \Coloneqq from mathtools, which centre the colon on the math axis and join it to the equals sign at the right kern; the ad hoc := puts a punctuation colon next to a relation and always sits a fraction too high.

    f \colon A \to B \qquad f : A \to B
    f \colon A \to B \qquad f : A \to B
    \{x \in \mathbb{R} : x \gt 0\} \qquad \{x \in \mathbb{R} \mid x \gt 0\} \qquad \left\{ x \;\middle\vert\; x \gt 0 \right\}
    \{x \in \mathbb{R} : x \gt 0\} \qquad \{x \in \mathbb{R} \mid x \gt 0\} \qquad \left\{ x \;\middle\vert\; x \gt 0 \right\}

    Tightening: negative space and the air fences leave

    \! is used for two situations and no more. The first is an operator followed by an auto-sized fence: \Gamma\!\left(\tfrac{1}{2}\right), \lim_{n \to \infty}\!\left(1+\tfrac{1}{n}\right)^{n}. The gap is real — the fence contributes its own side bearing and the group it opens is an inner atom — and a single negative thin space is what a typesetter would use. The second is pairing two constructs that touch: \mathcal{L}\!\left(f\right), or the \mathrm{d}\! in some differential layouts.

    What \! cannot do is fix structure. If a name is italic and cramped it was not declared as an operator; if a colon hugs the wrong symbol it is the wrong command; if a unit is glued to a number the number and the unit are in the same group. Double negatives — \!\!, and the hand-built \int\!\!\int — are the same mistake in the other direction, and amsmath supplies the real glyph for that case.

    The other half of the fix is telling TeX which class you want. A unary minus at the start of a line is a binary minus with a missing left neighbour, which is why {}-x, (-x) or \mathopen{}-x are the three standard repairs: the empty group or the opening fence gives the minus a neighbour without printing anything. The same trick in reverse — \mathclose{} — restores ordinary spacing after a group that amsmath made inner, which is occasionally what a superscript on a \left(\right) pair needs.

    \Gamma\!\left(\tfrac{1}{2}\right) = \sqrt{\pi} \qquad \lim_{n \to \infty}\!\left(1+\tfrac{1}{n}\right)^{n}
    \Gamma\!\left(\tfrac{1}{2}\right) = \sqrt{\pi} \qquad \lim_{n \to \infty}\!\left(1+\tfrac{1}{n}\right)^{n}
    {}-x \qquad f(-x) \qquad \sin(-x) \qquad a \cdot {}-b
    {}-x \qquad f(-x) \qquad \sin(-x) \qquad a \cdot {}-b

    Words inside maths, and spacing at the row level

    \text{…} is the one place inside a formula where typed spaces count — but only the ones inside the braces. \text{if } x \ge 0 keeps the space because it is part of the argument; \text{if} x \ge 0 prints "ifx≥0", which is the single most common visible spacing bug in piecewise definitions. The same applies to a comma: \text{, } is how you say "comma and a space" inside maths, and \text{for every } x \ne 0 is how a quantifier reads as English instead of as a variable run.

    Remember also that \text switches to the surrounding text font and size. Inside an italic theorem statement, \text{kg} comes out italic — a real typographic error for a unit — where \mathrm{kg} stays upright, because \mathrm takes the upright shape of the maths font and nothing else. That single difference is the clean rule: words in \text, symbols in \mathrm, operator names in \operatorname or a declared macro.

    Beyond the atom level there is row and column spacing, and it is configured rather than typed. The extra vertical space between rows of aligned, gathered or cases is the length \jot (6pt by default): change it once in the preamble with \setlength{\jot}{9pt}, or for one awkward row with \\[6pt] — never with an empty row, which also moves the alignment. Between columns there is no glue to rely on, so a two-pair derivation line separates its halves with an explicit \quad, and equal heights across rows come from a strut (\mathstrut, or \vphantom{\frac{a}{b}}) rather than from spaces.

    A final pair of details that a copy-editor will catch: the dots are amsmath's job, because \dots looks at the atom that follows and picks baseline dots before a comma (a_1, \dotsc, a_n) and centred dots around binary signs (a_1 + \dotsb + a_n), so hard-coding \ldots or \cdots is one less correct decision than typing \dots; and punctuation that belongs to your sentence, not the formula, goes outside the maths — \lVert x \rVert = 1, \quad \text{…} with the comma after the closing symbol, not inside it.

    \lVert A x \rVert_{2} = \lVert x \rVert_{2} \quad \text{for every } x \ne 0
    \lVert A x \rVert_{2} = \lVert x \rVert_{2} \quad \text{for every } x \ne 0
    a_1 + \dotsb + a_n \qquad a_1, \dotsc, a_n \qquad A_1 \cap \dotsb \cap A_n
    a_1 + \dotsb + a_n \qquad a_1, \dotsc, a_n \qquad A_1 \cap \dotsb \cap A_n
    \documentclass[11pt,a4paper]{article}
    \usepackage[T1]{fontenc}
    \usepackage{amsmath,amssymb}
    \usepackage{siunitx}        % \qty{5}{\kilogram} — numbers and units together
    \usepackage{mathtools}      % \coloneqq, \DeclarePairedDelimiter
    
    % A differential that behaves like a prefix operator (roman d, correct spacing)
    \newcommand{\dd}{\mathop{}\!\mathrm{d}}
    
    % Function names used throughout the document: declare once instead of
    % retyping \operatorname every time
    \DeclareMathOperator{\rank}{rank}
    \DeclareMathOperator*{\argmin}{arg\,min}   % star: the condition goes underneath
    
    % Extra row space inside aligned / cases blocks (6pt by default)
    \setlength{\jot}{9pt}
    
    \title{Spacing decisions, made once}
    \author{A. Student}
    \date{\today}
    
    \begin{document}
    \maketitle
    
    \section{Operators and text}
    \[
      \rank(AB) \le \min\{\rank A,\, \rank B\},
      \qquad
      \argmin_{x \in C} f(x),
      \qquad
      f\colon \mathbb{R}^{n} \to \mathbb{R}^{m}.
    \]
    
    \section{Differentials and units}
    \[
      \int_{0}^{1} x^{2} \dd x = \frac{1}{3},
      \qquad
      \qty{5}{\kilogram},
      \qquad
      v = \qty{1.2}{\metre\per\second}.
    \]
    
    \section{Conditions inside a display}
    \[
      \lVert A x \rVert_{2} = \lVert x \rVert_{2}
      \quad \text{for every } x \ne 0,
      \qquad
      x_{\text{max}} = \max_{i} x_i .
    \]
    The space after \verb|for every| belongs inside the braces; outside them it
    does not exist.
    
    \end{document}

    Lesson cheat sheet

    One key line of code from each section — jump to it or copy it straight from here.

    Section Key code
    a\,b \quad a\:b \quad a\;b \quad a\quad b \quad a\qquad b \quad a\!b
    \text{if } x \ge 0 \qquad \text{for all } x \ne 0 \qquad \text{, }
    \documentclass[11pt,a4paper]{article}
    \operatorname{rank} A \qquad \operatorname{rank}_{\mathbb{F}}(V) \qquad \operatorname{im} T
    m = 5\,\mathrm{kg} \qquad \rho = 1000\,\mathrm{kg}\,\mathrm{m}^{-3} \qquad \alpha = 30^{\circ}
    f \colon A \to B \qquad f : A \to B

    Common mistakes

    • A bare multi-letter name: sin x, max f, Var(X). Those are products of italic variables with your spaces deleted. Use \sin, \max, \operatorname{Var}, or declare the name once with \DeclareMathOperator.
    • \mathrm{max}_{x \in C} f instead of \operatorname{max}: the subscript never stacks in a display, and adding \limits after it is an outright error, because limit controls must follow a math operator.
    • Typing \; or \quad to repair a colon or a fence that is spaced wrongly. The cause is the atom class — \colon for a map, \mid for a separator, \mathopen{} for a fence — and a manual space only hides it on that one line.
    • 5 kg or \int f(x) dx: a typed blank inside maths is discarded and the letters are variables. Write 5\,\mathrm{kg} and f(x)\,\mathrm{d}x.
    • \text{if} x > 0 with the space outside the braces — it prints as "ifx>0". The space has to be part of the argument: \text{if } x > 0.
    • \text{kg} inside an italic theorem, which comes out italic because \text follows the surrounding text font. A unit is a symbol: \mathrm{kg}.
    • \!\! and \int\!\!\int as general-purpose tightening. One \! is a typesetter's correction; two are a symptom, and amsmath has the real glyph for the double-integral case.
    • A unary minus that opens a wrapped line losing its spacing: -x is then a binary operator with no left operand. Write {}-x, (-x) or \mathopen{}-x.

    Questions about this lesson

    How do I add a space in a LaTeX equation?
    With a maths space command, not the space bar: \, is 3mu (thin), \: is 4mu, \; is 5mu, \quad is 1em and \qquad 2em, all scaling with the surrounding size. \! removes 3mu. Typed blanks inside $ … $ are discarded by design because the space between two symbols is chosen from their atom classes.
    What does \, mean in maths mode?
    A thin space of \thinmuskip: 3mu, which is one sixth of an em and therefore scales with the size of the maths around it. Its three real uses are before a differential (\int f(x)\,\mathrm{d}x), between a number and a unit (5\,\mathrm{kg}) and inside a two-word operator name (\operatorname{arg\,min}).
    Why does LaTeX ignore the spaces I type between symbols?
    Because it is not typesetting words: a formula is a list of classified atoms and TeX inserts glue according to the classes on either side — thick around a relation, medium around a binary sign, thin after punctuation, none between two ordinary symbols. That is why a b is ab and why "Profit" in maths is a product.
    Should I use \text, \mathrm or \operatorname for a name in a formula?
    \operatorname for a function or operator name (one use) and \DeclareMathOperator for a document-wide one, because both give the operator class and hence correct limit behaviour. \mathrm for upright symbols that are not operators, such as units and \mathrm{d}. \text for real words, since it honours spaces and uses the text font — which also means it inherits italics inside a theorem.
    What is the difference between : and \colon in LaTeX?
    A plain colon is punctuation, so it gets no space before and a thin space after. \colon is defined for mappings — a small unbreakable space before the colon and a wider one after — so f\colon A \to B reads correctly. For a separator inside a set builder or a conditional probability, the relation-class \mid is the one that gets balanced space on both sides.
    Why is there a gap after \left( and how do I close it?
    The group between \left and \right is an inner atom and the fence carries its own side bearing, so f\left(x\right) sits looser than f(x). A single \! before the fence is the conventional repair, and the mleftright package avoids the problem by auto-sizing without reclassifying the group.
    How do I get more space between lines of an aligned block?
    Row spacing there is the length \jot, 6pt by default: \setlength{\jot}{9pt} in the preamble changes it everywhere, and \[6pt] on one line break lifts a single row. Never insert an empty row for space — it moves the alignment columns of the block.

    Live playground: edit the LaTeX, see it render

    The preview updates as you type — break it on purpose to see how errors look.

    KaTeX error
    Try an example

    Keep reading

    Three ways into the same idea: the notation itself, the lesson that teaches it, and an article that applies it.