# Hyphenating hyphens: how to change the default \discretionary

My language, Portuguese, have undergone another orthography reform (those committees simply can't avoid themselves from doing this mess from time to time.) Now we have to adapt, which brings us to my problem.

By the new rule, when a line break occurs at one word's literal hyphen, it should be doubled, appearing at the end of the first line and at the beginning of the second one.

A phrase like “Terão que adaptar-se ou perecer!”, broken at the hyphen, should look like this:

-se ou perecer!

In TeX terms, each literal hyphen should be replaced by

\discretionary{-}{-}{-}


or followed by

\discretionary{}{-}{}


But TeX's (apparently) hard coded discretionary for literal hyphen is \discretionary{}{}{} (called “empty discretionary”), inserted after every “-” [The TeXBook, p. 95].

The obvious solution

\chardef\hyphen=\-
\catcode\-=13
\def-{\penalty\exhyphenpenalty
\discretionary{\hyphen}{\hyphen}{\hyphen}}


is hardly usable because you cannot redefine a character like “-”, heavy used by TeX assignments, without getting too much trouble.

Is there a right way to get this effect? Or only by diving into TeX's source code or waiting for LuaTeX?

• The standard babel way to proceed would be to activate " and define a shortcut "-. The activation of - can be done, either, but one has to remember to say \string- when the hyphen is needed in the context of a numeric assignment. – egreg Jul 15 '11 at 7:54
• @Caio: Interesting, I was unaware of that specific rule, thanks for bringing this up. Sadly, we have to adapt (I think the transition period ends this year), but I'll resist until the very last minute. =) – Paulo Cereda Jul 16 '11 at 20:32

One approach without requiring LuaTeX would be to take a similar tack to the way hyphenation is/used to be handled in German. Something like

\documentclass{article}
\catcode\"=\active
\def"#1{\ifx#1-\discretionary{-}{-}{-}\fi}
\begin{document}
Some filler text.
Some filler text.
Some filler text.
Some filler text.
Some"-hyphenated word.
\end{document}


Of course, this requires the use of one additional character, which may not be desirable. On the other hand, I don't think that there is a hook in the TeX engine to alter things, so the only 'no input change' way to alter things is I guess to use LuaTeX.

• Hyphenated words are badly common in Portuguese, using this method would make the manuscript too messy. Thanks for the advice, I'll research LuaTeX for an answer and give a feedback to this forum. – Caio Lopes Jul 16 '11 at 3:32

The alternative I commonly use is the redefinition of the underscore to provide this doubled-when-broken hyphen:

\let\oldunderscode=_
\catcode\_\active
\def_{\ifmmode\oldunderscore\else\discretionary{-}{-}{-}\fi}


This keeps the meaning of _ in mathematical mode, which is essential!! Also, take care not to use _ in your labels, as they will not work. I usually write \label{my-section} anyway, so this is not a problem for me.

Now, you just need to write Terão que adaptar_se ou perecer!, which I think is still very readable and has the advantage of using a single character as well.

I usually place code similar to that in a separate .sty file which I import whenever I need it. This package helps me write small portions of Portuguese text inside documents with English as main language, which changes hyphenation rules (provided babel is loaded with Portuguese language) and also emits a \frenchspacing that disables the "double space" at the end of sentences that is traditional in English typography but not in Portuguese.

A more elegant approach would be to use babel command shorthand \babelhyphen{repeat} (section 1.5) to define a new character (or in this case two) for a repeatable hyphen. It's designed specifically for that (see this question):

hard hyphen inside compound words are repeated at the beginning of the next line.


Something like this:

    \documentclass{article}
\textwidth=5cm % just making easier to spot with less text
\usepackage[brazil]{babel}
\defineshorthand{"-}{\babelhyphen{repeat}} % generates a hyphen that will repeat on a new line
\begin{document}
Text Text Text Text sabendo-se % without repeated hyphen

Text sabendo-se                % won't repeat anyway

Text Text Text Text sabendo"-se % with repeated hyphen

Text sabendo"-se                % won't repeat anyway
\end{document}


Yes, it sill makes the the code look weird and demands author to type an extra character every time, but it works and it's simple.