grid在控制台中运行时查找字体,但在呈现Rmd时不查找

时间:2018-06-15 14:45:46

标签: r graphics r-markdown

当我在控制台中从下面的最小Rmarkdown运行块时,会显示正确的字体。但是,当我尝试将Rmarkdown文档编译为pdf时,我收到错误:font family 'Pacifico' not found in PostScript font database。该字体在我的Ubuntu机器上以ttf字体的形式提供。您可以在https://www.fontsquirrel.com/fonts/pacifico

下载该字体
---
output: pdf_document
---

```{r fig.cap = "Plot with ttf font"}
library(grid)
grid.newpage()
grid.text("Some text", gp = gpar(fontfamily = "Pacifico"))
```

当我使用“Courier New”等标准字体

时,它会起作用

1 个答案:

答案 0 :(得分:1)

使用pdf()设备时,我可以使用Rmarkdown和控制台重现错误:

library(grid)
pdf("...")
grid.newpage()
grid.text("Some text", gp = gpar(fontfamily = "Pacifico"))
# -> font family 'Pacifico' not found in PostScript font database
dev.off()

我找到的最简单的解决方案是使用cairo_pdf()设备:

---
output: pdf_document
---

```{r fig.cap = "Plot with ttf font", dev = 'cairo_pdf'}
library(grid)
grid.newpage()
grid.text("Some text", gp = gpar(fontfamily = "Pacifico"))
```

或者,您可以使用extrafont包。你首先要打电话

extrafont::font_import()

在控制台中。之后你可以使用

---
output: pdf_document
---

```{r fig.cap = "Plot with ttf font", dev = 'pdf'}
library(grid)
extrafont::loadfonts(quiet = TRUE)
grid.newpage()
grid.text("Some text", gp = gpar(fontfamily = "Pacifico"))
```