14

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!

1

5 Answers 5

30

-- 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.

4
  • 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, 2011 at 19:31
  • 6
    @Shahbaz microtype does come with TeX Live.
    – N.N.
    Nov 30, 2011 at 19:36
  • @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. Nov 30, 2011 at 21:33
  • Note: this only works for PDFLaTeX. For XeLaTeX and LuaLaTeX refer to tex.stackexchange.com/q/1841/250119. ■ Note on the terminology used in this answer: "the tt-family" does not refer to the font family literally named tt, instead the tt* refers to the default typewriter font \ttdefault at \AtBeginDocument. Quoting microtype documentation: "If a value is followed by an asterisk (like rm* and sf* in the first example), it does not designate an NFSS code, but will be translated into the document’s \〈value〉default, e.g., \rmdefault. "
    – user202729
    Nov 23, 2023 at 8:59
21

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

enter image description here

\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.

3
6
\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}
2
  • What does this code do?
    – N.N.
    Nov 30, 2011 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, 2011 at 20:46
3

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.

0

For Rmarkdown users: the -{}- doesn't seem to work and curly braces are inserted into the text. However, a workaround is to instead insert ${}$ between any consecutive hyphens. This:

-${}$- -${}$-${}$-

Outputs as:

-- ---
3
  • Why the $ signs, Math isn't needed here. Apr 6, 2023 at 0:16
  • @barbarabeeton It's just an empty math environment that tricks LaTeX into not seeing that there are consecutive dashes. In using LaTeX via Rmarkdown -{}- itself doesn't work. I should probably specify this in the answer
    – arara
    Apr 6, 2023 at 0:36
  • That makes more sense. Thanks. (I'm not an R user. I still tend to think in plain TeX.) Apr 6, 2023 at 1:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .