# How do I prevent LaTeX from creating en-dash and em-dash?

All over the internet, there's this question on how to make LaTeX do the en-dash and em-dash.

I have the opposite problem. I have some text from which I am generating HTML and LaTeX outputs. The original text has nothing to do with LaTeX and therefore -- and --- are needed to be written as they are, as opposed to being converted to en and em dashes.

Putting it in \texttt should probably solve it, but this could happen in normal text and I'd like to know if there is any other solutions.

P.S. I am writing the LaTeX files with a software, so in fact for example a thing like \dash{} instead of every - is easier for me, if such a thing exists that is!

-

-- and --- are TeX ligatures (like fl or fi). Disabling ligatures is one of the features of package microtype. You may deactivate all ligatures of all fonts:

\documentclass{article}
\usepackage{microtype}
\DisableLigatures{}
\begin{document}
---
\end{document}


or only the ligatures of the rm-family:

\documentclass{article}
\usepackage{microtype}
\DisableLigatures{family=rm*}
\begin{document}
---
\end{document}


or only the ligatures -- and --- of all fonts:

\documentclass{article}
\usepackage{microtype}
\DisableLigatures[-]{}
\begin{document}
---
\end{document}


or of one family, e.g. the tt-family:

\documentclass{article}
\usepackage{microtype}
\DisableLigatures[-]{family=tt*}
\begin{document}
\textrm{---} but \texttt{---}
\end{document}


See the manual of package microtype for more information about disabling ligatures.

-
Nice! This package doesn't come with TexLive, right? I think I'll stick to the accepted answer as I can give the LaTeX files away without instructing people to install additional packages. – Shahbaz Nov 30 '11 at 19:31
@Shahbaz microtype does come with TeX Live. – N.N. Nov 30 '11 at 19:36
Alright cool! I'll give it a shot – Shahbaz Nov 30 '11 at 20:00
@Shahbaz: Have a look at the package link at my answer. You will find a heading "Getting it" there. Below this heading you will find the information, that there's not only a TeXLive package with microtype but a MiKTeX package too. That's a very useful feature of Jim Hefferon's AZ web views of CTAN. – Schweinebacke Nov 30 '11 at 21:33

You could use {-}{-} and {-}{-}{-} for en-dash and em-dash respectively, or define a command \dash that does this:

\documentclass{article}
​\begin{document}
\newcommand{\dash}{{-}}%
\renewcommand{\arraystretch}{1.2}
\begin{tabular}{lccc}
& Default & \verb!{ }! & \verb!\dash! \\ \hline
\verb!-! & - & {-} & \dash \\
\verb!--! & -- & {-}{-} & \dash\dash \\
\verb!---! & --- & {-}{-}{-} & \dash\dash\dash \\ \hline
\end{tabular}
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​


I assume you'll be able to differentiate between math and text mode, since using {-} or \dash in math mode would mess up operator spacing.

-
The files are about code documentation and writing math is unlikely. Not to mention the primary target is HTML and I'm doing LaTeX as a side product (and quite a troublesome one!) Thanks for the answer, {-} looks very good – Shahbaz Nov 30 '11 at 18:28
Also -{}- and -{}-{}- works. – Jean-Christophe Dubacq Nov 30 '11 at 19:14
@Jean-ChristopheDubacq: True, but in an automated process, you would probably output -{}-{} and -{}-{}-{} since - would be replaced with -{} (or {}-). – Werner Nov 30 '11 at 19:17
@Werner, I'm back with a related question! If using soul package, I write: \ul{This{-}is{-}something}, what I get is This-is-somethi (underlined, of course). Which means \ul eats up as many {-} as I have! Any ideas? – Shahbaz Mar 5 '12 at 16:15
@Shahbaz: Your question won't be seen by many people here, so it would be best to repost it as a fresh question. Follow-up questions like this are more than welcome! Please use the "Ask Question" link for your new question; there you can link to this question to provide the background. – Werner Mar 14 '12 at 23:43
\def\normalhyphen{-}
\catcode-=\active
\protected\def-{\normalhyphen\ifmmode\else\kern0pt \fi}


You won't be able to specify negative dimensions by -, but you can say

\setlength{\mylen}{\normalhyphen 3pt}

-
What does this code do? – N.N. Nov 30 '11 at 19:36
The code makes - into an active character, which behaves like a macro; if it's given in text mode it becomes -\kern0pt, which will prohibit a ligature if the following character is again a -. – egreg Nov 30 '11 at 20:46

The simplest possibility is use an environment that is designed for verbatim text, such as the verbatim environment or the \verb command. I mention it because you said in a comment that you are documenting code and this is a pretty straightforward way to print code. But it doesn't sound like that is what you want since you've already considered \texttt.

Better yet, the listings` package is designed to produce nicely typeset code with minimal effort. Personally I've had great results typesetting SQL snippets and even entire Perl scripts. The brilliant thing about it is that I can import working code from a file and print it well formatted without having to edit the code itself.

-