When one should use spacing line \quad or \,
The classic Mathematics into Type, by Ellen Swanson (the AMS has made a PDF copy available here
), gives a good explanation (Section 3.1 SPACING BETWEEN SYMBOLS IN MATHEMATICS) of when to use no space, thin space, thick space, em quad and two-em quad in math mode. A brief summary:
No space
Between two symbols and between a number and the symbol it multiplies.
Before and after subscripts, superscripts, parentheses, braces, brackets, and vertical rules.
In expressions in the subscript or superscript.
Thin space
Before and after symbols used as verbs.
Before and after symbols used as conjunctions.
After, but not before
+
,-
,\pm
,\mp
used as an adjective.After the commas in sets of symbols, sequences of fractions, and coordinates of points.
Before and after the symbols of integration, summation, product, and union.
Before and after functions set in roman type. Exceptions: If any of these functions are preceded or followed by parentheses, braces, brackets, or bars, then the space is eliminated.
Before and after vertical rules appearing singly rather than in pairs; the same rule holds for a colon that is used as a mathematical symbol rather than as punctuation.
Before back subscripts.
Before and after
ds
,dx
, and similar combinations ofd
and another symbol following.
Thick space
Before the parenthesis in congruences in text.
Before a mathematical condition in text.
Em quad
Between a symbolic statement and a verbal expression in displayed expressions.
Around conjunctions.
Two-em quad
Between two separate equations or inequalities in the same line.
Between a symbolic statement and a condition on the statement.
TeX has built-in spacing, and most of the times it does an excellent job, so (most of the times) you don't need to add space manually.
The following document contains the summary of the rules given by Swanson and some examples; these examples also contain some cases which are not built-in and which require manual attention.
\documentclass{article}
\usepackage{amsmath}
\setlength\parindent{0pt}
\begin{document}
\textbf{No space}\\
Between two symbols and between a number and the symbol it multiplies:
\[
ab\qquad xy\qquad 2a\qquad 2xz\qquad 4aC
\]
Before and after subscripts, superscripts, parentheses, braces, brackets,
and vertical rules:
\[
2x^2y_3z (x)y\qquad a\{b\}\qquad y[a]\qquad a\lvert x\rvert\qquad b\lVert y\rVert
\]
In expressions in the subscript or superscript:
\[
\lim_{0\to a}\qquad a^{n-1}
\]
\textbf{Thin space}\\
Before and after symbols used as verbs:
\[
a \subseteq 2
\]
Before and after symbols used as conjunctions:
\[
a +2
\]
After, but not before $+$, $-$, $\pm$, $\mp$ used as an adjective
\[
a= -2
\]
After the commas in sets of symbols, sequences of fractions, and
coordinates of points:
\[ (a,b,c) \]
Before and after the symbols of integration, summation, product, and
union:
\[
a\int x\,\mathrm{d}y
\]
The thin space between $x$ and $\mathrm{d}y$ in the expression above is \emph{not} built
into \TeX.
Before and after functions set in roman type:
\[
a \sin x \qquad \log 2.
\]
Exceptions: If any of these functions are preceded or followed by parentheses, braces, brackets, or bars, then the space is eliminated:
\[
a \sin \lvert x \rvert.
\]
Before and after vertical rules appearing singly rather than in pairs; the
same rule holds for a colon that is used as a mathematical symbol rather
than as punctuation:
\[
a \mid b
\]
Before back subscripts:
\[
a\,_2T_3
\]
The thin space between $a$ and the following back subscript on $T$ in this example is not built into \TeX.
Before and after $\mathrm{d}s$, $\mathrm{d}x$, and similar combinations of $d$ and another
symbol following:
\[
\int f(x)\,\mathrm{d}x\qquad \iiint f(x)\,\mathrm{d}r\,\mathrm{d}\theta\,\mathrm{d}\phi
\]
\textbf{Thick space}\\
Before the parenthesis in congruences in text: $z= a\pmod x$
Before a mathematical condition in text: $t_n\ (n=1,2,\ldots, p)$
\textbf{Em quad}\\
Between a symbolic statement and a verbal expression in displayed expressions
\[
E_n(t) \to e^{-t}\quad\text{as }t\to\infty.
\]
Around conjunctions:
\[
x(a+b)\quad\text{or}\quad y(a-b).
\]
\textbf{Two-em quad}\\
Between two separate equations or inequalities in the same line
\[
x^2 + y^2 = a^2,\qquad x-y=b.
\]
Between a symbolic statement and a condition on the statement
\[
x^n + y^n = a^n\qquad (n = 1,2,\ldots p).
\]
\end{document}
The compiled code looks like :
Addressing why, the table in the AMS' Short Math Guide for LaTeX, discussing spacing in math mode, provides a helpful approach. If we use the elements as intended then TeX will provide good (arguably, best) spacing. However, if the elements are used like an member of another class then adjustments to spacing may be needed:
Class Mnemonic Description Examples
number (Parts of speech)
0 Ord simple / ordinary ("noun") A 0 Φ ∞
1 Op prefix operator \sum
2 Bin binary operator (conjunction) + ∪ ∧
3 Rel relation/comparison (verb) = < ⊂
4 Open left/opening delimiter ( [ {
5 Close right/closing delimiter ) ] }
6 Pun postfix/punctuation . , ; !
Some of these adjustments are made automatically by TeX, e.g.
symbols of class Bin, notably the minus sign −, are automatically coerced to class 0 (no space) if they do not have a suitable left operand.
The spacing for a few symbols follows tradition instead of the general rule: although
/
is (semantically speaking) of class 2, we writek/2
with no space around the slash rather thank / 2
. And comparep|q p|q
(no space) withp\mid q p | q
(class-3 spacing).
As others have pointed out, there are views on what changes to spacing improve the look, or ease of comprehension, of a document. Often an author will have to follow a house style but changes to the default should be considered carefully when a symbol is being used as TeX would expect. It should be noted, too, that best typographic practice varies between languages and over time.
One common case (in math mode) is with integrals:
\int f(x)\,dx
is much better with \,
(even better is \int f(x)\,\textup{d}x
, by the way).
Another common case is with factorials: compare 2!3!4!
to 2!\,3!\,4!
.
Also, some people might argue that \sqrt{\,\log 2}
look better than without a \,
.
Another interesting case is with :
- \{x:x^2-1>0\}
, but f\colon X\to Y
. Formally, you don't use \,
or similar macros, but \colon
is similar: it is hand-made tuning of spacing.
On the other hand, sometimes \!
(a negative thin space) is also needed, although I can't recall the exact example.
(These examples are what I remember from The TeXbook, which I don't have at home; as far as I remember there are more of them there.)
In general, since both semantics and typesetting of math is so complex, it is actually quite often that you need fine-tuning like that.
And \quad
and \qquad
is another story – e.g., spacing between two formulas in display mode.