8

A programming tool that I use (coqdoc) generates LaTeX documentation from my source code. Parts of the manual include pretty-printed code listings and math proofs.

These listings and proofs are rather math-heavy, and thus are not wrapped in verbatim or lstlistings. When successions of dashes are found (-- or ---) in code or proofs, they are dumped literally in the LaTeX output, and thus get converted to and as the document is typeset by pdflatex. Code and proof listings are wrapped in a particular environment, which I can renew.

What can I add to my preamble to disable en and em dash ligatures in that specific environment?

Ideally, I would like something like the following:

\documentclass{article}
\usepackage[T1]{fontenc}
\renewenvironment{coqdoccode}{MAGIC HERE}{}

\begin{document}
This should be an an and an em dash: -- ---
\begin{coqdoccode}
  This should be two and three small dashes: -- ---
  This should still be ligatures: ff fi
\end{coqdoccode}
\end{document}

microtype only seems to be able to disable ligatures at the entire document level.

  • 2
    Is LuaLaTeX an option? Switching off ligatures in LuaLaTeX – Alan Munn Apr 7 '15 at 17:54
  • Unfortunately no; the output of the tool is heavily reliant on pdflatex. – Clément Apr 7 '15 at 17:55
  • The verbatim environment use \@noligs to stop -- ligatures, and the tt font has no ff ligatures, is monospace acceptable for this use? – David Carlisle Apr 7 '15 at 17:57
8

Activate the hyphen in the coqdoccode environment, defining it as a hyphen followed by a zero kern.

\documentclass{article}
\usepackage[T1]{fontenc}

\newcommand{\activatehyphen}{%
  \begingroup\lccode`~=`-
  \lowercase{\endgroup\def~}{\char`\-\kern0pt }%
  \catcode`\-=\active
}

\newenvironment{coqdoccode}
  {\flushleft\activatehyphen\ttfamily}
  {\endflushleft}

\begin{document}
This should be an an and an em dash: -- ---
\begin{coqdoccode}
  This should be two and three small dashes: -- --- \\
  This should still be ligatures: ff fi \\
\end{coqdoccode}
\end{document}

enter image description here

I added \ttfamily just for clarity, but it's not necessary.

This is the result without \ttfamily in the definition

enter image description here

  • I don't see ligatures for ff and fi in the last output... – Bakuriu Apr 7 '15 at 21:11
  • @Bakuriu That's because I used \ttfamily. – egreg Apr 7 '15 at 21:13
  • That's awesome, thanks. Could you explain how this works? I understand that it makes - active, but I'm not sure what the \lowercase and \lccode parts do. – Clément Apr 7 '15 at 21:44

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.