3

I would like to set up my LaTeX (XeTeX) document in such a way so that I could type a hyphen (-, the one on the keyboard) and it would get replaced with an en-dash (, the "longer" dash) in the resulting document.

For example, for a LaTeX source:

Lietuvos intelektualai - prieš liberaliąją demokratiją? % "Usual" dash typed

I would like to get the en-dash ("long" dash) in the rendering:

Lietuvos intelektualai – prieš liberaliąją demokratiją?

and not the usual hyphen ("short" dash):

Lietuvos intelektualai - prieš liberaliąją demokratiją?

I'm aware that this could be achieved by typing two hyphens (--) instead of a single hyphen (-), but is there a better way to do that?

(Due to the nature of documents I typeset and the requirements of the language I use, I'm required to type an en-dash 98% of all times and might need the "usual" hyphen just occasionally).

Linas Valiukas
  • 565
  • 6
  • 9
  • Better in what way? What do you find wrong with the way it is? Also, are you entirely sure that you will not want to write a hyphen at all? Or do you want to remap that to something else as well? – jonalv Feb 16 '12 at 16:11
  • Use your editor's text replace feature and make a habit of writing ~-- from now on. – Andrey Vihrov Feb 16 '12 at 16:13
  • 8
    One could define a mapping which replace the hyphen. But this would also affect hyphens inserted a hyphenation points – where such an en-dash is wrong. And quite probably you will soon fine other places where you actually don't want the en-dash (like in en-dash). In the long run you will probably spend more time to get around the side-effects than you gain now by avoiding a correct input (either -- or a real en-dash). – Ulrike Fischer Feb 16 '12 at 16:15
  • Also, since you're using XeLaTeX, there's no reason not to simply type the relevant character directly in your source. – Alan Munn Feb 16 '12 at 16:26
  • I agree with most others here that it's not a good idea, but if you really want it, you can make the dash active and replace it with \textendash. You can simply write \catcode`-=13\def-{\textendash} and then use - as if it were an endash. – Roelof Spijker Feb 16 '12 at 16:33
  • @Alan: The relevant endash character would work with pdflatex too. The reason why I'm not using it is not the engine but the keyboard and my editor: -- is much to type and easier to see in the source. – Ulrike Fischer Feb 16 '12 at 16:35
  • you could type a space followed by a hyphen and another space, and at the end, globally replace that combination by space, two hyphens, space. that wouldn't conflict with "real" hyphens for hyphenation, although it could be a problem in math unless you're careful never to leave a space after a hyphen meant as a minus. it would even withstand repeated "replacements" since the three-character input combination shouldn't occur anywhere else. – barbara beeton Feb 16 '12 at 16:58
  • Found a workaround for XeTeX:
    `\usepackage{ifxetex}
    
    \ifxetex
        \usepackage{xesearch}
        \UndoBoundary{-}
        \SearchList{dash}{--}{-}
    \else
        \typeout{Well, too bad.}
    \fi`, just that SE doesn't allow me to answer my own question yet.
    
    – Linas Valiukas Feb 16 '12 at 17:00
  • @UlrikeFischer I agree with you completely on the visibility issue. And typing -- is effectively semantic markup, which is good too. – Alan Munn Feb 16 '12 at 17:18

1 Answers1

1

Found a workaround for XeTeX:

\usepackage{ifxetex}

\ifxetex
    \usepackage{xesearch}
    \UndoBoundary{-}
    \SearchList{dash}{--}{-}
\else
    \typeout{Well, too bad.}
\fi

Also, note the comments under the original question discussing why automatic replacements are not always a good idea.

Linas Valiukas
  • 565
  • 6
  • 9