You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需CLI:Quarto中以Data URI嵌入SVG及持久化指定Pandoc版本

在Quarto中指定Pandoc版本以实现SVG的Data URI嵌入并持久化设置

问题背景

我用Quarto生成自包含HTML报告,需要把SVG图表以Data URI嵌入(而非内联SVG),且不想用CLI解决方案。之前在RMarkdown里通过以下配置实现了需求:

---
title: 'test'
knit: (function(input, ...) {pandoc::with_pandoc_version(version = '3.2.1', rmarkdown::render(input))})
output:
  html_document:
    self_contained: yes
---

```{r}
plot(1:10)
但换到Quarto后,当前配置仍生成内联SVG而非Data URI:
```yaml
---
title: 'test'
format: 
  html:
    embed-resources: true
    fig-format: svg
execute:
  fig-dev: 'svg'
---

```{r}
plot(1:10)
问题应该和Pandoc版本行为有关。我试过安装并激活Pandoc 3.5,但重启会话后设置就失效了:
```r
> pandoc::pandoc_install('3.5', force = TRUE)
ℹ Installing Pandoc release 3.5
✔ Pandoc version 3.5 installed.
> pandoc::pandoc_activate('3.5', rmarkdown = TRUE)
✔ Version '3.5' is now the active one.
> Sys.setenv(RSTUDIO_PANDOC = pandoc::pandoc_locate())

# 会话重启后
> rmarkdown::pandoc_version()
[1] '3.2'

解决方案

1. 在Quarto YAML中直接指定Pandoc版本

可以通过自定义knit函数,强制渲染时使用指定的Pandoc版本,修改后的YAML配置如下:

---
title: 'test'
format: 
  html:
    embed-resources: true
    fig-format: svg
execute:
  fig-dev: 'svg'
knit: (function(input, ...) {pandoc::with_pandoc_version(version = '3.5', quarto::quarto_render(input))})
---

这样每次渲染该文档时,都会自动调用Pandoc 3.5,无需手动激活版本。

2. 跨会话持久化Pandoc版本设置

要让Pandoc 3.5在每次启动R/RStudio时都生效,有两种方法:

方法一:通过.Rprofile配置

  • 找到用户主目录下的.Rprofile文件(没有的话手动创建一个)
  • 添加以下代码:
if (requireNamespace("pandoc", quietly = TRUE)) {
  pandoc::pandoc_activate('3.5', rmarkdown = TRUE)
  Sys.setenv(RSTUDIO_PANDOC = pandoc::pandoc_locate())
}

保存后重启R/RStudio,再次执行rmarkdown::pandoc_version()就能看到版本为3.5。

方法二:设置系统环境变量

直接在系统层面配置RSTUDIO_PANDOC环境变量,指向Pandoc 3.5的安装路径:

  • Windows:在系统环境变量中添加RSTUDIO_PANDOC,值为Pandoc 3.5的pandoc.exe路径(可通过pandoc::pandoc_locate('3.5')获取)
  • Mac/Linux:在.bashrc或.zshrc中添加:
export RSTUDIO_PANDOC="$(pandoc::pandoc_locate('3.5'))"

或者直接写死路径,比如:

export RSTUDIO_PANDOC="/Users/你的用户名/Library/Application Support/pandoc/bin/pandoc"

内容的提问来源于stack exchange,提问作者mat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 22:46:01