Beamer:使用 tikzDevice 时在绘图中使用文档主字体

Beamer: use the document main font in plots when using tikzDevice

我正在尝试创建一个使用 tikz 设备作为绘图输出的 Rmd 文档模板。通常,在Rweave(sweave)文档中做同样的事情时,tikz图形中的字体与文档中的字体相同,因为tikz接管了设置字体的文档序言

在 Rmarkdown 中,我只能在设置块选项时实现这一点 external = F。这样,tikz grpahics 就不会被预编译。

但我想使用这种外部性,并且在最终的 pdf 输出中仍然具有相同的图形字体。

这是一个使用 tikz 的简单 beamer 演示文稿。如何使绘图具有相同的无字体?

---
title: "TikZ"
author: "Martin Schmelzer"
date: "10/9/2017"
output: beamer_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, dev = "tikz")
```

## Slide with Plot

```{r pressure}
plot(pressure)
```

你需要告诉tikzDevice你正在使用beamerclass,否则它不知道并且会假设一个正常的\documentclass{article}。如果查看帮助页面 ?tikzDevice::tikz,您可能会看到文档 class 可以通过选项 tikzDocumentDeclaration 声明,例如

options(tikzDocumentDeclaration = '\documentclass{beamer}')

完整示例(我减小了绘图大小,以便您可以在绘图中更清楚地看到字体样式):

---
title: "TikZ"
author: "Martin Schmelzer"
date: "10/9/2017"
output: beamer_presentation
---

```{r setup, include=FALSE}
options(tikzDocumentDeclaration = '\documentclass{beamer}')
knitr::opts_chunk$set(echo = FALSE, dev = "tikz")
```

## Slide with Plot

```{r pressure, fig.width=5, fig.height=4}
plot(pressure)
```