# hyphenation of text within math

I would like text like

$$(\abcdefghij,\abcdefghij)$$-pair


(with \abcdefghij defined as \textit{abc\-defg\-hij}) to be hyphenated as

(abc·defg·hij,·abc·defg·hij)-·pair


("·" indicates all possible linebreak points; there would of course need to be hyphens if the adjacent characters on both sides are letters). Let's assume that I need a macro like \abcdefghij in many places in the document. How can I achieve the intended hyphenation?

-
Don't. If it's hyphenatable, it's not math. –  egreg Feb 2 '13 at 11:04
@egreg Good point. I think the cases where I'd like to do this are defensible because they are a certain type of textual material. But I wouldn't hyphenate numbers, because that just might lead to trouble ;-) –  Lover of Structure Feb 3 '13 at 11:54

Hyphenation works in math as long as you are not inside a {} group which is essentially an \hbox in math mode. \begingroup groups are OK though.

As you did not provide a MWE (grumble) I stole one from Hendrik's answer.

\documentclass{article}
\newcommand*\abcdefghij{%
\begingroup\mathit{\xdef\tmp{\fam\the\fam\relax}}\tmp
abc\-defg\-hij\endgroup}

\begin{document}
\noindent
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and to end with, one last
$$(\abcdefghij,\abcdefghij)$$-pair.
\end{document}

-
+1, this is for sure better than what I propose. (By the way, I noticed that often you don't crop your images. Is this on purpose?) –  Hendrik Vogt Feb 3 '13 at 7:22
Thanks! If I replace \mathit by \textnormal or \texttt{\textit{[...]}} or \textrm{\sbfont\selectfont[...]}, I can't get it to work. I looked for other TeX primitives like \fam but couldn't find any. Any thoughts? –  Lover of Structure Feb 3 '13 at 9:14
the \text... commands are \hbox so don't allow line breaking, nothing special about \mathit there though \mathrm or \mathsf etc will all work. –  David Carlisle Feb 3 '13 at 10:34
@HendrikVogt well I crop by eye normally in an editor, often I pick up the page margins just to show layout, here I seem to have been a bit generous with white space for no particular reason:-) –  David Carlisle Feb 3 '13 at 10:36

One way to achieve the intended hyphenation is to put the "syllables" into separate \textit commands, separated by discretionary breaks \-. (This interferes with the kerning in abcdefghij, but works reasonably well.) To allow a line break after the comma, you can use the code from this answer by egreg.

Note that I put an italic correction \/after the j so that it's not that close to the closing parenthesis anymore.

\documentclass{article}
\newcommand*\abcdefghij{\textit{abc}\-\textit{defg}\-\textit{hij\/}}
\AtBeginDocument{%
\mathchardef\mathcomma\mathcode\,
\mathcode\,="8000
}
{\catcode,=\active
\gdef,{\mathcomma\discretionary{}{}{}}
}
\begin{document}
\noindent
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and a
$$(\abcdefghij,\abcdefghij)$$-pair and to end with, one last
$$(\abcdefghij,\abcdefghij)$$-pair.
\end{document}
`
-