降价:!包 pdftex.def 错误;找不到在线图片?

RMarkdown: ! Package pdftex.def Error; online image not found?

我 运行 在尝试将基于 Web 的图像包含在 R Markdown PDF 文档中时遇到了问题。

最小示例

---
title: "Random"
output: pdf_document
---

![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)

编织上面的错误:

! Package pdftex.def Error: File `https://octodex.github.com/images/bannekat.pn g' not found.

但是,如果我使用以下代码,图像会显示:

---
title: "Random"
output:
  html_document: default
  html_notebook: default
---

![Benjamin Bannekat](https://octodex.github.com/images/bannekat.png)

当输出为 HTML 时,相同的代码工作正常。

如何使图像显示在 PDF 文档中?

如果您编织成 PDF,文件必须 运行 通过 LaTeX。正如突出显示的 in this question.

,LaTeX 没有显示托管在网络上的文件的内置功能

最好的解决方法是将网络图像下载到您的本地目录,然后将其指定为文件路径。

以下代码使用download.file函数下载图片,然后使用include_graphics显示。 include graphics的好处是可以指定输出文档中图片的宽度,我设置为40像素。

---
title: "Random"
output: pdf_document
---


```{r results = 'asis', out.width="40px"}
download.file(url = "https://octodex.github.com/images/bannekat.png",
          destfile = "image.png",
          mode = 'wb')
knitr::include_graphics(path = "image.png")
```

Find out more reasons why include_graphics should be used to include graphics in R Markdown documents. Check out my other answer here for more details why.