Skip to content

Instantly share code, notes, and snippets.

@carlbfrederick
Created November 12, 2018 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlbfrederick/2b9af78e0153c35b21195fbf0812d587 to your computer and use it in GitHub Desktop.
Save carlbfrederick/2b9af78e0153c35b21195fbf0812d587 to your computer and use it in GitHub Desktop.
Windows Fonts in Rmd
---
title: "Sample Lato .Rmd"
author: "Carl Frederick"
date: "11/12/2018"
mainfont: Lato
output:
pdf_document:
latex_engine: xelatex
---
```{r, include = FALSE, echo = FALSE}
library(extrafont)
loadfonts(device = "win")
```
# Intro
This is Lato, cheers!
Apparently, the font doesn't carry over to tables made via `knitr::kable()`.
I have found this all works better with [TinyTeX](https://yihui.name/tinytex/)
rather than [MiKTeX](https://miktex.org/). To install TinyTeX:
```{r, eval = FALSE, echo = TRUE}
install.packages('tinytex')
tinytex::install_tinytex()
```
# Prerequisites
All of this is in the extrafont package readme
<https://cran.r-project.org/web/packages/extrafont/README.html>, but to
summarize:
In order to use Lato, you need to use the `extrafont` package:
```{r, eval = FALSE, echo = TRUE}
install.packages(extrafont)
```
The first time you load extrafont you need to import the fonts on your
computer:
```{r, eval = FALSE, echo = TRUE}
library(extrafont)
font_import()
```
After the first time, you simply need to load the imported fonts via:
```{r, eval = FALSE, echo = TRUE}
loadfonts(device = "win")
```
For each file you create you need to embed the fonts (requires GhostScript)
using `embed_fonts()`. See the package readme for details on this.
# Specifying which font to use
In the YAML header (between the --- --- lines at the top), specify
`mainfont: Lato` and set the latex_engine to xelatex as in:
```
---
title: "title"
author: "author"
date: "MM/DD/YYYY"
mainfont: Lato
output:
pdf_document:
latex_engine: xelatex
---
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment