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

rmarkdown::render()未传递pandoc的-shell-escape选项至pdflatex

解决Rmarkdown渲染时丢失-shell-escape选项的问题

我明白你遇到的问题了——在YAML里加了pandoc_args: "--pdf-engine-opt=-shell-escape",但rmarkdown::render()在生成.tex文件后调用pdflatex时,这个选项没被传递过去。这是因为你给pandoc的参数只在pandoc直接调用PDF引擎时生效,而当流程走到单独调用pdflatex编译.tex文件这一步时,这个参数不会自动继承。

这里有两个靠谱的解决方案,你可以根据自己的需求选择:

方案1:让Pandoc直接生成PDF(推荐)

确保你的YAML头配置正确,把pandoc_args嵌套在pdf_document下面,这样pandoc会直接调用pdflatex并带上-shell-escape选项,跳过后续单独编译.tex的步骤:

---
title: "你的文档标题"
output:
  pdf_document:
    pandoc_args: ["--pdf-engine-opt=-shell-escape"]
    # 如果需要保留生成的.tex文件,加上下面这行
    # keep_tex: true
---

之后直接调用rmarkdown::render("你的文件.Rmd")就行,这时候你看控制台输出的pdflatex命令,应该能看到-shell-escape已经带上了。

方案2:给pdflatex单独指定-shell-escape选项

如果你的工作流必须先生成.tex文件再编译,那可以直接告诉rmarkdown在调用pdflatex时加上这个选项。在YAML里配置latex_options即可:

---
title: "你的文档标题"
output:
  pdf_document:
    keep_tex: true
    latex_engine: pdflatex
    latex_options: ["shell-escape"]
---

或者你也可以在调用render函数时临时传递这个参数:

rmarkdown::render("你的文件.Rmd", 
                  output_options = list(latex_options = "shell-escape"))

这样当rmarkdown调用pdflatex编译.tex文件时,命令会变成pdflatex -shell-escape mwe.tex,选项就不会丢失了。

你可以通过查看render过程中的控制台输出来验证,确认pdflatex的调用命令里确实包含了-shell-escape。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:32:34