Hyphenation problem with — versus \textemdash

While investigating problems with hyphenation and em-dashes, I came across this:

a piece---perhaps an installation---, involving structural feedback


The document class is article. The problem was that installation was not hyphenated, and an overfull line was produced, with installation--- moving over the right margin, and the next line beginning with the comma.

Now I happened to try this, and it fixed the problem:

a piece---perhaps an installation\textemdash, involving structural feedback


properly breaking the line such as

...instal-
lation---, involving...


This seems counter-intuitive to me, and I really prefer to see the triple hyphens in my source code, much more readable. Is there a way to fix this problem?

It looks clearly like a latex error to me, because standard usage of em-dash states: "According to most American sources [...] and some British sources [...], an em dash should always be set closed, meaning it should not be surrounded by spaces."

-
Try your original construction with \sloppy? –  Ethan Bolker May 20 '12 at 20:29

You have, at least, three possible options:

1. Use \textemdash instead of ---. You already mentioned this in your question and it seems counter-intuitive for you.
2. Manually declare the valid hyphenation points for the word preceding the ---.
3. Load the babel package and use \allowhyphens---. I think this approach will give you something closer to what you want since you could declare some character as active (" for example) and define a command "--- to be \allowhyphens---; you then will write "--- to get an em-dash allowing hyphenation for the preceding word (this approach was used by Javier Bezos in his implementation of a similar shorthand in the spanish module for babel).

Here's some code showing the problem and the three alternatives I mentioned:

\documentclass{article}
\usepackage[english]{babel}

\begin{document}

some filler text to illustrate the problem here's a piece---perhaps an installation---, involving structural feedback

some filler text to illustrate the problem here's a piece---perhaps an installation\textemdash, involving structural feedback

some filler text to illustrate the problem here's a piece---perhaps an installation\allowhyphens---, involving structural feedback

some filler text to illustrate the problem here's a piece---perhaps an in\-stal\-la\-tion---, involving structural feedback

\end{document}


Here's some code that could be used to implement my suggestion in the third numeral above :

\documentclass{article}
\usepackage[english]{babel}

\catcode"=13
\def"-{\allowhyphens-}

\begin{document}

some filler text to illustrate the problem here's a piece---perhaps an installation---, involving structural feedback

some filler text to illustrate the problem here's a piece---perhaps an installation"---, involving structural feedback

\end{document}


Yet another alternative, taken from this answer by Herbert to babel: Adding ngermans language shorthands to english as the main document language (thanks to egreg for pointing out this question) is to use the spanish modulo as a secondary language and use its already existing shorthand:

\documentclass{article}
\usepackage[spanish,english]{babel}
\useshorthands{"}

\begin{document}

some filler text to illustrate the problem here's a piece---perhaps an installation---, involving structural feedback

some filler text to illustrate the problem here's a piece---perhaps an installation"---, involving structural feedback

\end{document}


-
But that's even more horrible than falling back to \textemdash, and I will need to hyphenate each word preceding an em-dash, just in case it ends up at a line end. To me this is clearly a latex error (see my addendum about common em-dash use). –  Emit Taste May 20 '12 at 18:30
@EmitTaste you're right. I've updated my answer now showing another possible solution. –  Gonzalo Medina May 20 '12 at 21:42

The reason why installation--- doesn't allow TeX to hyphenate the word is explained in the TeXbook (p. 95):

The most common case of a discretionary break is a simple discretionary hyphen

\discretionary{-}{}{}


for which TeX accepts the abbreviation ‘\-’. The next most common case is

\discretionary{}{}{}


(an “empty discretionary”), which TeX automatically inserts after ‘-’ and after every ligature that ends with ‘-’. In the case of plain TeX, empty discretionaries are therefore inserted after hyphens and dashes. (Each font has an associated \hyphenchar, which we can assume for simplicity is equal to ‘-’.)

Conversely, LaTeX defines

\DeclareTextSymbol{\textemdash}{OT1}{124}


or, with the T1 encoding,

\DeclareTextSymbol{\textemdash}{T1}{22}


so that the command \textemdash directly selects a character, without passing through the ligature mechanism, so that no empty discretionary is inserted.

In my opinion you should use ---, manually correcting possible bad breaks and deciding, in tough situations, whether hyphenating the word next to the dash is the lesser evil.

-

Another solution, besides the ones already mentioned in the other answers, consists of inserting \nobreak\hskip0pt between installation and ---. That way, TeX can hyphenate the word "installation", but there won't be line break right between tion and the em-dash.

-

Instead of the traditional --- ligature, I use the Unicode character —, which is equivalent to \textemdash. In my opinion, there should be no line break at the em dash, but hyphenation within the adjacent words seems acceptable to me. To achieve this, I patch \textemdash as follows:

\documentclass{article}

\usepackage{etoolbox}

\usepackage[utf8]{inputenc}

\makeatletter

\apptocmd{\textemdash}{%
\nobreak\hskip\z@skip%
}{%
}{%
}

\robustify\textemdash

\makeatother

\begin{document}

\showhyphens{hyphenation—hyphenation}

\end{document}

-

In case of strange behavior or foreign languages, you can manually instruct LaTeX to hyphenate your word like this

in\-stal\-la\-tion


\- shows a possible hyphen.

-
It's not a problem of the dictionary. If I don't have the em-dash, it properly breaks e.g. at instal\-lation with english babel and install\-ation` with UKenglish (the one I use now). –  Emit Taste May 20 '12 at 18:59