3

By default, inline maths breaks long expressions in such a way that the line break happens after binary operators, e.g.

a + b +
c + d

Is there any easy way to convince TeX to break before the binary operator, to get

a + b
+ c + d

I would prefer if I do not have to insert special instructions at every binary operator by hand.

  • Short answer: there's no easy way. The behavior is built-in; maybe LuaTeX can do it. On the other hand, I find that breaking before the operator inserts a good deal of ambiguity for the reader. – egreg Oct 20 '16 at 15:28
2

EDIT to make + active only in math mode, using egreg's answer from Make Characters Active via Macro in Math Mode

When + is active, I use a \discretionary to allow the breakpoint where desired.

The MWE shows

  1. it making a break at the right point when it hits the margin. Further, the terms are properly nestled to the margin boundary,

  2. it gives proper math spacing when not making a break

  3. the math-active + does not screw up the use of + for dimensioning.

Here is the MWE.

\documentclass{article}

\usepackage{amsmath}
\usepackage{xcolor,etoolbox}

\makeatletter
\newcommand{\DeclareMathActive}[2]{%
  % #1 is the character, #2 is the definition
  \expandafter\edef\csname keep@#1@code\endcsname{\mathchar\the\mathcode`#1 }
  \begingroup\lccode`~=`#1\relax
  \lowercase{\endgroup\def~}{#2}%
  \AtBeginDocument{\mathcode`#1="8000 }%
}

\DeclareMathActive{+}{\mathbreak\std{+}}

\newcommand{\std}[1]{\csname keep@#1@code\endcsname}
\patchcmd{\newmcodes@}{\mathcode`\-\relax}{\std@minuscode\relax}{}{\ddt}
\AtBeginDocument{\edef\std@minuscode{\the\mathcode`-}}
\makeatother

\textwidth 1.75cm
\parindent 0pt
\parskip 1ex
\def\mathbreak{\discretionary{}{}{}}
\begin{document}
\hrulefill

\rule{1cm}{1pt}$ a + b + c + d$ \rlap{(broken spacing aligns to margins)}

$+ c$ \rlap{(for left-margin comparison)}

\mbox{\rule{1cm}{1pt}$ a + b + c + d$ \rlap{(unbroken spacing is proper)}}

Did + revert to original definition?\parskip=+30pt\relax

Yes it did
\end{document}

enter image description here

Now that I have tested the approach to my satisfaction, here it is in a more general setting:

\documentclass{article}
\usepackage{amsmath}
\usepackage{etoolbox}
\makeatletter
\newcommand{\DeclareMathActive}[2]{%
  % #1 is the character, #2 is the definition
  \expandafter\edef\csname keep@#1@code\endcsname{\mathchar\the\mathcode`#1 }
  \begingroup\lccode`~=`#1\relax
  \lowercase{\endgroup\def~}{#2}%
  \AtBeginDocument{\mathcode`#1="8000 }%
}
\DeclareMathActive{+}{\mathbreak\std{+}}
\newcommand{\std}[1]{\csname keep@#1@code\endcsname}
\patchcmd{\newmcodes@}{\mathcode`\-\relax}{\std@minuscode\relax}{}{\ddt}
\AtBeginDocument{\edef\std@minuscode{\the\mathcode`-}}
\makeatother
\def\mathbreak{\discretionary{}{}{}}
\usepackage[nopar]{lipsum}
\begin{document}
\lipsum[4]. And now we will demonstrate the breaking ability
$a + b + c + d$ of the method. And here, $a + b + c + d$ when not broken.
\end{document}

enter image description here

  • 1
    You can make + math active, instead. – egreg Oct 20 '16 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.