Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstration of how to use other fonts in an Rmarkdown document
---
output:
html_document:
dev: svg
fig_width: 5
fig_height: 4
---
<!-- The output of this file can be found at
http://rpubs.com/richarddmorey/fonts
-->
```{r setup, include=FALSE}
library(showtext)
## Needed for the location of Candara on my system
font_paths("/System/Library/Fonts")
## Add two fonts, Avenir and Candara
font_add("avenir", regular = "Avenir.ttc", bold = "Avenir.ttc")
font_add("candara",
regular = "Candara.ttf",
bold = "Candara Bold.ttf",
italic = "Candara Italic.ttf",
bolditalic = "Candara Bold Italic.ttf"
)
## Add hooks so that I can choose fonts in the chunk options
knitr::knit_hooks$set(
avenir = function(before, options, envir)
if (before) par(family = "avenir"),
candara = function(before, options, envir)
if (before) par(family = "candara")
)
## set chunk options to use showtext
knitr::opts_chunk$set(fig.showtext=TRUE)
## Could also do
# knitr::opts_chunk$set(fig.showtext=TRUE, candara = TRUE)
## to always use candara
```
The chunk below will use avenir.
```{r echo=FALSE, avenir=TRUE}
x = mvtnorm::rmvnorm(1000, c(0,0), diag(2))
plot(x,
las = 1, ylab = "Dependent variable",
xlab = "Independent variable",
pch = 19, col = rgb(0,0,1,.1) )
```
The chunk below will use candara.
```{r echo=FALSE, candara=TRUE}
x = mvtnorm::rmvnorm(1000, c(0,0), diag(2))
plot(x,
las = 1, ylab = "Dependent variable",
xlab = "Independent variable",
pch = 19, col = rgb(0,0,1,.1) )
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.