[Tex/LaTex] Prevent line breaking in inline math, but allow flexible spacing

line-breakingmath-modespacing

How can I easily tell Latex to prevent line-breaks inside a part of an inline formula, without affecting inter-word spacing?


Writing something like $ab(c+d)$ in Latex is bad: Latex might put a line break after +. But I do not want to disable line breaks after a + globally; it is perfectly fine to have a line break in $abc+def$. Therefore I have followed the approach mentioned in this thread: simply add extra braces.

In principle, this is great. I can write things like ${ab(c+d)}$ and $abc+{de(f+g)}$ to control which parts of an inline equation are "atomic" in the sense that Latex must not add line breaks inside them.

Unfortunately, there seems to be an unwanted side effect: Latex typesets ${a+b}$ and $a+b$ in the middle of a fully-justified line of text in different ways. If I write $a+b$, Latex seems to add "flexible" spaces around +: just like regular inter-word spaces, Latex can slightly adjust the spacing so that a fully-justified paragraph looks good. However, if I write ${a+b}$, Latex seems to use fixed-width spaces around +.

This answer mentions that one possible approach is to write something like $a+\nobreak b$, but this gets tedious and error-prone, even with some macros. I would like to "mark" a certain part of an inline equation as a "nobreak-zone". How can I do that, with the least amount of extra code?

Ideally, I would like to write something like $abc + \x{de(f+g)}$, where \x is a macro that prevents line breaks but allows flexible spacing.


An example:

\documentclass[a4paper]{article}

\begin{document}
    Foobar $aaaa+bbbb+cccc+dddd+eeee+ffff+gggg$ foobar foobar foobar foobar
    foobar $aaaa+bbbb+cccc+dddd+eeee+ffff+gggg$ foobar foobar foobar.

    Foobar ${aaaa+bbbb+cccc+dddd+eeee+ffff+gggg}$ foobar foobar foobar foobar
    foobar ${aaaa+bbbb+cccc+dddd+eeee+ffff+gggg}$ foobar foobar foobar.

\end{document}

Produces:

example

Best Answer

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.