0

I am using pdfLaTex. I'm trying to define a command \cmd that prints its argument in a typewriter font (i.e. using \texttt for code formatting). For example, I want

\cmd{--my-command} 

to print exactly

--my-command 

(with two separate hyphens), but instead it produces an en‐dash followed by my-command (i.e. “–my-command”).

My original macro:

\newcommand{\cmd}[1]{\texttt{\detokenize{#1}}} 

I’ve tried several methods to “break” the ligature, including:

  1. Disabling hyphen ligatures:
\usepackage{microtype} \DisableLigatures[-]{encoding = *, family = * } \newcommand{\cmd}[1]{\texttt{\detokenize{#1}}} 
  1. Disabling all ligatures:
\usepackage{microtype} \DisableLigatures[]{encoding = *, family = * } \newcommand{\cmd}[1]{\texttt{\detokenize{#1}}} 
  1. Manually inserting a zero-width kern between hyphens (using xstring):
\usepackage{xstring} \newcommand{\cmd}[1]{% \StrSubstitute{#1}{--}{-\kern0pt-}[\temp]% \texttt{\temp}% } 
  1. Disabling all ligatures:
\usepackage{microtype} \DisableLigatures{} \newcommand{\cmd}[1]{\texttt{\detokenize{#1}}} 
  1. Disabling all ligatures:
\usepackage{microtype} \DisableLigatures{family=rm*} \newcommand{\cmd}[1]{\texttt{\detokenize{#1}}} 

None of these methods work as expected; the output still ligatures the two hyphens.

2
  • 3
    Sounds like a job for \verb|...| (or listings or minted) Commented Feb 4 at 15:20
  • 2
    To be fair, \documentclass{article}\begin{document}\texttt{\detokenize{--my-command}}\end{document} shows no ligatures. There's something else going on which you do not show. Commented Feb 4 at 15:45

2 Answers 2

3

You can selectively disable ligatures with microtype, because you don't want to disable them for the main font.

\documentclass{article} \usepackage[T1]{fontenc} \usepackage{microtype} \DisableLigatures[-]{encoding=*,family=tt*} \begin{document} endash -- and emdash --- \texttt{--my-command} \end{document} 

output

On the other hand, OT1 encoded monospaced fonts don't have - ligatures and depending on what you want to feed \cmd with, switching to OT1 could do.

\documentclass{article} \usepackage[T1]{fontenc}% just for the example \newcommand{\cmd}[1]{{\fontencoding{OT1}\ttfamily #1}} \begin{document} endash -- and emdash --- \cmd{--my-command} \end{document} 
2
  • Would something such as -\strut- locally break the ligature? Works for me, but I am using very different class and fonts and packages. Commented Feb 4 at 16:37
  • 1
    @rallg \kern0pt would do, but it requires typing it any time you have --. Commented Feb 4 at 16:50
0

Using the following resolves the issue:

\usepackage{microtype} \DisableLigatures[-]{} 
1
  • Welcome to TeX.SE! Please always show here an compilable TeX code with your solution. And please look before into the other answer, there is an complete answer already available. Commented Feb 5 at 13:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.