What is the purpose of : in math mode?

What is the purpose of $:$?

We know that in math mode one should write in natural language and not being so strict with math notation.

For example: $f:A\to B$ is not "correct", the correct way would be $f\colon A\to B$ (and variants).

I do not remember any math book that uses the colon as part of a sentence in math mode.

The only 2 uses I can give to $:$ are when learning about divisions ($2:4=1:2$) and scales ($1:2$).

Does (La)TeX give a meaning of : in math mode?

• The colon is not used for division in USA/British tradition. In scale ratios, I'd prefer spacing the colon as a relation rather than an operation. – egreg May 16 at 7:39

The : is a relational symbol in TeX. In the TeXbook it is used in conjunction with =.

Plain \TeX\ treats the four characters |=|, |<|, |>|, and |:|\ as
^{relations}'' because they express a relationship between two
quantities. For example, ${x<y}$' means that $x$~is less than~$y$.
Such relationships have a rather different meaning from binary
operations like $+$, and the symbols are typeset somewhat differently:
\beginmathdemo
|$x=y>z$|&x=y>z\cr
|$x:=y$|&x:=y\cr
|$x\le y\ne z$|&x\le y\ne z\cr
|$x\sim y\simeq z$|&x\sim y\simeq z\cr
|$x\equiv y\not\equiv z$|&x\equiv y\not\equiv z\cr
|$x\subset y\subseteq z$|&x\subset y\subseteq z\cr
\endmathdemo

In the comments, egreg pointed out another example in the TeXbook, where : is used to separate the condition from the set variable.

\beginmathdemo
|$\{\,x\mid x>5\,\}$|&\{\,x\mid x>5\,\}\cr
|$\{\,x:x>5\,\}$|&\{\,x:x>5\,\}\cr
\endmathdemo
(Some authors prefer to use a ^{colon} instead of $\mid$', as in the second
example here.)

The colon has the exact same meaning in Plain TeX and LaTeX, as can be verified using this simple document:

\showthe\mathcode:
\csname @@end\endcsname
\bye

The output is

$pdftex test.tex This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdftex) restricted \write18 enabled. entering extended mode (./test.tex > 12346. l.1 \showthe\mathcode: ? ) No pages of output. Transcript written on test.log.$ pdflatex test.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2018-12-01>
> 12346.
l.1 \showthe\mathcode`:

?
)
No pages of output.
Transcript written on test.log.

Here the same table rendered in LaTeX: \documentclass{article}
\usepackage{array}
\usepackage{amsmath}
\usepackage{listings}
\lstset{basicstyle=\ttfamily}
\begin{document}
\lstMakeShortInline|
\begin{tabular}{l>{$}l<{$}}
|$x=y>z$|                 & x=y>z                 \\
|$x:=y$|                  & x:=y                  \\
|$x\le y\ne z$|           & x\le y\ne z           \\
|$x\sim y\simeq z$|       & x\sim y\simeq z       \\
|$x\equiv y\not\equiv z$| & x\equiv y\not\equiv z \\
|$x\subset y\subseteq z$| & x\subset y\subseteq z \\
\end{tabular}
\end{document}
• I think := should be typeset with \coloneqq: tex.stackexchange.com/a/4217/156344. One should not use $a:=b$ as Knuth said. – user156344 May 16 at 6:25
• @Mico If you look in the Metafontbook, you will see “:=” clearly input as :=. Whether the mathtools macros \coloneqq should be used is a question of personal choice and taste (I would never use := in my math, of course). I agree that := is by no means “the” intended usage (see page 174 in the TeXbook for the main one). In proportions such as a:b=c:d I'd not treat the colon as a binary operation. – egreg May 16 at 7:36
• @HenriMenke It is not “the” (in the sense of the unique) intended usage. – egreg May 16 at 7:53
• @JouleV -- While \coloneqq exists now (and should now be used), it did not exist when TeX was created -- there simply wasn't enough memory to accommodate all possible symbols. – barbara beeton May 16 at 13:33
• @egreg For := I use always your \mathrel{\mathop:}= :-) from Comprensive list symbols. – Sebastiano May 16 at 15:08

Quite often, one sees : employed in place of a vertical bar (|) to denote conditioning on some event or condition. E.g., both $E(X : X>0)$ and $E(X\mid X>0)$ would express the "expectation of the random variable X conditional on this random variable taking on positive values".

By default, TeX treats both : and \mid -- but not | and \vert -- as "relational" operators and inserts an amount of whitespace around them that's also used for other relational operators such as <, =, and >.

For other, i.e., non-conditioning math uses of the colon symbol, one should employ \colon, \coloneq (or, depending on the package that's in use, \coloneqq, etc.