Rmarkdown在输出目录包含空格时生成PDF文件出错。

4

这里有一个最小化的工作示例,名为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 文件时,因为包含空格(mwetest 之间),它在包含绘图的行上失败了。

\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)。


4
这是一个已知问题:https://github.com/rstudio/rmarkdown/issues/1285 - r2evans
1
啊,我明白了。所以除了在rmarkdown内请求添加一些“魔法”中间步骤,就没有容易的解决方法了,正如yihui在这里建议的那样:https://github.com/rstudio/rmarkdown/issues/1285#issuecomment-373141838 - Stefan Avey
我在使用pdflatex时,output_dir中的空格出现了问题。也许使用临时目录的解决方法也可以行得通。或者使用相对目录,但我不太喜欢这种方法。 - phargart
1个回答

3

上述代码似乎确实可以使用 pdflatex 工作。

对于 xelatex,一个解决方法是使用临时目录:

origin <- "./repos/mwe test/mwe.Rmd"
destination <- "./repos/mwe test/reports"

render <- function(origin,destination) {
  tmpdir <- tempdir()
  on.exit(unlink(tmpdir))
  rmarkdown::render(origin, output_dir = tmpdir)
  suppressWarnings(dir.create(destination))
  file.copy(file.path(tmpdir,paste0(basename(tools::file_path_sans_ext(origin)),'.pdf')),destination, overwrite = T)
}

render(origin,destination)

#processing file: mwe.Rmd
#  |...................................                                   |  50%
#  ordinary text without R code
#
#  |......................................................................| 100%
#label: unnamed-chunk-1
#
#output file: mwe.knit.md
#
#/usr/bin/pandoc +RTS -K512m -RTS mwe.utf8.md --to latex --from  markdown+autolink_bare_uris+tex_math_single_backslash --output /tmp/RtmpnHeDOy/mwe.tex --self-contained --highlight-style tango --pdf-engine xelatex --variable graphics --lua-filter /usr/local/lib/R/site-library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /usr/local/lib/R/site-library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in'

#Output created: /tmp/RtmpnHeDOy/mwe.pdf
#[1] TRUE

dir("./repos/mwe test/reports")
[1] "mwe.pdf"



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接