我有一些R标记,其中包括以下代码:
```{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.
这是可以理解的,因为include使用的是Latex的\includegraphics{images/Huff-2009-fig5.1.svg}来包含图像。所以,它本身并不是一个bug。
有没有更好的方法来包含SVG图像,这样我就不需要将其预处理为PDF或PNG格式?
发布于 2019-05-08 23:54:56
您可以创建一个辅助函数来将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)
}您还可以考虑使用R包将转换为,如SVG (它基于ImageMagick)。
发布于 2021-09-16 12:51:46
对于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, ...)
}
}https://stackoverflow.com/questions/50165404
复制相似问题