How to fine-tune hyphenation for mix of math and text
there's actually an existing command in amsmath
for this: \nobreakdash
. it's
documented on p.12 of the manual (texdoc amsmath
).
for convenience, here's a scan of the relevant section.
Placing the hyphen in an \mbox
will preclude its use as a breakpoint.
EDIT: Thanks to barbara for providing the magic incantation to avoid the need to manually hyphenate the word following the $math$\mbox{-}
. In particular, a strategically placed \nobreak
, allong with the \hspace{0pt}
provides the generalization: $math$\mbox{-}\nobreak\hspace{0pt}lengthyword
.
\documentclass{article}
\textwidth1pt
\parindent 0pt
\parskip 1ex
\begin{document}
\mbox{Not this:}
$math$-\hspace{0pt}lengthyword
\mbox{But this:}
$math$\mbox{-}\nobreak\hspace{0pt}lengthyword
\end{document}
With UTF8 input there is ‑
(U+2011 NON-BREAKING HYPHEN). I also show that U+2013 EN DASH doesn't allow a break
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}
\parbox{0pt}{\hspace{0pt}% force hyphenation everywhere possible
$p$\nobreakdash-dimensional
$p$‑dimensional % non breaking hyphen
1\nobreakdash--9
1–9 % en-dash
}
\end{document}
It is possible to change the definition for U+2011 to allow hyphenation in the following word:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{newunicodechar}
\newunicodechar{‑}{\mbox{-}\nobreak\hspace{0pt}}
\begin{document}
\parbox{0pt}{\hspace{0pt}% force hyphenation everywhere possible
$p$\nobreakdash-dimensional
$p$‑dimensional % non breaking hyphen
1\nobreakdash--9
1–9 % en-dash
}
\end{document}