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.
It turned out this wasn't at all related to any code I had in my actual document, but to a header definition I use in a package I've authored myself. If some fields where empty, some parts of the header ended up with two \\
following directly on each other. The first page had \maketitle
on it, and thus didn't print out the header area, so the error didn't occur until LaTeX tried to create the second page - completely unrelated to what was in the document at the pagebreak.
Lessons learned:
When things go wrong at page breaks, don't only check the document itself - also check the header and footer definitions.
When authoring your own packages, make sure they don't make any unrealistic assumptions, such as "the document author will always want a subtitle"...
Best Answer
Short answer
\penalty-10000
and\linebreak
are very much different from each other. And\allowbreak
is very different from both.Long answer
The macros
\nobreak
,\allowbreak
and\break
mean, respectively,They are in LaTeX because they are in Plain TeX and the original LaTeX loaded
lplain.tex
which was a slightly edited copy ofplain.tex
. They are documented neither in the LaTeX manual nor in the LaTeX Companion. Actually,\nobreak
appears in the index of the latter, with a reference to p. 234, but it's probably a typo for\nopagebreak
).Those macros are included for back compatibility: many old LaTeX documents used Plain TeX programming. However they shouldn't be used in newer documents because they are against LaTeX's guidelines of distinguishing commands that go in horizontal and vertical mode.
The problem with
\break
is thatand
mean two very different things: the first one breaks the line (like
\linebreak
), the second introduces a page break.With LaTeX's command
\linebreak
you'd get a line break in the first case and an error in the second one.In macros, where the programmer can control precisely the timing, those shortcuts can be handy. However
\allowbreak
appears only once in the LaTeX kernel, in the definition of\pmod
which is copied fromplain.tex
;\break
appears twice, in the definition of\eject
(again copied fromplain.tex
) and in the definition of\@gnewline
(part of the code for\\
, but in the macro an\ifvmode
test is made, so this\break
can appear only in horizontal or math mode);\nobreak
appears more frequently, but always in controlled situations, where the mode can be predicted; a typical appearance is\if@nobreak\ifvmode\nobreak\fi\fi
.The conclusion is easy: don't use those macros in documents. You're allowed to use them in lower level programming, but you should know what you're doing. Don't blame LaTeX if one of your macros using
\break
issues a page break rather than a line break.