3

I am trying to improve the current implementation (v0.1b) of the lstfiracode package. In particular, I wish to achieve the opposite of Use Fira Code font with ligatures in code listings.

Here is what I have come up with

% !TeX program = XeLaTeX
% !TeX encoding = UTF-8 Unicode
\documentclass{article}
\usepackage{fontspec}
\setmonofont{FiraCode-Regular.otf}[ % https://github.com/tonsky/FiraCode/releases
  Ligatures=Common,Contextuals=Alternate
]

\makeatletter
% Snippets of lstfiracode.sty, v0.1b
\let\lstfiracode@verbatim@nolig@list\verbatim@nolig@list
\newcommand*\ActivateVerbatimLigatures{%
  \def\verbatim@nolig@list{\do\`\do\,\do\'}%
}
\newcommand*\DeactivateVerbatimLigatures{%
  \expandafter\def\expandafter\verbatim@nolig@list\expandafter{%
    \lstfiracode@verbatim@nolig@list
    \do\w\do\*\do\/\do\:\do\=\do\!\do\~\do\[%
    \do\#\do\(\do\?\do\_\do\.\do\;\do\&\do\|%
    \do\+\do\$\do\@\do\%\do\]% Do "7B and "7D, how?
  }%
}
\makeatother

\begin{document}

\ActivateVerbatimLigatures
\begin{verbatim}
A<-www>>=B#{C|}D
\end{verbatim}

\DeactivateVerbatimLigatures
\begin{verbatim}
A<-www>>=B#{C|}D
\end{verbatim}

\end{document}

sample

As shown in the above example, I could not figure out how to \do\{ and \do\}.

As far as I know, simply \do\{ or \do\} messes up category code and the result is a big ⚠. However, the \do\# turns out just fine, even though \do@noligs has made # active before \def~{\leavevmode\kern\z@\char`#1} and before the other \do’s.

What should I do to suppress the Fira Code ligatures involving { and } (and \, just in case)?

  • I would say you are misusing the noligs-code (and I don't see how it should work). Why don't you simply remove the Contextuals=Alternate if you want to suppress them? – Ulrike Fischer Dec 21 '18 at 17:00
  • I'm afraid you can't like this: characters specified in the “nolig list” are made active, but then \end{verbatim} doesn't match the terminator sequence, which is expecting { and } with category code 12. – egreg Dec 21 '18 at 17:05
  • @UlrikeFischer I absolutely agree. In fact, to avoid ligatures all together, one should simply use Fira Mono instead. I am just wondering if a counterpart of \ActivateVerbatimLigatures is possible. – Ruixi Zhang Dec 21 '18 at 17:40
  • @egreg Ahhh, this is what I have suspected. It is extremely lucky that \end{verbatim} does not contain the lowercase letter w. – Ruixi Zhang Dec 21 '18 at 17:44

Your Answer

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

Browse other questions tagged or ask your own question.