# Defining hyphen rules for word parts

One can define optimal hyphenation of a single word in the preamble of a LaTeX document that will be used if line length is awkward.

\hyphenation{biblio-philia} % Just an example


What if I wanted to be more general and define hyphenation for word parts:

% not real but imagined command:
\hyphenation{.+o-philia\$}


Adding a discretionary hyphen to any word that ends with ophilia after the o?

• no, you could define a pattern but (unless you are using luatex) that requires building a new format, you can't use \patterns in a normal document run (and tex patterns are not regex like that but you could match those characters at end of any word) – David Carlisle Oct 3 '18 at 14:32
• Good to know! I'm actually using xelatex and not luatex. – snoram Oct 3 '18 at 14:34
• The standard setting for American English hyphenates bib-lio-philia – egreg Oct 3 '18 at 17:30
• @egreg right, I'm not working in English, it's just an abstract example. – snoram Oct 3 '18 at 17:33

You can see the effect of patterns using Luatex

Underfull \hbox (badness 10000) in paragraph at lines 8--8
[] \TU/lmr/m/n/10 bibliophilia bibliophilias thingophilia

Underfull \hbox (badness 10000) in paragraph at lines 13--13
[] \TU/lmr/m/n/10 biblio-philia bibliophilias thingophilia

Underfull \hbox (badness 10000) in paragraph at lines 18--18
[] \TU/lmr/m/n/10 biblio-philia bibliophilias thingo-philia

Underfull \hbox (badness 10000) in paragraph at lines 25--25
[] \TU/lmr/m/n/10 biblio-philia biblio-philias thingo-philia


which is the terminal output from

% don't use the default US English patterns as biblio-philia already allowed there
% which spoils the example

\newlanguage\zzz
\language\zzz

% nothing hyphenates
\showhyphens{bibliophilia bibliophilias thingophilia}

\hyphenation{biblio-philia}

% just bibliophilia
\showhyphens{bibliophilia bibliophilias thingophilia}

\patterns{o9philia.}% tied to end of word

% bibliophilia and thingophilia hyphenate
\showhyphens{bibliophilia bibliophilias thingophilia}

\newlanguage\zzzb
\language\zzzb
\patterns{o9philia}% not tied to end of word

% all hyphenate
\showhyphens{bibliophilia bibliophilias thingophilia}

\stop


Essentialy the same code would work with pdflatex or xelatex but you would have to modify the language files and build new formats with initex, you can not use \patterns in a normal tex run with classic tex.

If you're free to switch from XeLaTeX to LuaLaTeX, it's straightforward to set up a Lua function that adds a discretionary hyphenation point in words that contain "ophilia". In fact, as the following example shows, one can easily generalize the function so that it treats all words that contain either the letter a or o followed by either philia or phobia.


\documentclass{article}
\usepackage{luacode}
\begin{luacode}

function ophilia ( s )
s = s:gsub ( "([ao])(philia)" , "%1\\-%2" )
s = s:gsub ( "([ao])(phobia)" , "%1\\-%2" )
return s
end

\end{luacode}
"process_input_buffer", ophilia, "ophilia" )}}

%% just for this example:
\setlength{\textwidth}{1mm}
\setlength{\parindent}{0pt}

\begin{document}
bibliophilia cryptophilia necrophilia agoraphilia
bibliophobia cryptophobia necrophobia agoraphobia
\end{document}

• but don't do \newcommand\agoraphilia {zzz} in that case:-) – David Carlisle Oct 3 '18 at 15:43
• @DavidCarlisle - True, I hadn't considered this possibility for practical insanity. :-) I'll post an addendum to state explicitly that a person who employs the method shown above shouldn't create macros whose names contain [ao]philia or [ao]philia... – Mico Oct 3 '18 at 16:09
• for the ending given command names are unlikely but a reader might be encouraged by success with that ending to make all words ending in tion hyphenate as -tion but then \section might not work too well..... – David Carlisle Oct 3 '18 at 16:12
• @DavidCarlisle - Do you approve of the extra paragraph I added a minute ago? :-) – Mico Oct 3 '18 at 16:17
• i even voted:-) – David Carlisle Oct 3 '18 at 16:20