【问题标题】:RMarkdown: ! Package pdftex.def Error; online image not found?降价:!包 pdftex.def 错误;网上图片没找到?
【发布时间】:2018-07-11 14:11:22
【问题描述】:

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

最小示例

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

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

编织上述导致错误:

!包 pdftex.def 错误:文件 `https://octodex.github.com/images/bannekat.pn g' 未找到。

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

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

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

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

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

【问题讨论】:

  • 希望答案有所帮助。只是检查一下:为什么要在代码块中指定图像路径,然后将其包装在cat() 中?您可以只在外面显示代码,因为我已经编辑了问题。不过,根据我的回答,我仍然建议使用include_graphics

标签: r r-markdown knitr


【解决方案1】:

如果您要编织成 PDF,文件必须通过 LaTeX 运行。 LaTeX 没有显示托管在网络上的文件的内置功能,如突出显示的in this question

解决此问题的最佳方法是将 Web 图像下载到本地目录,然后将其指定为文件路径。

以下代码使用download.file 函数下载图像,然后使用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")
```

了解为什么应该使用 include_graphics 在 R Markdown 文档中包含图形的更多原因。查看my other answer here 了解更多详情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-20
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    相关资源
    最近更新 更多