8
votes

I have a similar question as this one, but I'm getting a different error. I want to set the mainfont in yaml to roboto, but I get the error "fontspec error: "font-not-found"" when I knit it to PDF.

---
title: "My Title"
header-includes:
  - \usepackage[sfdefault]{roboto}
  - \usepackage[T1]{fontenc}
output:
  pdf_document:
    latex_engine: xelatex
mainfont: roboto
---

Running MacTex 2016

sessionInfo()
#R version 3.3.2 (2016-10-31)
#Platform: x86_64-apple-darwin13.4.0 (64-bit)
#Running under: macOS Sierra 10.12.4

Update 1

It does work to define the font in header-includes, but it seems like the mainfont specification should also work:

---
title: "My Title"
header-includes:
  - \usepackage[sfdefault]{roboto}
  - \renewcommand{\familydefault}{\sfdefault}
output:
  pdf_document:
    latex_engine: xelatex
---

Update 2

monofont also failed for me, but header-includes worked:

---
title: "My Title"
header-includes:
  - \usepackage{fontspec}
  - \setmonofont[Mapping=tex-text]{inconsolata}
  - \usepackage[sfdefault]{roboto}
  - \renewcommand{\familydefault}{\sfdefault}
output:
  pdf_document:
    latex_engine: xelatex
---

What am I doing wrong with mainfont and monofont in the yaml?

2

2 Answers

9
votes

This option must be indented. It worked when you didn't indent the option latex_engine only because it was ignored and not really passed to pdf_document() as an argument. R Markdown uses the yaml package to parse YAML, and you can compare the output when latex_engine is indented or not:

yaml::yaml.load(
'output:
  pdf_document:
    latex_engine: xelatex
mainfont: roboto')

Output:

$output
$output$pdf_document
$output$pdf_document$latex_engine
[1] "xelatex"

$mainfont
[1] "roboto"

When not indented, you were essentially calling rmarkdown::render(, pdf_document()):

yaml::yaml.load(
'output:
  pdf_document:
  latex_engine: xelatex
mainfont: roboto')

Output:

$output
$output$pdf_document
NULL

$output$latex_engine
[1] "xelatex"


$mainfont
[1] "roboto"

In this case, pdf_document's default engine pdflatex is used.

I cannot say for sure, but I believe the roboto package is for pdflatex only. Your original example was failing because you mixed up pdflatex and xelatex: roboto is for pdflatex, and the mainfont option is for xelatex (which will be translated to LaTeX code \setmainfont{roboto} using the fontspec package). You can choose either way, but not both.

If you want to use xelatex, you have to make sure you have installed the font in your system, and you know the exact font name (case-sensitive). I guess the name is probably Roboto. Unless you have other reasons to use xelatex (e.g. you know the fontspec package well enough and want to configure more fonts), I'd recommend you to stay with the roboto package and pdflatex since it is simple enough and you don't have to learn too many lower-level technical details.

You may spend a minute reading this page about YAML: https://bookdown.org/yihui/bookdown/r-markdown.html

3
votes

Update: Yihui's answer comprehensively addresses the question

This is a problem caused by the way RMarkdown is parsing the YAML header. The latex_engine should not be indented and then it will work. Currently, I believe pandoc is ignoring the latex_engine command because it is nested in pdf_output when it is part of the output part. Below is the correct header:

---
title: "My Title"
header-includes:
    - \usepackage[sfdefault]{roboto}
    - \usepackage[T1]{fontenc}
output:
    pdf_document:
    latex_engine: xelatex
mainfont: roboto
---