这里有一个最小化的工作示例,名为mwe.Rmd:
---
output:
pdf_document:
latex_engine: xelatex
keep_tex: TRUE
## header-includes:
## - \usepackage[space]{grffile}
---
```{r}
plot(1, 1)
```
这将在调用
rmarkdown::render("~/repos/mwe test/mwe.Rmd")
时起作用。但是,如果我调用rmarkdown::render("~/repos/mwe test/mwe.Rmd", output_dir = "~/repos/mwe test/reports/")
,它会失败并出现以下错误:/usr/local/bin/pandoc +RTS -K512m -RTS test_rmarkdown_fail.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc4f583fe0effa.tex --template /usr/local/lib/R/3.6/site-library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --pdf-engine xelatex --variable graphics=yes --variable 'geometry:margin=1in' --variable 'compact-title:yes'
! Missing $ inserted.
<inserted text>
$
l.132 ...files/figure-latex/unnamed-chunk-1-1.pdf}
当我检查 .tex 文件时,因为包含空格(mwe 和 test 之间),它在包含绘图的行上失败了。
\includegraphics{/Users/savey/repos/mwe test/reports/test_rmarkdown_fail_files/figure-latex/unnamed-chunk-1-1.pdf}
当未指定 output_dir
时,这只是一个相对路径,所以可以正常工作。
当 latex_engine
设置为 pdftex
时,带有空格的路径可以正常工作,但对于 xelatex
则不行。我尝试向 LaTeX 包 grffile 添加 space
选项,但出现了选项冲突的错误(与默认的 tex 模板冲突)。我还尝试修改模板以在标头中添加此内容,但没有成功。
我如何使用 xelatex 运行 rmarkdown 并指定包含空格的输出目录?
有什么选项可以强制 pandoc 在双引号内包装图像路径吗?
在你建议我删除路径中的空格,因为它们不属于那里之前,我想说,这是一个同步的文件夹,我无法重命名它(感谢 OneDrive)。
rmarkdown
内请求添加一些“魔法”中间步骤,就没有容易的解决方法了,正如yihui在这里建议的那样:https://github.com/rstudio/rmarkdown/issues/1285#issuecomment-373141838 - Stefan Aveypdflatex
时,output_dir
中的空格出现了问题。也许使用临时目录的解决方法也可以行得通。或者使用相对目录,但我不太喜欢这种方法。 - phargart