TeX is only breaking a formula automatically in 2 places: after a binary or a relational operator. They are controlled through the parameters \binoppenalty
and \relpenalty
. If you set both to 10000 there will be no break ever. If you do that locally in a formula then it will apply only there. So a definition like
\newcommand\nobreakformula{\relpenalty10000\relax \binoppenalty 10000\relax}
might be your friend (perhaps with a shorter command name :-))
If you want to prevent only a single break in a formula but not all, then you can use \nolinebreak
at the point where TeX did break. However, you may end up with an even less desirable break so it really depends on the situation (so use this kind of fine-tuning only if your document is next to be ready). For example if in the showcase below we use $a+b =\nolinebreak c+d+e$
then TeX will break after a +
which I think is worse than breaking after the equal sign.
The use of a brace group or the use of \mbox
is less advisable as it will result in the formula always be set at its nominal width regardless of how much stretching or shrinking goes on inside the line. You can see this nicely in the following example:
\documentclass{article}
\usepackage{calc}
\begin{document}
\newlength\normwidth
\newcommand\myline{A test $a+b=c$ with $1+1$ formulas inside}
\settowidth\normwidth{\myline}
Spreading a line b 0, 5,15,25 and 50 points:
\indent \mbox{\myline}
\makebox[\normwidth+5pt][s]{\myline}
\makebox[\normwidth+15pt][s]{\myline}
\makebox[\normwidth+25pt][s]{\myline}
\makebox[\normwidth+50pt][s]{\myline}
\bigskip
Contrast this with using a brace group or \verb=mbox= on the first formula:
\renewcommand\myline{A test ${a+b=c}$ with $1+1$ formulas inside}
\indent \mbox{\myline}
\makebox[\normwidth+5pt][s]{\myline}
\makebox[\normwidth+15pt][s]{\myline}
\makebox[\normwidth+25pt][s]{\myline}
\makebox[\normwidth+50pt][s]{\myline}
\bigskip
\newcommand\nobreakformula{\relpenalty10000\relax \binoppenalty 10000\relax}
Testing \verb=\nobreakformula=:
Some sample text to make this break inside the first formula
A test $ a+b=c + d + e$ with $1+1$ formulas inside
And now with suppression:
Some sample text to make this break inside the first formula
A test $\nobreakformula a+b=c + d + e$ with $1+1$ formulas inside
\end{document}
which results in this output:

\mbox{$k\in\{1\dots,K\}$
give you?k\in
and\{1,...,K\}
. It would probably not be OK to have a line break inside\{1,...,K\}
.mbox
like: $\mbox{No Break This Text}$. Thus, \mbox{$k\in\{1\dots,K\}$} would keep it together in your text