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?
3.3. PDF document. To create a PDF document from R Markdown, you specify the pdf_document output format in the YAML metadata: --- title: "Habits" author: John Doe date: March 22, 2005 output: pdf_document ---. Within R Markdown documents that generate PDF output, you can use raw LaTeX, and even define LaTeX macros.
I specifically wanted to be able to knit an RMarkdown to PDF using the Source Sans Pro Google font, but these instructions should work for any Google font. To embed custom fonts in PDFs, you can use the Cairo graphics library.
Markdown is a simple formatting >syntax for authoring HTML, PDF, and MS Word documents. For >more details on using R Markdown see When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
Within R Markdown documents that generate PDF output, you can use raw LaTeX, and even define LaTeX macros. See Pandoc’s documentation on the raw_tex extension for details. Note that PDF output (including Beamer slides) requires an installation of LaTeX (see Chapter 1). 3.3.1 Table of contents
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
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
---
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With