# How to fine-tune hyphenation for mix of math and text

In maths texts it is common to introduce special terminology which follows the scheme

$math$-word


An example: $\alpha$-conversion, $k$-clique.

Now the first problem one encounters when using this pattern is that the word is not recognised by the hyphenation algorithm anymore. A simple fix is to insert a 0-length space before the word:

$math$-\hskip0pt word


With this trick the hyphenation patterns for word are used correctly again.

The problem is that I would like to avoid the following line breaks:

$math$-
lengthyword

$math$
-lengthyword


and only allow hyphenation in lengthyword

$math$-len-
gthyword


I tried with $math$\nobreak-\nobreak\hskip0pt word to no avail. What is the correct way to instruct the hyphenation?

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}


• a hybrid of this and the original use of \nobreak allows the word following the hyphen to be hyphenated automatically: $math$\mbox{-}\nobreak\hspace{0pt}lengthyword. then, don't need to be bothered with \-s. – barbara beeton Jan 12 '17 at 19:14

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.

• Very nice!!! Touche! – Steven B. Segletes Jan 12 '17 at 20:23
• @StevenB.Segletes -- i guess i knew it was there, but the details escaped me. the topic comes up so often of how to permit automatic hyphenation of a word after a hyphen, usually not preceded by math, that i just go for the "raw" code. there's no reason, of course, that this couldn't be used in that situation as well, although i'm somewhat embarrassed at the one-letter control-sequence suggestions. (i didn't write that.) – barbara beeton Jan 12 '17 at 21:20
• This is very useful thanks! I would still keep the current accepted answer because it does not depend on amsmath even though...who does not use it? :) – Bordaigorl Jan 12 '17 at 22:20

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}


• Is it possible to extend this to "normal" characters, i.e. characters that do not fulfill the requirement "Only characters above U+007F may be defined"? – user121799 Apr 5 '18 at 19:23
• @marmot Nothing prevents you from making p active, but then don’t be upset if you can’t \hspace any longer. – egreg Apr 5 '18 at 19:35
• Oh, I see. Thanks. This would be too great a price to pay. – user121799 Apr 5 '18 at 19:37