如何使用包含 SVG 图像的 bookdown 制作 PDF
How to make a PDF using bookdown including SVG images
我有一些包含以下代码的 R markdown:
```{r huff51, fig.show='hold', fig.cap='Design decisions connecting research purpose and outcomes [@huff_2009_designingresearchpublication p. 86].', echo=FALSE}
knitr::include_graphics('images/Huff-2009-fig5.1.svg')
```
当使用 bookdown 生成 HTML 输出时,一切都按预期工作。
当使用 bookdown 生成 PDF 输出时,我收到一条错误消息 ! LaTeX Error: Unknown graphics extension: .svg.
这是可以理解的,因为 knitr 使用 Latex 的 \includegraphics{images/Huff-2009-fig5.1.svg}
来包含图像。所以,这本身不是错误。
有没有更好的方法来包含 SVG 图像,这样我就不需要将其预处理成 PDF 或 PNG 等格式?
您可以创建一个辅助函数来将 SVG 转换为 PDF。例如,如果您安装了系统包 rsvg-convert
,则可以使用此功能来包含 SVG 图形:
include_svg = function(path) {
if (knitr::is_latex_output()) {
output = xfun::with_ext(path, 'pdf')
# you can compare the timestamp of pdf against svg to avoid conversion if necessary
system2('rsvg-convert', c('-f', 'pdf', '-a', '-o', shQuote(c(output, path))))
} else {
output = path
}
knitr::include_graphics(output)
}
您也可以考虑使用 magick(基于 ImageMagick)之类的 R 包将 SVG 转换为 PDF。
bookdown
,我真的不喜欢我的网站上有 PDF 文件。所以我使用这个代码:
if (knitr::is_html_output()) {
structure("images/01-02.svg", class = c("knit_image_paths", "knit_asis"))
} else {
# do something for PDF, e.g. an actual PDF file if you have one,
# or even use Yihui's code in the other answer
knitr::include_graphics("images/01-02.pdf")
}
它使用网站的 SVG 文件(即,HTML 输出)。
它非常适合生成所有内容:网站、gitbook、pdfbook 和 epub。
要防止将此代码添加到 bookdown
项目中的每个块,请将其添加到 index.Rmd
:
insert_graphic <- function(path, ...) {
if (knitr::is_html_output() && grepl("[.]svg$", basename(path), ignore.case = TRUE)) {
structure(path, class = c("knit_image_paths", "knit_asis"))
} else {
knitr::include_graphics(path, ...)
}
}
我有一些包含以下代码的 R markdown:
```{r huff51, fig.show='hold', fig.cap='Design decisions connecting research purpose and outcomes [@huff_2009_designingresearchpublication p. 86].', echo=FALSE}
knitr::include_graphics('images/Huff-2009-fig5.1.svg')
```
当使用 bookdown 生成 HTML 输出时,一切都按预期工作。
当使用 bookdown 生成 PDF 输出时,我收到一条错误消息 ! LaTeX Error: Unknown graphics extension: .svg.
这是可以理解的,因为 knitr 使用 Latex 的 \includegraphics{images/Huff-2009-fig5.1.svg}
来包含图像。所以,这本身不是错误。
有没有更好的方法来包含 SVG 图像,这样我就不需要将其预处理成 PDF 或 PNG 等格式?
您可以创建一个辅助函数来将 SVG 转换为 PDF。例如,如果您安装了系统包 rsvg-convert
,则可以使用此功能来包含 SVG 图形:
include_svg = function(path) {
if (knitr::is_latex_output()) {
output = xfun::with_ext(path, 'pdf')
# you can compare the timestamp of pdf against svg to avoid conversion if necessary
system2('rsvg-convert', c('-f', 'pdf', '-a', '-o', shQuote(c(output, path))))
} else {
output = path
}
knitr::include_graphics(output)
}
您也可以考虑使用 magick(基于 ImageMagick)之类的 R 包将 SVG 转换为 PDF。
bookdown
,我真的不喜欢我的网站上有 PDF 文件。所以我使用这个代码:
if (knitr::is_html_output()) {
structure("images/01-02.svg", class = c("knit_image_paths", "knit_asis"))
} else {
# do something for PDF, e.g. an actual PDF file if you have one,
# or even use Yihui's code in the other answer
knitr::include_graphics("images/01-02.pdf")
}
它使用网站的 SVG 文件(即,HTML 输出)。
它非常适合生成所有内容:网站、gitbook、pdfbook 和 epub。
要防止将此代码添加到 bookdown
项目中的每个块,请将其添加到 index.Rmd
:
insert_graphic <- function(path, ...) {
if (knitr::is_html_output() && grepl("[.]svg$", basename(path), ignore.case = TRUE)) {
structure(path, class = c("knit_image_paths", "knit_asis"))
} else {
knitr::include_graphics(path, ...)
}
}