21

What effect does the tilde character ~ have in math mode? Is it the same non-breaking space that it is in text mode? Is it bad style to use ~ in math mode? I've seen it used in some examples but have not seen it explained in any of the usual LaTeX references.

23

Using ~ or \⍽ (control space, just in order to make it clear) in math mode is not equivalent, as the following example shows:

\documentclass{article}
\begin{document}

$a=\ =b$

$a=~=b$

\end{document}

This produces

enter image description here

Why is that? Because the definition of ~ is

1299 \DeclareRobustCommand{\nobreakspace}{%
1300    \leavevmode\nobreak\ }
1301 \catcode `\~=13
1302 \def~{\nobreakspace{}}

(line numbers are those in latex.ltx). Thus ~ also adds a {} group that's significant in math mode and explains the difference in output: in the first line we have (Ord stands for an atom of class “ordinary”, Rel for an atom of class “binary relation”)

Ord Rel <skip> Rel Ord

and TeX ignores explicit skips when deciding what math spacing to insert. So by rule it inserts a thick space between Ord and Rel and between Rel and Ord, but no space between Rel and Rel. The <skip> due to \⍽ is inserted back when converting the math list to a horizontal box.

In the second case we have

Ord Rel <skip> Ord Rel Ord

because in math mode {} counts as an Ord atom. Thick spaces will be inserted at either side of the empty Ord atom.

Thus it's better to use \⍽ instead of ~, in order to avoid surprises. However, the usage should be limited to separating parts of a formula that need to be considered as words, typically in displays. There's no difference between

\[
\sin\pi = 0\ \text{and}\ \cos\pi = -1
\]

and

\[
\sin\pi = 0 \text{ and } \cos\pi = -1
\]

Just a question of personal preference, because either will use the interword space relative to the current font outside of math, without stretching or shrinking. For spacing math symbols the best is to use \mkern or \mskip (possibly the latter, for which amsmath provides the \mspace interface, analog to \hspace).

Note that in inline math mode \⍽\text{and}\⍽ and \text{⍽and⍽} are different, as the former inserts spaces that may participate to stretching and shrinking, whereas the latter inserts “frozen” spaces. However, something like

the set $N_n(R)=\{\,x\in R: x^{n-1}\ne 0\ \text{and}\ x^{n}=0\,\}$

is more properly written as

the set $N_n(R)=\{\,x\in R: x^{n-1}\ne 0$ and~$x^{n}=0\,\}$

so as to give TeX more chances to properly break the line.

  • sigh. I missed the {} I suppose I'll have to delete mine now. – David Carlisle Sep 29 '14 at 9:58
  • Why LaTeX includes this {} in the code of ~? This seems as absurd from first point of view. – wipet Sep 29 '14 at 10:04
  • 4
    Very very useful information! However I am slightly scared by the last example...I see the TeXnical point but it seems "wrong" semantically...is there really no better way? – Bordaigorl Sep 29 '14 at 16:09
  • 2
    @egreg ...the style of last example can also lead to errors, for example: $\left\{\frac{A}{B}: A>0$ and $B<9 \right\}$ as opposed to $\left\{\frac{A}{B}: A>0 \text{ and } B<9 \right\}$ which however cannot be broken... – Bordaigorl Sep 29 '14 at 16:15
  • 2
    @egreg ok, my example was artificial, just to illustrate the issue...I am just asking since $ $ couples are conceptually like parenthesis and the last example of your answer is not well-parenthesised... – Bordaigorl Sep 29 '14 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.