Skip to content

Instantly share code, notes, and snippets.

@richarddmorey
Created January 16, 2018 18:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richarddmorey/27e74bcbf28190d150d266ae141f5117 to your computer and use it in GitHub Desktop.
Save richarddmorey/27e74bcbf28190d150d266ae141f5117 to your computer and use it in GitHub Desktop.
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