# Disallowing line break before dash (en dash and em dash)

In Polish typography dash (pol. myślnik) should not be put after a line break. Below you can find badly and correctly typed samples using en dash (pol. półpauza) and em dash (pol. pauza).

\documentclass[12pt]{article}
\usepackage[paperwidth=95mm,paperheight=55mm,margin=5mm,right=24mm,marginparsep=5mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}
\usepackage{xcolor}
\pagestyle{empty}
\begin{document}
% line break before a dash is a sin according to Polish typography rules
To jest maciupeńki test półpauzy -- na Zachodzie nazywanej \emph{en dash}.  {\color{orange}\hfill~--}
\par \emph{Em dash} za to nazywamy pauzą --- obecnie dość rzadko spotykana. {\color{orange}\hfill~---}
\vfill
% line break after a dash -- this is the way it should be done
\leavevmode\marginpar{\textsc{\color{teal}dobrze\\(good)}}%
To jest maciupeńki test półpauzy~-- na Zachodzie nazywanej \emph{en dash}.  {\color{orange}\hfill~--}
\par \emph{Em dash} za to nazywamy pauzą~--- obecnie dość rzadko spotykana. {\color{orange}\hfill~---}
\end{document}


To obtain correct result I had to use non-breaking space (tie) before each dash.

Is it possible to fix behavior of all en/em dashes surrounded by normal spaces in LaTeX document?

Side note: I am not asking about workarounds requiring preprocessing, like using s/ -- /~-- / in Vim/sed/perl/etc.

-
Not in a robust way. The ties are the safest method. –  egreg Jul 5 '11 at 20:30
@przemoc: 1) En dash is never surrounded by spaces. It's always surrounded by characters. 2) It's not good to brake neither after En dash, nor before it. Better both words, or numbers, before and after the En dash to be on the same line. 3) Em dash is treated as the hyphen not only in polish, but as far as I know, in all European languages. –  Karl Karlsson Jul 5 '11 at 22:06
@Karl: In german en-dash is used to separate "thoughts" in a sentence ("Gedankenstrich") and is always surrounded by spaces if it used between words: "foo -- bar". There is no space if a comma or dot follows: "foo --, bar". Breaks before and after the en dash in the first case are ok, in the second case ("--,") the break before should be supressed. –  Ulrike Fischer Jul 6 '11 at 7:26
@Karl: I have never seen an en dash separating words (not ranges) without spaces. Em dash is (often) not surrounded by spaces in English typography, but even here it's not the case for many other languages — for example, you ought to use thin/hair spaces in Russian and Latvian, except for ranges. –  Andrey Vihrov Jul 6 '11 at 9:45
@Karl: Wrt. 1): Even in English writing, many style guides recommend to use an en-dash surrounded by spaces in places where the (American) writer would commonly use an em-dash: [...] For example, the Canadian The Elements of Typographic Style recommends the spaced en dash – like so – and argues that the length and visual magnitude of an em dash "belongs to the padded and corseted aesthetic of Victorian typography." (en.wikipedia.org/wiki/Dash). –  Daniel Jul 6 '11 at 10:03

The only way to accomplish the task is to make - an active character and define it in such a way that it expands to a minus sign in math mode while, in text mode it looks forward to see whether one or two hyphens follow it and act in consequence.

A possible implementation with the active hyphen is as follows

\makeatletter
\def\ah@hyphen{-}
\def\ah@endash{--}
\def\ah@emdash{---}
\catcode\-=\active
\protected\def-{\ifmmode\ah@hyphen\else\expandafter\ah@check\fi}
\def\ah@check{\@ifnextchar-{\ah@checki}{\ah@hyphen}}
\def\ah@checki#1{\@ifnextchar-{\ah@three}{\ah@two}}

\def\ah@two{\unskip~\ah@endash\space\ignorespaces}
\def\ah@three#1{\unskip~\ah@emdash\space\ignorespaces}
\makeatother


There is, however, a way out using Unicode characters. If your document is written in UTF-8 you can say

\usepackage{newunicodechar}
\newunicodechar{–}{\unskip~--\space\ignorespaces}
\newunicodechar{—}{\unskip~---\space\ignorespaces}


where in line 2 – is U+2013 EN DASH and in line 3 — is U+2014 EM DASH; using these characters in your source will do what you want. The main problem here is that they are almost indistinguishable from each other in a monospaced font. Just to show them I'll put them in a code box:

– U+2013 EN DASH
— U+2014 EM DASH


and here's how they appear in a quotation box:

– U+2013 EN DASH
— U+2014 EM DASH

The rendering on screen depends on the font, of course.

-
– — <- here they are in monospace font. –  przemoc Jul 5 '11 at 21:11
@przemoc - just edited your contribution into the answer... –  Brent.Longborough Nov 15 '11 at 12:38
@Brent.Longborough Thanks –  egreg Nov 15 '11 at 13:43
@Brent.Longborough: I thought the idea of showing the dashes as a quote was to make the difference between them visible. As code, they're just as indistinguishable as in the code sample above. –  doncherry Nov 15 '11 at 14:19
I moved the active character part in your solution to the front, just to keep things together that belong to one thought. –  doncherry Nov 15 '11 at 14:23

Inserting ties manually is still a good option: it doesn't have side effects, it is readable and it is easy to train yourself to always type ~---.

That said, the extdash package provides commands for dashes with non-breaking spaces. With the [shortcuts] option the \---` command is made available and stands for an em-dash with non-breaking space. Space surrounding the dash is also reduced for a better appearance.

-