Put the math expression into braces {...}
, then it will be a math atom and not broken at the end of the line.
${v_{initial} = \SI{1000}{m/s}}$
to prevent an overfull box use \sloppy
or better the sloppypar
environment:
\documentclass{article}
\usepackage{siunitx}
\begin{document}
\begin{sloppypar}
A text with an inline equation which is broken in to two parts
but is not wanted right here ${v_{initial} = \SI{1000}{m/s}}$.
\end{sloppypar}
\end{document}
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.
Best Answer
LaTeX can break inline formulas after a relational or a binary symbol. If you don't want this to happen you can explicitly prevent it by setting:
LaTeX will then always try to keep the
$...$
within one line, but as a result you may end up with overfull hboxes if the paragraph doesn't have enough flexibility to allow for some acceptable line breaking. In that case xou then have to place an explicit linebreak (or an optional one) manually into the formula using\linebreak
(with or without an optional argument).The alternative (as a one-time change) is that you place an explicit linebreak just before the formula (via
\linebreak
) which will then force a linebreak at that point with a justified line in fron of the formula. Again this only works with enough flexibility in the paragraph.The other possibility for a one-time change is to put the above parameter settings inside the
$...$
in which case they only apply to that equation.What you should not do (normally) is to surround the whole equation by, say an
\mbox
. In practive this will also prevent LaTeX from breaking it over a line, but it has the additional effect of making the spacing within the formula rigid, i.e., the formula will not be queezed or spread in line with the rest of the paragraph material.The only other alternative is to go for displayed formula as mentioned in the comment.