[Tex/LaTex] automatic \allowbreak around mathbin and mathrel characters

line-breakingmath-mode

I would like linebreaks to be permitted within math formulas on both sides of any characters of the mathbin and mathrel character classes. It seems like linebreaking is by default only permitted after these characters:

\documentclass{article}

\begin{document}

text text text text text text text text text text text text \(x + x + x + x + x + x + x + x + x\)

i text text text text text text text text text text text text \(x + x + x + x + x + x + x + x + x\)

text text text text text text text text text text text text \(x + x + x + x + x \allowbreak+ x + x + x + x\)

text text text text text text text text text text text text \(x = x = x = x = x = x = x = x = x\)

text text text text text text text text text text text text \(x = x = x = x = x \allowbreak= x = x = x = x\)

\end{document}

Oddly, I have also seen situations where an \allowbreak afterwards had an effect (that was after a \cong symbol), but I cannot reconstruct an example right now.

How can I automatically allow linebreaks around mathbin and mathrel symbols? Of course I could always manually insert \allowbreak or include it in my own macros defining such operators as well as \let-redefine the existing such symbols occurring in my document, but that seems like the wrong approach.

I know what I am doing stylistically; what I typeset is not ordinary math with run-of-the-mill equations, plus I really like having an indication of formula continuation at the beginning of the next line. In any case, I think for example that in something like a = b = c⋅d a line break before the second equals sign looks perfectly fine. Basically, I would in some sense like to get rid of LaTeX's preference.

I'm looking for a way to (1) give the left-hand side of such a symbol the same status as its right-hand side or (2) to basically insert automatic \allowbreaks around such symbols. (Note that these two options are not the same.) Note that this ideally shouldn't screw up more complex math symbols built with existing or self-defined mathbin/mathrel symbols.

Best Answer

The flexisym package allows arbitrary commands to be inserted into math character definitions. It is usually used in conjunction with breqn for which it was written but here I use it directly. the definition of binary and relation functions are a bit opaque but just copied directly from the existing definition with the addition of \allowbreak as in your example, but applied to all characters of those classes (as long as they are defined via the standard LaTeX declarations).

enter image description here

\documentclass{article}

\usepackage{flexisym}

\begingroup
\catcode`\_11
\catcode`\:11
\gdef\math_bsym_Bin:Nn#1#2{%
 \allowbreak\math_char:NNn 2#1{#2}\allowbreak}
\gdef\math_bsym_Rel:Nn#1#2{%
 \allowbreak\math_char:NNn 3#1{#2}\allowbreak}
\endgroup

\begin{document}


text text text text text text text text text text text text \(x + x + x + x + x + x + x + x + x\)

i text text text text text text text text text text text text \(x + x + x + x + x + x + x + x + x\)

text text text text text text text text text text text text \(x + x + x + x + x + x + x + x + x\)

text text text text text text text text text text text text \(x = x = x = x = x = x = x = x = x\)

text text text text text text text text text text text text \(x = x = x = x = x = x = x = x = x\)

\end{document}