How do I prevent LaTeX from creating en-dash and em-dash?
--
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.
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.
\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}