# [Tex/LaTex] How to properly handle non-breaking spaces in math mode

math-modespacing

I'm trying to print a list of sets inline to illustrate some definitions. My code is something like:

$\descendantsplus{\texttt{A}} = \{ \texttt{A}, \texttt{B}, \texttt{C} \},\ \descendantsplus{\texttt{B}} = \{ \texttt{B} \}$


and it produces:

I don't like that descendants(C) = { C } is split on two different lines. I tried to add $~$ like this:

$\descendantsplus{\texttt{A}}~=~\{~\texttt{A},~\texttt{B},~\texttt{C}~\},\ \descendantsplus{\texttt{B}}~=~\{~\texttt{B}~\}$


The result is the following:

So the line doesn't break at all and creates an infinitely long line.

I read the answer to this question: What does the tilde character (~) do in math mode?, and it seems that ~ is not what I want to use as separator, but I find that the solution the answer offers to properly split the line is not satisfactory:

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.

Is there any neat way to handle non-breaking spaces in math mode?

I can reproduce your first image with

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage[sc]{mathpazo}
\usepackage{microtype}

\usepackage{showframe} % just for the example

\newcommand{\ancestorplus}[1]{\mathit{ancestor}^{+}(#1)}
\newcommand{\descendantsplus}[1]{\mathit{descendants}^{+}(#1)}

\begin{document}

\noindent
we have:
$\descendantsplus{\mathtt{A}} = \{ \mathtt{A}, \mathtt{B}, \mathtt{C} \}$,
$\descendantsplus{\mathtt{B}} = \{ \mathtt{B} \}$,
$\descendantsplus{\mathtt{C}} = \{ \mathtt{C} \}$,
$\ancestorplus{\mathtt{A}} = \{ \mathtt{A} \}$,
$\ancestorplus{\mathtt{B}} = \{ \mathtt{A,B} \}$,
$\ancestorplus{\mathtt{C}} = \{ \mathtt{A,C} \}$.

\end{document}


Using microtype gives a smaller amount of overfull. Note that, differently from your code, this is six different formulas. A single one is wrong and gives TeX even less chances to split a line, because breaking at commas inside formulas is disallowed, so only binary relations are feasible break points.

There is little chance to typeset this paragraph without splitting after = anyhow, unless you're able to move “we have:” to the line above.

You can disallow breaking after a particular = sign by using

=\nolinebreak


but this wouldn't help in the first two cases of the following tests, because there's no way to fit \{\mathtt{C}\} in the line. Try it in the second example.

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage[sc]{mathpazo}
\usepackage{microtype}

\usepackage{showframe} % just for the example

\newcommand{\ancestorplus}[1]{\mathit{ancestor}^{+}(#1)}
\newcommand{\descendantsplus}[1]{\mathit{descendants}^{+}(#1)}

\begin{document}

\noindent
we have:
$\descendantsplus{\mathtt{A}} = \{ \mathtt{A}, \mathtt{B}, \mathtt{C} \}$,
$\descendantsplus{\mathtt{B}} = \{ \mathtt{B} \}$,
$\descendantsplus{\mathtt{C}} = \{ \mathtt{C} \}$,
$\ancestorplus{\mathtt{A}} = \{ \mathtt{A} \}$,
$\ancestorplus{\mathtt{B}} = \{ \mathtt{A,B} \}$,
$\ancestorplus{\mathtt{C}} = \{ \mathtt{A,C} \}$.

\bigskip

\noindent
have:
$\descendantsplus{\mathtt{A}} = \{ \mathtt{A}, \mathtt{B}, \mathtt{C} \}$,
$\descendantsplus{\mathtt{B}} = \{ \mathtt{B} \}$,
$\descendantsplus{\mathtt{C}} = \{ \mathtt{C} \}$,
$\ancestorplus{\mathtt{A}} = \{ \mathtt{A} \}$,
$\ancestorplus{\mathtt{B}} = \{ \mathtt{A,B} \}$,
$\ancestorplus{\mathtt{C}} = \{ \mathtt{A,C} \}$.

\bigskip

\noindent
$\descendantsplus{\mathtt{A}} = \{ \mathtt{A}, \mathtt{B}, \mathtt{C} \}$,
$\descendantsplus{\mathtt{B}} = \{ \mathtt{B} \}$,
$\descendantsplus{\mathtt{C}} = \{ \mathtt{C} \}$,
$\ancestorplus{\mathtt{A}} = \{ \mathtt{A} \}$,
$\ancestorplus{\mathtt{B}} = \{ \mathtt{A,B} \}$,
$\ancestorplus{\mathtt{C}} = \{ \mathtt{A,C} \}$.

\end{document}


By the way, \texttt is wrong, because it only changes the font family, but not other attributes, so you'd get italic letters in a theorem statement.