# How to properly handle non-breaking spaces in math mode?

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 have no idea what you are asking. Do you want to get breaks or avoid them? And under which conditions? The best would be if you would show a complete complete example. Commented Jun 2, 2018 at 19:22
• @UlrikeFischer thank you for the feedback. I tried to add pictures to improve clarity. Is this better? Commented Jun 2, 2018 at 19:33

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.

• Thank you, @egreg! I learnt a lot from your answer. Didn't know about formulas not splitting at comma or mathtt vs texttt. Much appreciated! Commented Jun 2, 2018 at 21:12

You can suppress breaks in math with additional braces:

\documentclass{article}
\usepackage{amsmath}
\textwidth=1cm
\begin{document}

$\text{abc} = C$

${\text{abc} = C}$ %no break

\end{document}


• I tried update my code like this: ${\descendantsplus{\texttt{A}} = \{ \ texttt{A}, \texttt{B}, \texttt{C} \}}, {\descendantsplus{\texttt{B}} = \{ \texttt{B} \}}, {\descendantsplus{\texttt{C}} = \{ \texttt{C} \}}, {\parentplus{\texttt{A}} = \{ \texttt{A} \}}, {\parentplus{\texttt{B}} = \{ \texttt{A}, \texttt{B} \}}, {\parentplus{\texttt{C}} = \{ \texttt{A}, \texttt{C} \}}$ And I am in the same scenario as when I add ~. The line doesn't split in between the different sets. Commented Jun 2, 2018 at 19:50