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.
Here's a possibility:
\documentclass{article}
\usepackage{amsmath}
\makeatletter
\everymath{\if@display\else\thickmuskip=2mu plus 2mu\fi}
\makeatother
\begin{document}
\begin{center}% just to show the effect
$a=b$
\end{center}
\[
a=b
\]
\end{document}
Alternatively (and preferably), but this requires using \(...\)
for inline math:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xpatch}
\xpatchcmd{\(}{$}{$\thickmuskip=2mu plus 2mu }{}{}
\begin{document}
\begin{center}% just to show the effect
\(a=b\)
\end{center}
\[
a=b
\]
\end{document}
Note that $$...$$
will produce reduced spaces, if the first method is used. But this construction should never be used in LaTeX anyway.
Best Answer
It's theoretically possible to make LaTeX recognize clusters of letters and typeset them as if they were input as argument to
\mathit
, but it would be deadly slow: each alphabetic character should be turned into a “math active” one, which checks whether the following item is an alphabetic character; if it is it should typeset itself, starting\mathit
if it's the first one, and then pass the same control to the next character; if not followed by an alphabetic character it should end\mathit
.It's instead better to do something like
where
\mli
stands for MultiLetter Identifier; use any control sequence name. Then you'd inputthat has a good deal of advantages, but mainly keeps information about the input.
Note: I have a nice proof of the above statement, but unfortunately there are length limitations for posts on this site.
Heiko's solution in
expl3
.