# Allowing hyphenation of words containing commands

A command inside a word will make it non-hyphenatable for latex, giving an ugly overfull \hbox, see the first line of sample output. In the following example:

\documentclass{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{sanskrit}
\newfontfamily\sanskritfont{Latin Modern Roman}

\newenvironment{fixhyphenation}{%
\let\oldtextbf\textbf
\renewcommand{\textbf}[1]{\hspace{0pt}\oldtextbf{##1}\hspace{0pt}}
}
{}

\begin{document}

XXX XXX XXX XXX XXX XXX  navanīrabhāra\textbf{bhara}mantharāmbudastanitātiḍambaragabhīrayā

XXX XXX XXX XXX XXX XXX  \begin{fixhyphenation}navanīrabhāra\textbf{bhara}mantharāmbudastanitātiḍambaragabhīrayā\end{fixhyphenation}

XXX XXX XXX XXX XXX XXX XXX XXX XXX  \begin{fixhyphenation}navanīrabhāra\textbf{bhara}mantharāmbudastanitātiḍambaragabhīrayā\end{fixhyphenation}

\end{document}


the fixhyphenation environment allows hyphenation to happen in a long Sanskrit compound, a part of which is set in bold font, by making tex think it's dealing with three words. (second line) My idea was to redefine in the same manner all commands that can happen within a word, which might get quite a long list though. If however the line break happens to be at the beginning or the end of the sequence in bold, we're naturally missing the desired hyphen. (third line)

How would I write a command that

• checks if a line break is occuring in its position

• if that's the case replace itself with a hyphen before the line break

• if there's no line break replace itself with \hspace{0pt}

? Or is there a more elegant way of doing this, saving me from redefining a possibly long list of commands, and enclosing the concerned words in an environment?

EDIT: I am now beginning to incorporate this into commands, after having used it only manually. For the moment, I am not redefining old commands, but rather defining new ones, such as \wfm{} here. But now there is one undesired effect: If I bolden the syllables at the end of a compound, and at that place a line is full, I will get a hyphenation mark instead of a space. If there's no line break, I get the desired space. See the following example:

\documentclass{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\setdefaultlanguage{sanskrit}
\newfontfamily\sanskritfont{Latin Modern Roman}

\newcommand{\comglue}{\discretionary{-}{}{}\nobreak\hspace{0pt}}
\newcommand{\wfm}[1]{\comglue\textbf{\textsanskrit{#1}}\comglue}

\begin{document}

XXXXXX navanīrabhāra\wfm{bhara}mantharāmbudastanitātiḍambara\wfm{gabhīrayā} girā

navanīrabhāra\wfm{bhara}mantharāmbudastanitātiḍambara\wfm{gabhīrayā} girā

\end{document}


I tried to put brackets after the \comglues in the definition of \wfm, and also I put an {} at the end of the compound, both without any effect. Anyway, the space does appear at other places than the end of the line, so that's not the problem.

EDIT

Another problem that can happen is a hyphen before a word:

caused by the following line in the above example.

XXXXX navanīrabhāra\wfm{bhara}mantharāmbudastanitātiḍambaragabhīrayā \wfm{girā}

It is interesting to note that the hyphen is not attached to the preceding word, but to the preceding \hspace{0pt} which is part of the \comglue command. Maybe one could extend the \comglue by two tests for preceding or following whitespace at its beginning and end respectively, which if true suppress the output of \discretionary{-}{}{}\nobreak\hspace{0pt}. Let's see.

EDIT: I am a small step further. I can now test for a following space:

\documentclass{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\usepackage{amsgen}%for the command \new@ifnextchar, with which one can test for a space.
\setdefaultlanguage{sanskrit}
\newfontfamily\sanskritfont{Latin Modern Roman}

\makeatletter
\newcommand{\comglue}{\new@ifnextchar { }{\discretionary{-}{}{}\nobreak\hspace{0pt}}}
\makeatother

\newcommand{\wfm}[1]{\comglue{}\textbf{\textsanskrit{#1}}\comglue{}}

\begin{document}

XXXXXX navanīrabhāra\wfm{bhara}mantharāmbudastanitātiḍambara\wfm{gabhīrayā} girā

navanīrabhāra\wfm{bhara}mantharāmbudastanitātiḍambara\wfm{gabhīrayā} girā

XXXXXX navanīrabhāra\wfm{bhara}mantharāmbudastanitātiḍambaragabhīrayā \wfm{girā}

XXXXX navanīrabhāra\wfm{bhara}mantharāmbudastanitātiḍambaragabhīrayā \wfm{girā}

\end{document}


So now the third line gives me the desired output:

However this still doesn't test for 1) a preceding space, and 2) newlines, and other possible whitespace such as tabs. How would I do these?

• How often do you need commands within words? You just could mark the positions, where Latex is allowed to hyphenate the words by \-, in case the number of occasions, where you need this is limited – JMP Apr 11 '16 at 7:40
• @JMP Unfortunately it occurs quite often, and as I'm dealing with a conversion from TEI-XML files via XSLT to LaTeX, inserting discretionaries manually is not really an option. One could have the XSLT insert them, but that would mean to having to reimplement the hyphenation rules there, and having the discretionaries in every possible place in these words, rendering them rather ugly in your LaTeX file. Quite possibly it will have to be done like that, but I'd prefer not to. – muk.li Apr 11 '16 at 7:59
• Don't use \hspace{0pt} but \discretionary{-}{}{} – Ulrike Fischer Apr 11 '16 at 9:16
• @UlrikeFischer: This works only for the third line, where we have the line break occuring at the end of the command, not in the second line, where no hyphenation occurs at all. – muk.li Apr 11 '16 at 10:17
• Oh yes, didn't look at this line. Try \discretionary{-}{}{}\nobreak\hspace{0pt} – Ulrike Fischer Apr 11 '16 at 10:43