Change font of Kable in Rmarkdown pdf

2019-05-22 16:19发布

问题:

Is there a way to change the font of a kable in Rmarkdown when knitting to pdf? I can find plenty on changing size or bold etc but not on changing the actual font. I would just like something sans serif.

Example markdown doc:

---
title: "R Notebook"
output:
  pdf_document: default
---

Change the font in the table below:

```{r message=FALSE, warning=FALSE, echo=FALSE}
library(kableExtra)

mtcars %>% head() %>% kable()
```

Default table output font

回答1:

You can use the Latex fontspec library, see below. Note that you can see your available fonts and their names in the Fonts page from Settings.

---
title: "R Notebook"
output:
  pdf_document: 
    latex_engine: xelatex
header-includes:
  \usepackage{fontspec}
---

Change the font in the table below:
## using the Arial font as an example
\fontspec{Arial}

```{r message=FALSE, warning=FALSE, echo=FALSE}
library(kableExtra)

mtcars %>% head() %>% kable()