4

I am trying to typeset an em dash using --- inside an alltt environment, but I get three hyphens instead of a single em dash.

MWE

\documentclass[12pt]{article}

\usepackage{alltt}

\begin{document}
Usually, \verb"---" becomes an em dash, i.e. "---", in normal mode.
But in the alltt environment, it becomes three hyphens.
\begin{alltt}
Please---will you give me an em dash?
\end{alltt}
\end{document}

Response to comments The comments asked why I want to typeset an em dash in a monospaced environment. I would like to typeset a dash that looks like an em dash, but takes the length of two spaces in a monospaced environment, i.e. like "--" but joined together to form a single em dash like character.

2
  • An em-dash doesn't make much sense in a monospaced font, does it? – egreg Sep 22 '14 at 17:45
  • @egreg Indeed, I now have changed my question to ask for something different that fits in a monospaced environment. – I Like to Code Sep 22 '14 at 18:19
4

My suggestion would be to "conceal" it in the form of a macro where the em-dash it taken from \normalfont:

enter image description here

\documentclass{article}
\newcommand{\emdash}{{\normalfont ---}}
\usepackage{alltt}

\begin{document}
Usually, \verb"---" becomes an em dash, i.e. ``---'', in normal mode.
But in the alltt environment, it becomes three hyphens.
\begin{alltt}
Please\emdash{}will you give me an em dash?
\end{alltt}
\end{document}

Unless the mono-spaced font comes with an em-dash, it will be set as separate dashes. You could force a mono-spaced-looking em-dash, perhaps with a definition for \emdash that resembles:

enter image description here

\documentclass{article}
\makeatletter
\newlength{\emdashttlen}
\settowidth{\emdashttlen}{\ttfamily---}
\newcommand{\emdash}{\makebox[\emdashttlen]{-\cleaders\hbox{\hspace{-.45ex}-}\hfill\kern0pt}}
\makeatother
\usepackage{alltt}

\begin{document}
Usually, \verb"---" becomes an em dash, i.e. ``---'', in normal mode.
But in the alltt environment, it becomes three hyphens.
\begin{alltt}
Please---will you give me an em dash?
Please\emdash{}will you give me an em dash?
\end{alltt}

\end{document}

Some information on leaders can be found in Want to fill line with repeating string.

2
  • 3
    wouldn't you want a dash that's a multiple of the character width, to preserve monospacing? – David Carlisle Sep 22 '14 at 17:57
  • 1
    Setting \emdashttlen in the preamble will not give a correct length when \small, \footnotesize or another size changing command is in force. – egreg Sep 22 '14 at 19:52
2

Simple solution: use UTF-8

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{alltt,etoolbox}
\apptocmd{\alltt}{\def\textemdash{{\fontfamily{cmvtt}\selectfont---}}}{}{}

\begin{document}

\begin{alltt}
Please—will you give me an em dash?
Please--will you give me an em dash?
\end{alltt}

\end{document}

Note that the first line contains — (Unicode U+2014 EM DASH)

enter image description here

More complicated solution: transform --- into {\fontfamily{cmvtt}\selectfont---}

\documentclass{article}

\usepackage{alltt,etoolbox}

\makeatletter
\begingroup\lccode`~=`-
\lowercase{\endgroup\apptocmd{\alltt}{\let~\alltthyphen}{}{}
\newcommand\alltthyphen{\@ifnextchar~{\@alltthyphen@i}{\char`\- }}
\def\@alltthyphen@i#1{% #1 is -
  \@ifnextchar~{{\fontfamily{cmvtt}\selectfont---}\@gobble}{\char`\-\char`\- }%
}}
\makeatother

\begin{document}

\begin{alltt}
Please---will you give me an em dash?
PleaseXXwill you give me an em dash?
\end{alltt}

\end{document}

The output is the same as before

Overcomplicated solution: fake an em-dash, because the font family hasn't a variable width typewriter font

\documentclass{article}

\usepackage{alltt,etoolbox}
\usepackage{tgcursor}

\makeatletter
\begingroup\lccode`~=`-
\lowercase{\endgroup\apptocmd{\alltt}{\let~\alltthyphen}{}{}
\newcommand\alltthyphen{\@ifnextchar~{\@alltthyphen@i}{\char`\- }}
\def\@alltthyphen@i#1{% #1 is -
  \@ifnextchar~{\fake@em@dash\@gobble}{\char`\-\char`\- }%
}}
\def\fake@em@dash{%
  \sbox0{--}%
  \makebox[\wd0][s]{-\hss-\hss-}%
}
\makeatother

\begin{document}

\begin{alltt}
Please---will you give me an em dash?
PleaseXXwill you give me an em dash?
\end{alltt}

\end{document}

enter image description here

You might want to simplify the setting using just -- instead of ---; this would need changes to the macros above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.