2

In standard LaTeX (i.e., with, say, cmr fonts etc) a "---" is replaced by an emdash. I believe this is treated as a ligature. Is it possible to make a "---", in effect, generate the contents of the following macro?

\def\mdash{\unskip\kern.16667em\textemdash\penalty\exhyphenpenalty\hskip.16667em\relax}

(See comment on Dashes: - vs. – vs. —).

Update: I'd like to clarify what I'm trying to achieve. Suppose I have several document that are sharing a "company style file" (or a company class file). This allows documents that use semantic markup to be made to look consistent. For example the meaning of \emph can be made to mean italics or (shudder) underlining. What if I wanted to consistently make emdashes (entered as ---) look like \mdash (i.e., adhere to that spacing). Aside from editing all the files and replacing --- with \mdash is there a .sty-based solution?

  • 2
    not without difficulty, you'd have to make - active and then make two -- and three --- expand to required macros (fairly easy) and then make everything else not break lengths, (\hspace{-3pt}), math $1-2$, hyphenation hypenation{foo-bar} this is as hard as you want to make it, depending on how many packages you want to keep working – David Carlisle Oct 17 '16 at 21:13
  • 3
    If you can input (U+2014), it's easier to make it into a command that does what you need. – egreg Oct 17 '16 at 21:28
  • Do you always use the same font for that? Is it entirely under your control (on your own computer)? If so, one possibility is to edit the font. Simply change the length of the emdash glyph, and leave a little space on each side. Or, if the font is Open Type, there may be an alternative emdash at three-quarters em instead of one em, which can be selected as part of a style set. I have done this myself, but then fonts are under my personal control. Actually, egreg's above comment is a good start, without having to edit the font. – user103221 Oct 18 '16 at 1:10
  • Thank you, that helps. I think my question may be lacking in context, so I will add some. – banbh Oct 18 '16 at 2:24
3

Really I'd just do a global replace in the editor of --- to \mdash but if you want to try TeX then

enter image description here

\documentclass{article}

\begin{document}

a-b 1--2

a---b c --- d

a \raisebox{-5pt}{zzz} a

$ 1+2 -3 $

x \the\numexpr -\numexpr 1-2\relax\relax

\hyphenation{zzzz-yyyy}

\rule{--3em}{2pt}

\parbox{--3em}{\raggedright a zzzzyyyy}

section: \addtocounter{section}{-1}\thesection

\bigskip\hrule\bigskip

\makeatletter

\edef\dashone{\string-}
\edef\dashtwo{\dashone\dashone}
\edef\dashthree{\dashone\dashone\dashone}
\catcode`\-=13
\protected\def\dashactive{\futurelet\dashtmp\dashtesta}
\let-\dashactive
\def\dashtesta{%
\ifx\dashtmp-\expandafter\@firstoftwo
\else\expandafter\@secondoftwo
\fi
\dashbgobble\dashone}
\def\dashbgobble#1{\futurelet\dashtmp\dashtestb}
\def\dashtestb{%
\ifx\dashtmp-\expandafter\@firstoftwo
\else\expandafter\@secondoftwo
\fi
\mdashgobble\dashtwo}
\def\mdashgobble#1{\mdash}
\def\mdash{%
\leavevmode% you need this:-)
\unskip\kern.16667em\textemdash
\penalty\exhyphenpenalty
\hskip.16667em\relax}

\let\oldsetlength\setlength
\protected\def\setlength#1#2{%
  \let-\dashone\oldsetlength{#1}{#2}\let-\dashactive}

\let\oldhyphenation\hyphenation
\protected\def\hyphenation#1{%
  \let-\dashone\oldhyphenation{#1}\let-\dashactive}

\makeatother

a-b 1--2

a---b c --- d

a \raisebox{-5pt}{zzz} a

$ 1+2 -3 $

x NO!%\the\numexpr -\numexpr 1-2\relax\relax

\hyphenation{zzzz-yyyy}

\rule{--3em}{2pt}

\parbox{--3em}{\raggedright a zzzzyyyy}

section: NO!%\addtocounter{section}{-1}\thesection

\end{document}

The above uses a non-expandable \futurelet test to look ahead for one or two more - following a -, this means it doesn't work in primitive constructs expecting a number (if you remove the % you will get errors at the NO!).

An alternative would be to define an expandable lookahead based on macro arguments \def-#1{\ifx-#1 ... that would make the cases with primitive number work but would give nasty parse errors from any context that looked like -} which would stop - grabbing an argument and would lose brace group information in contexts that looked like -{ as - would grab the whole group not just a single character.

  • I used your definition, but I suspect you should have \ignorespaces at the end of \mdash note the asymmetric c - d – David Carlisle Oct 18 '16 at 12:33

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.