# Hyphens at end of line disappear upon pasting, even if they were present in the input

When I paste text from pdflatex output, there are two things about hyphens that I would like to change:

• Hyphens paste as "hyphen-minus" (U+002D, "-"), but I would prefer the newer Unicode character "hyphen" (U+2010, "‐"), used for textual hyphens.
• Line-breaking hyphens always disappear. This is good if there was no hyphen in the input, but when a word such as "ice-cream" breaks over a line, it would be nice to keep the hyphen.

Note that line-final en-dashes paste correctly. Here is some code illustrating the issue:

\documentclass{article}
\input glyphtounicode
\pdfgentounicode=1
\usepackage[T1]{fontenc}

\begin{document}

Hello World, this is dummy text intended to cause a line break: I love line-breaking. Now a long word without an original hyphen: antidisestablishmentarianism. Some dummy text for getting the desired line breaks; some dummy text for getting the desired line breaks.

Compound adjective (with en-dash): World\ War\ II--related, World\ War\ II--related, World\ War\ II--related.

\end{document}


With this code, the pasting behavior is as follows: the hyphen in "line-breaking" disappears (not desired), the hyphen for the line-breaking of "antidisestablishmentarianism" disappears (desired), and the en-dash remains (desired). (Just in case it matters, I'm using Adobe Reader X (version 10.1.4) on Windows 7.)

Is there an easy way to address these two points? An ideal solution won't make use of new commands (say, using accsupp, as great as this package is) but will modify the way (La)TeX deals with - in its input source code. Also a solution ideally applies the specialized hyphen character (U+2010) conservatively: for example hyphens in URLs are ordinarily simple hyphen-minuses. (Yep, I know that all this might be quite tricky to implement.)

-
with evince 3.4.0 on Ubuntu 12.04 both hyphens are copied as hyphen-minus (u+002D) and the en-dash is copied as it is (u+2013) –  henrique Sep 22 '12 at 1:09
See this question for a starting point. The general idea is to make - an active character, then you can redefine its behavior to whatever you want. It might require some TeX wizardry to code all of this correctly. tex.stackexchange.com/questions/5737/changing-to-textendash –  krlmlr Oct 18 '12 at 21:22

Here's a first step towards an answer, which can hopefully be made better with the help of others (or can motivate those in the know to post a better answer):

\documentclass{article}
\input{glyphtounicode}
\pdfgentounicode=1
\usepackage[T1]{fontenc}
\usepackage{accsupp}

\usepackage{url}

\let\originalhyphen-

\makeatletter
\newcount\vh@hyphens

\def\vh@mkhyphens#1\originalhyphen
{%
\ifnum#1>\z@
\expandafter\vh@mkhyphens\number\numexpr#1-\@ne\relax\originalhyphen
\fi
\originalhyphen
}

\catcode\-=\active
\DeclareRobustCommand\visiblehyphen
{%
\ifmmode
\originalhyphen
\else
\expandafter\@visiblehyphen
\fi
}

\newcommand\@visiblehyphen
{%
}

\newcommand\@@visiblehyphen
{%
\ifnum\vh@hyphens>\z@
\expandafter\vh@mkhyphens\number\vh@hyphens\originalhyphen
\global\vh@hyphens\z@
\else
\mbox{\BeginAccSupp{method=hex,unicode,ActualText=200B2010}\originalhyphen\EndAccSupp{}}\hskip\z@%
\fi
}
\makeatother

\let-\visiblehyphen

\begin{document}

Hello World, this is dummy text intended to cause a line break: I love line-breaking. Now a long word without an original hyphen: antidisestablishmentarianism. Some dummy text for getting the desired line breaks; some dummy text for getting the desired line breaks.

Compound adjective (with en-dash):
World\ War\ II--related, World\ War\ II--related, World\ War\
II--related.

Math mode
$a^2-b^2$

Url
\begin{center}
\url{http://chat.stackexchange.com/rooms/41/tex-latex-and-friends}
\end{center}

\end{document}


It has the following drawbacks:

1. It uses the accsupp package, which you don't want. But I can't imagine how to achieve this result otherwise.
2. It seems that at least on Linux, acroread suppresses any kind of hyphen inside of a word, so u+2010 as well as u+2011 both disappear. To show that the technique works at all, I now used a zero-width space before the hyphen (u+200B), which strangely turns into a regular space here upon pasting, but maybe you can find something better by experimentation.
3. Redefining a character like - in an unexpandable way has a lot of side effects, for instance inside \numexpr or \dimexpr. Maybe it would be better to use a dedicated command for an explicit hyphen inside text instead. Or to somehow restrict the redefinition of - to the document text.

Here's the pasted text:

Hello World, this is dummy text intended to cause a line break: I love line ‐
breaking. Now a long word without an original hyphen: antidisestablishmentarianism.
Some dummy text for getting the desired line breaks; some dummy
text for getting the desired line breaks.
Compound adjective (with en ‐dash): World War II–related, World War II–
related, World War II–related.
Math mode
a2 − b2
Url
http://chat.stackexchange.com/rooms/41/tex-latex-and-friends

-
Re 1: The accsupp package is totally fine. I just meant that the user should be able to type ordinary hyphen-minuses (-) that are contextually resolved as much as possible, with the user needing to rely on new-commands-that-use-accsupp` as little as possible. Re 3: You might be right. I'm wondering about the most unobtrusive way to do this. I suppose someone else creative can refine your answer (or create a new one). Thanks! –  Lover of Structure Dec 7 '12 at 3:21