据我所知,从互联网插入图像到 LaTeX 是不可能的。您必须先下载图像,然后将其包含在本地。
使用 R bookdown,我是这样做的:
download.file('https://bookdown.org/yihui/bookdown/images/cover.jpg','cover.jpg', mode = 'wb')
knitr::include_graphics('cover.jpg')
它对于 gitbook 和 pdf 输出都非常有效。但我认为这并不聪明。 pdf需要先下载文件,而gitbook输出则不需要。图像可以通过以下方式直接包含到 gitbook 输出中:
knitr::include_graphics('https://bookdown.org/yihui/bookdown/images/cover.jpg')
当我想要同时输出 pdf 和 gitbook 时,有什么方法可以切换它们吗?我的意思是,当我编译 .Rmd 文件时,我希望获得一个插入了图像的 .pdf 文件,以及一个插入了从网址插入的原始图像的 gitbook 文件。
您可以在
knitr::pandoc_to()
中查看当前的输出格式。如果是 'html'
,则使用图像的 URL,否则使用文件路径,例如
cover_url = 'https://bookdown.org/yihui/bookdown/images/cover.jpg'
if (!file.exists(cover_file <- 'cover.jpg'))
xfun::download_file(cover_url, cover_file)
knitr::include_graphics(if (knitr::pandoc_to('html')) cover_url else cover_file)