As a followup to this question, I'd like to ask: how to properly add hyphenation exceptions that contain underscores?
NB: Previous questions (and I found a few) settled with somehow fixing the hyphenation, even if it meant mutilating each and every instance of the word(s) to be hyphenated. But I need a solution that doesn't require me to (from the view of the LaTeX engine) pre-process the text before handing it to LaTeX. Also, modifying each and every instance of these seems to violate one of those sermons I've heard when I initially started with LaTeX during my university time: that LaTeX frees you from having to micro-manage details like that and you just type out the text and the document class in tandem with LaTeX will do the rest.
Consider the answer by Ulrike Fischer, slightly adjusted:
\documentclass{article}
\usepackage[ngerman,english]{babel}
\usepackage{xparse}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\DeclareRobustCommand\funcfamily
{\fontencoding{OT1}\fontfamily\ttdefault\selectfont}
\hyphenation{
An-Obscenely-Long-Function-Name
Another_Name_With_Under-scores
}
\NewDocumentCommand{\funcref}{mo}{{%
\funcfamily\hyphenchar\font=130
\hyperref[#1]{#1(\IfValueTF{#2}{#2}{})}}}
\usepackage{hyperref}
\begin{document}
AnObscenelyLongFunctionName AnObscenelyLongFunctionName AnObscenelyLongFunctionName
AnObscenelyLongFunctionName AnObscenelyLongFunctionName
foo bar baz \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName}
\end{document}
pdfLaTeX chokes on line 11 with:
Improper \hyphenation will be flushed. Another_
Improper \hyphenation will be flushed. Another_Name_
Improper \hyphenation will be flushed. Another_Name_With_
Initially I thought this answer to the question titled "Hyphenation for words with underscore" would help me out, but it doesn't. In fact I tried also with \usepackage{underscore}
.
It also doesn't make much of a difference whether I use
\hyphenation{
An-Obscenely-Long-Function-Name
Another_Name_With_Under-scores
}
or:
\hyphenation{
An-Obscenely-Long-Function-Name
Another\_Name\_With\_Under-scores
}
(Well, not entirely true, I get more errors on line 11 if I use the latter, but effectively it's the same to me as both fail to work. Also, if I modify the catcode
both variants behave identically.)
I read the appendix H "Hyphenation" in the TeX-book, but I reckon it doesn't discuss my case since natural languages don't use underscores.
I also tried adjusting the catcode for the underscore to be 11 before defining the hyphenation exceptions (i.e. surrounding the \hyphenation{}
, inspired by this answer), but that failed as well.
So how can I define hyphenation exceptions for words/names with underscores?
I would prefer to avoid having to "treat" each and every instance of these words/names (as in this Q&A), which is why I am looking to add them to the hyphenation exceptions.
underscore
doesn't make_
of catcode 11 or 12, but active.url
before and had some issue. But I'll revisit this now and possibly ask in a separate question. Still I think it's worthwhile know if it's possible to use underscores in the hyphenation exceptions and how.