rlatexyamlpandocligature

Enable ligatures with Fira Code in PDF output of Rmarkdown


The following code enables me to use Fira Code as mono font.

---
monofont: "Fira Code"
output: 
  pdf_document:
    latex_engine: xelatex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
x <- 1:5
x != 2
```

This produces the desired ligature for != but not for <-.

enter image description here

The solution detailed in the accepted answer in this post works when done in LaTeX, e.g.,

\documentclass{article}

\usepackage{mathspec}
\setmonofont[Contextuals={Alternate}, Scale=0.75, Ligatures=TeX]{Fira Code}
\makeatletter
\def\verbatim@nolig@list{}
\makeatother

\begin{document}

\begin{verbatim}
x <- 1:5
x != 2
\end{verbatim}

\end{document}

enter image description here

The following YAML does not work.

---
monofont: "Fira Code"
output: 
  pdf_document:
    latex_engine: xelatex
header-includes: |
  \makeatletter
  \def\verbatim@nolig@list{}
  \makeatother
---

Can enabling ligatures be done directly in the YAML?


Solution

  • If you look at the generated LaTeX output you see that the curly braces have been quoted: \def\verbatim@nolig@list\{\}. This quoting issue has been discussed in several issues on github, and there seems to be a fix available (c.f. this issue), but I have not tested it since my Debian testing machine still uses pandoc 1.19.2.4. As a workaround you can place the necessary commands into a separate file, say preamble.tex:

    \makeatletter
    \def\verbatim@nolig@list{}
    \makeatother
    

    and then use

    ---
    monofont: "Fira Code"
    output: 
      pdf_document:
        latex_engine: xelatex
        include:
          in_header: preamble.tex
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r}
    x <- 1:5
    x != 2
    ```