I would say
\newcommand{\x}[1]{%
{}$% get out of math
\kern-2\mathsurround % in case it's non zero
$% reenter math
\binoppenalty10000 \relpenalty10000 #1% typeset the subformula
{}$% get out of math
\kern-2\mathsurround % in case it's non zero
$% reenter math for the rest of the formula
}
TeX breaks formulas only after binary operators or relation symbols, the desirability of such breaks is measured by the two mentioned parameters. However the values used the penalties are those valid at the end of the formula, so simply enclosing #1
in \begingroup...\endgroup
and setting the values wouldn't do anything.
Of course this can work only if used in suitable places of the formula, for example $a+\x{b+c}$
would have the right spacing after the first +
(because of the empty subformula); the last empty subformula does nothing.
My opinion is still that bad breaks must be solved with suitably placed \nobreak
commands.
Some examples:
\documentclass[a4paper,draft]{book}
\newcommand{\x}[1]{{}$\kern-2\mathsurround${}
\binoppenalty10000 \relpenalty10000 #1{}$\kern-2\mathsurround${}}
\begin{document}
\parbox{5cm}{
A formula \(a+\x{c+d}\)\break showing that spaces are right
A new formula \(a+\x{c+d}\) showing that spaces are right
A brand new formula x \(a+\x{c+d}\) showing that spaces are right
A brand new formula xx \(a+\x{c+d}\) showing that spaces are right
A brand new formula xxx \(a+\x{c+d}\) showing that spaces are right
A brand new formula xxxx \(a+\x{c+d}\) showing that spaces are right
Another brand new formula \(a+\x{c+d}\) showing that spaces are right
Right: $\sin(\x{a+b})$
Wrong: $\sin\x{(a+b)}$
\mathsurround=30pt
A formula xxxxxxx \(a+\x{c+d}\) showing mathsurround
A formula xxxxxxx \(a+c+d\) showing mathsurround
}
\end{document}
Addition about usage
The \x
macro (possibly with a more descriptive name) should be used in specific places. Its contents must
(1) start with an ordinary symbol or be preceded by an ordinary symbol;
(2) end with an ordinary symbol or be followed by one.
It doesn't support the style declarations \displaystyle
, \textstyle
, \scriptstyle
, or \scriptscriptstyle
; it may make sense to carry a \displaystyle
declaration, this might be done with a *-variant.
It doesn't support \left
or \right
: it's not allowed something like
$...\left(\x{a+b}\right)...$
but this is not a problem, as no formula can be split at relation or operation symbols between \left
and \right
and the spaces around these symbols never participates to stretching or shrinking.
\documentclass{article}
\begin{document}
Here is text which starts off the line.
We can write $A^n$ as
\[
\bigcup_{a^{n-1} \in A^{n-1}} \{(a^{n-1}, a) : a \in A \}.
\]
More text, then I have something like the following
$(-3, 3)^{C}$ in $A = (-\infty, -3] \cup [3, \infty)$.
\end{document}
The complicated formula is best set in display mode; no added spaces around the colon, possibly instead after \{
and before \}
.
The second case has a big flaw: the word "in" must go outside math mode because it's text. If you want to avoid a break at \cup
, write
\cup\nobreak
but leave these decisions for when the document is complete.
Best Answer
Using
${....}$
does indeed prevent a formula from being broken across lines but at the cost that you freeze up all spacing within the formula at its natural width. If you write$a=b+c$
then LaTeX generates the following list for you:As you can see this list has a number of "glue" items that can stretch (and possibly shrink) and it also shows two break points with associated penalties 500 and 700.
In contrast if you do
${a=b+c}$
you getwhich looks similar but really isn't. Now the break points are gone (TeX drops them inside hboxes) and the whole formula is in a box of a defined size. So even though the glue items are still around they do not help any more because the size of this hbox is frozen.
In short, if a paragraph line gets slightly stretched a normal formula would stretch too and the same when a line gets squeezed. But in the second case the formula would just stay frozen at its natural width.
I should add that the suggestion of using
${...}$
in formulas was originally given by Don Knuth and can be found in the TeX book. Nevertheless, from a typographic point of view it is a questionable advice (or worse) as it strongly break what TeX normally is attempting to achieve: a uniform and high quality appearance of textual material.An alternative that doesn't have this defect is to set the two parameters that generate the break points to different values:
Used inside a formula this would prevent linebreaks inside the formula, used outside would prevent linebreak in following formulas.
\allowbreak
could be used to provide an explicit line break possibility inside a formula and\nobreak
would prevent one, e.g.,$a=b+\nobreak c$
.Now why made TeX an overfull line rather than moving the whole formula to the next line? That is just the standard way the paragraph breaking algorithm works. If it can't find a solution that satisfies its current settings of
\tolerance
etc. it will produce an overfull line rather than making the paragraph longer. The basic reason if that this is the only way to deal with the scenario where the offending object is bigger than the whole line. If that would be moved, the same problem would happen on the next line ... and you would end up with a paragraph consisting of endless lines being empty.So the answer is: change the paragraph parameters to allow for more flexibility if that is desired. A possible candiate to look at is
\emergencystretch
, but also allowing a slightly higher value for\tolerance
often improves the overall results without producing really bad effects. A word of warning though: the paragraph algorithm is very complex and has a lot of bells and wistles and starting to change its parameter setting should be done with care and only after studying the algorithm in some detail.