是否可以在由rmarkdown呈现的pdf文档中包含svg图像? [英] Is it possible to include svg image in pdf document rendered by rmarkdown?

查看:212
本文介绍了是否可以在由rmarkdown呈现的pdf文档中包含svg图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将svg图像嵌入到使用rmarkdown渲染的pdf文档中.这似乎不可能,还是我错过了什么?如果不可能,是否有办法在嵌入之前先将.svg图像转换为.png?

I want an svg image to be embedded in an pdf document that I render by using rmarkdown. This doesn't seem possible, or do I miss something? If it's not possible is there a way to first convert the .svg image to a .png before embedding it?

在我的rmarkdown文件中,它看起来像这样(注意两种插入图像的方式):

In my rmarkdown file it looks like this (notice the two different kinds of inserting the image):

```{r results="asis",eval=TRUE,echo=FALSE,message=FALSE, error=FALSE, warning=FALSE, comment = NA,fig.height=10}

cat("![](svg_file.svg)")

```

![](svg_file.svg)

推荐答案

我找到了适合我的解决方案.它基于使用svg LaTeX软件包和Rmarkdown文件中的一些LaTeX代码.

I found a solution that works for me. It is based on using the svg LaTeX package and some LaTeX code inside your Rmarkdown file.

这可以通过以下主要步骤实现:

This can be achieved in these main steps:

  1. svg软件包包括到LaTeX序言中.可以使用Rmarkdown标头完成此操作.
  2. 确保在启用--shell-escape选项的情况下调用LaTeX.也可以使用Rmarkdown标头完成此操作.
  3. 在Rmarkdown文本内使用\includesvg{svg-image}包括SVG图像.请注意,不要包含文件扩展名.
  1. Include the svg package to the LaTeX preamble. This can be done using the Rmarkdown header.
  2. Make sure that LaTeX is called with the --shell-escape option enabled. This can also be done using the Rmarkdown header.
  3. Include your SVG image using \includesvg{svg-image} inside your Rmarkdown text. Be careful, do not include the file extension.

确保将pdflatex用作LaTeX引擎.其他引擎也可能会工作,但是它们将需要不同的选择...

Make sure to use pdflatex as the LaTeX engine. Other engines will probably work as well, but they will require different options...

以下是在我的机器上工作的示例,在工作目录中有一个名为svg-image.svg的SVG映像.

The following is a example that works in my machine, having a SVG image called svg-image.svg in the working directory.

---
output: 
  pdf_document:
    latex_engine: pdflatex
    pandoc_args: "--latex-engine-opt=--shell-escape"
header-includes:
   - \usepackage{svg}
---

\includesvg{svg-image}

希望对您有帮助!

这篇关于是否可以在由rmarkdown呈现的pdf文档中包含svg图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆