# Typesetting very long words with hyphens inside

I am typesetting a novel which in a few places contains very long strings of capital letters split into shorter groups separated by hyphens, something like SIERRA-TANGO-CHARLIE-ECHO-YANKEE-OSCAR-MIKE-NOVEMBER-CHARLIE-VICTOR-WHISKEY-ALPHA-PAPA-BRAVO-UNIFORM. Wherever such a 'word' appears, the TeX engine has a hard time trying to typeset it without having it fall off the page margin and spewing pages of 'Overfull \hbox' warnings (as if LaTeX didn't emit too much unnecessary output already).

Begrudgingly, I have added \- sequences to the strings, which helped TeX find better word breaks (I'd prefer to put hyphenation information somewhere where it wouldn't be distracting, but \hyphenation doesn't seem to support specifying word breaks for hyphenated words). I have also adjusted \fontdimen settings, and it helped in some cases, but there still remain instances where such 'words' begin a paragraph and \fontdimen makes no difference, like

SIERRA-TANGO-CHARLIE-ECHO-YANKEE-OSCAR-MIKE-NOVEMBER-CHARLIE-VICTOR-WHISKEY-ALPHA-PAPA-BRAVO-UNIFORM-BLAH-BLAH-BLAH-BLAH-BLAH-BLAH'', Tom said.

Sometimes the overflow can be as small as a single hyphen; I tried to adjust microtype's character protrusion settings to mitigate these cases, but I failed to make it do anything. Setting \emergencystretch produces underflows instead, and still looks quite ugly.

What else can I do to remedy this?

I am currently using XeLaTeX, and would prefer not to change the engine; I really need the bidirectional capabilities of XeTeX, and I find the time it takes to run LuaLaTeX unacceptably slow.

• Maybe add a {} after a hyphen? – Bernard Feb 12 '17 at 21:16
• @Bernard that doesn't enable hyphenation. – David Carlisle Feb 12 '17 at 21:20
• @David Carlisle: I guess it allows typing the rest of the word next line if judiciously placed. I suppose a real hyphenation in the middle of a component of this long word wouldn't be recommended. – Bernard Feb 12 '17 at 21:24
• Which TeX distribution do you use? It's true that the very first time Lua(La)TeX is run on a document, compilation can be slow, but that's because the luaotfload utility has to build a font feature cache. Once the cache is built, compiling a doc with LuaLaTeX shouldn't be significantly slower than it is with XeLaTeX. – Mico Feb 12 '17 at 21:52
• @Bernard ? It doesn't do anything at all in text mode after a hyphen as far as I can think. – David Carlisle Feb 12 '17 at 21:52

As you are using xetex you can set up character classes to insert some glue around the hyphens to help with line breaking and enable hyphenation.

\documentclass{article}

\usepackage{color}
\XeTeXinterchartokenstate=1
\newXeTeXintercharclass\hclass
\newXeTeXintercharclass\lclass
\XeTeXcharclass\-=\hclass

\count0=\a
\loop
\XeTeXcharclass\count0=\lclass
\ifnum\count0<z
\repeat
\count0=\A
\loop
\XeTeXcharclass\count0=\lclass
\ifnum\count0<Z
\repeat

\count0=1

\XeTeXinterchartoks\hclass\lclass{\hskip0pt plus .3pt minus .1pt\relax}
\XeTeXinterchartoks\lclass\hclass{\nobreak\hskip0pt plus .2pt\relax}

\righthyphenmin=2
\hyphenation{BL-AH UNI-F-ORM AL-PHA}
\begin{document}

\noindent X\dotfill X

a    SIERRA-TANGO-CHARLIE-ECHO-YANKEE-OSCAR-MIKE-NOVEMBER-CHARLIE-VICTOR-WHISKEY-ALPHA-PAPA-BRAVO-UNIFORM-BLAH-BLAH-BLAH-BLAH-BLAH-BLAH''

zzzzzzzzz     SIERRA-TANGO-CHARLIE-ECHO-YANKEE-OSCAR-MIKE-NOVEMBER-CHARLIE-VICTOR-WHISKEY-ALPHA-PAPA-BRAVO-UNIFORM-BLAH-BLAH-BLAH-BLAH-BLAH-BLAH''

zzzzzzzzz jjjj axaxax axaxaxa axaxax      SIERRA-TANGO-CHARLIE-ECHO-YANKEE-OSCAR-MIKE-NOVEMBER-CHARLIE-VICTOR-WHISKEY-ALPHA-PAPA-BRAVO-UNIFORM-BLAH-BLAH-BLAH-BLAH-BLAH-BLAH''

\noindent X\dotfill X

\end{document}

• Will this operate on the entire document, or just on the long "words" that consist of uppercase letters and hyphens? – Mico Feb 12 '17 at 21:55
• @Mico well as written it is not scoped but you can just enable \XeTeXinterchartokenstate inside a specific environment if that is required also I picked up - followed by upper or lowercase ascii, you could just look for uppercase if that is needed – David Carlisle Feb 12 '17 at 21:58
• I've just tried this answer and I find the results satisfying; I think I'm going to accept it. Now there are only the warnings in the TOC to deal with... – user114332 Feb 13 '17 at 7:43

I suggest you do three things:

• Load the babel package in the following way:

\usepackage[ngerman,english]{babel}
\useshorthands{"}

• Change all instances of - in the long strings to "=; this allows LaTeX to find hyphenation points (and break lines at those points...) for the words inside the long strings.

• Encase the paragraphs that contain the long strings in sloppypar environments.

As the following screenshot shows, the result is guaranteed to be perfect, but it'll be a whole lot better than without these modifications. Incidentally, if you're free to use LuaLaTeX instead of XeLaTeX, you'll get slightly better results, simply because microtype cooperates more fully with LuaLaTeX than it does with XeLaTeX.

% !TeX program = xelatex
\documentclass{article}
\usepackage{fontspec,microtype}

\usepackage[ngerman,english]{babel}
\useshorthands{"}

\begin{document}
\hrule % just to illustrate width of textblock
\bigskip

\begin{sloppypar}
SIERRA"=TANGO"=CHARLIE"=ECHO"=YANKEE"=OSCAR"=MIKE"=NOVEMBER"=CHARLIE"=VICTOR"=WHISKEY"=ALPHA"=PAPA"=BRAVO"=UNIFORM"=BLAH"=BLAH"=BLAH"=BLAH"=BLAH"=BLAH'', Tom said.
\end{sloppypar}

\bigskip

SIERRA"=TANGO"=CHARLIE"=ECHO"=YANKEE"=OSCAR"=MIKE"=NOVEMBER"=CHARLIE"=VICTOR"=WHISKEY"=ALPHA"=PAPA"=BRAVO"=UNIFORM"=BLAH"=BLAH"=BLAH"=BLAH"=BLAH"=BLAH'', Tom said.

\bigskip

SIERRA-TANGO-CHARLIE-ECHO-YANKEE-OSCAR-MIKE-NOVEMBER-CHARLIE-VICTOR-WHISKEY-ALPHA-PAPA-BRAVO-UNIFORM-BLAH-BLAH-BLAH-BLAH-BLAH-BLAH'', Tom said.
\end{document}

• Ugh, no. I'd rather not write a simple hyphen with some funky digraph. And the results look no better than adjusting \emergencystretch. – user114332 Feb 13 '17 at 7:22
• @user114332 - If you were willing to use LuaLaTeX, it would be a piece of cake to write a Lua function to convert all instances of - that occur between groups of capital letters to "= on the fly, obviating any need for manual intervention. – Mico Feb 13 '17 at 7:40
• @Mico As far as I know the use of babel in combination with XeLaTeX is not recommended. – TeXnician Feb 13 '17 at 21:08
• @TeXnician - How up-to-date is your information? I know that up to about 3 or 4 years ago, babel didn't work all that well (if at all) with XeLaTeX and that polyglossia didn't work at all with LuaLaTeX. However, I though those incompatibilities are a thing of the past. I may be missing something, naturally. – Mico Feb 13 '17 at 21:17
• @Mico From the babel CTAN page: "Users of XeTeX are ad­vised to use poly­glos­sia rather than Ba­bel." On the other hand I did not experience any problems with babel in XeLaTeX nor with polyglossia` in LuaLaTeX. – TeXnician Feb 14 '17 at 5:27