4

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?

hpesoj626
  • 3,529
  • 1
  • 17
  • 25

1 Answers1

1

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
```
Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • I was thinking along the line of putting the commands in a separate file. But I haven't thought of looking at the LaTeX output. The quoting is worth exploring. – hpesoj626 Apr 28 '18 at 23:18
  • @hpesoj626 The quoting issue has been reported and apparently solved, but I cannot test it easily. See the edited answer, where I have also streamlined the solution. – Ralf Stubner Apr 29 '18 at 06:19
  • Thanks, @RalfStubner. I'll be reading the thread. Thanks for the effort, man. – hpesoj626 Apr 29 '18 at 06:37
  • Any way to do this for word_document output? – jzadra Jun 20 '18 at 20:43
  • @jzadra I don’t know how to do this with Word. – Ralf Stubner Jun 20 '18 at 21:21
  • @jzadra Are the ligatures in Fira Code compatible with Word in the first place, i.e. independent of using Rmarkdown? I don't have a copy of Word to check and it is not listed in Fira Code's compatibility table. – Ralf Stubner Jun 21 '18 at 05:39
  • @RalfStubner It sure doesn't! Thanks for the suggestion to check on that first. – jzadra Jun 21 '18 at 16:52