4

I have Fira Code with ligatures enabled as my default mono font using fontspec, but this doesn't seem to have any effect on verbatim or lstlisting environments.

Why are those contexts different, and how can I fix it to get these ligatures everywhere?

\documentclass{article}

\usepackage{fontspec}
\setmonofont[Contextuals={Alternate}]{FiraCode}

\usepackage{listings}
\lstset{basicstyle=\ttfamily}

\begin{document}

\section{tt}

{\tt ->}

\section{verb}

\verb|->|

\section{verbatim}

\begin{verbatim}
->
\end{verbatim}

\section{lstlisting}

\begin{lstlisting}
->
\end{lstlisting}

\end{document}

Rendered with lualatex:

  • 3
    The verbatim- and lstlisting-environments are made to represent code and not the effect the code would have. Therefore it is not desirable to have ligatures inside of them. – Skillmon Mar 26 '17 at 18:11
  • Shouldn't it be Fira Code instead of FiraCode ? Are you sure you have the right spelling in your file? – Moriambar Mar 26 '17 at 18:14
  • 1
    @Skillmon I don't know what you mean by "the effect code would have", but the Fira Code ligatures are specifically designed to display code. – Chris Martin Mar 26 '17 at 18:20
  • @ilFuria It works with or without the space, doesn't seem to matter. – Chris Martin Mar 26 '17 at 18:21
  • @ChrisMartin: I mean it is designed to print out exactly what you gave it. Not processing your code in any other way. For me a ligature of -> doesn't make sense, because I won't ever use that kind of character in any programming language I know. But for example in C I use the combination -> for lists. – Skillmon Mar 26 '17 at 18:27
  • @ChrisMartin what I'm trying to say is, that it is the design and sense of lstlisting or verbatim to not evaluate any contents you give them (except for lstlisting performing stuff like syntax-highlighting). – Skillmon Mar 26 '17 at 18:30
  • @Skillmon So perhaps I'm looking for a different environment for my code blocks then? – Chris Martin Mar 26 '17 at 18:33
  • you can use the rightarrow character directly but as @Skillmon says verbatim environments go to some lengths to break ligartures, if you were showing (say) c code you would want -> (legal c syntax) not (a syntax error) – David Carlisle Mar 26 '17 at 18:38
  • @ChrisMartin May I ask, why you need those ligatures? – Skillmon Mar 26 '17 at 18:39
  • @Skillmon There are a lot of operators in Haskell that look very nice with these ligatures, and in the context of the book I'm working on, its intended audience having intermediate-level Haskell knowledge, it will be clear what source characters they represent. – Chris Martin Mar 26 '17 at 18:43
  • See the "literate programming" section of the listings manual which describes how to make suitable substitutions. – Andrew Swann Mar 26 '17 at 18:56
  • Surely it would be clearer to actually show the code people should type and not something they have to interpret into code they should type? Aesthetics are important, but they are surely secondary to the meaning you are conveying. \tt has been obsolete for 20+ years in LaTeX and ought not be used in documents using 2e. Unless you are still running 2.09, you shouldn't use it. (And if you are running 2.09, I think the rest of the code is probably wrong, but that's before my time.) – cfr Mar 26 '17 at 19:10
  • @cfr The meaning of the arrows is exceedingly obvious in context, and the rest of the ligatures are minor positioning adjustments that shift groups of characters that form infix operators closer together. – Chris Martin Mar 26 '17 at 19:22

1 Answers1

5

LaTeX actively suppress the ligatures of - and > in verbatim context through the \verbatim@nolig@list. You can change/empty this list but be aware that it can affect other combinations too. listings works differently. Here you can use the literate option.

\documentclass{article}
\usepackage{fontspec}
\setmonofont{FiraCode-Regular.otf}[Contextuals={Alternate}]
\usepackage{listings}
\lstset{basicstyle=\ttfamily,
        literate={->}{\texttt{->}}{1}}
\begin{document}
{\ttfamily ->  -- >>}


\verb|-> -- >>|

\makeatletter
\def\verbatim@nolig@list{}%
\makeatother
\verb|-> -- >>|

\begin{lstlisting}
-> -- >>
\end{lstlisting}
\end{document}

enter image description here

gernot
  • 49,614
Ulrike Fischer
  • 327,261