【问题标题】:Pandoc's environment cslreferences undefined when knitting RMarkdown to PDF in RStudio在 RStudio 中将 RMarkdown 编织为 PDF 时,Pandoc 的环境 cslreferences 未定义
【发布时间】:2020-03-30 07:19:33
【问题描述】:

在尝试创建引文时(针对 pandoc 版本 2.8.0.1 和 R 版本 3.6.1),将 RMarkdown 文件编织到 PDF 失败(在 RStudio 版本 1.2.1335 中)。 (例如,编织成 HTML 时不会发生这种情况。)

这是一个小代表。前任。在 RMarkdown 中:

---
title: "Rep. Ex. for 'LaTeX Error: Environment cslreferences undefined'"
output:
  pdf_document: default
bibliography: report.bib
---

```{r generate-bibtex-file, include=FALSE}
knitr::write_bib(file = "report.bib", prefix = "")
```

# Used R version

R 3.6.1 [@base]

# References

将其编织为最终输出(在我的机器上):

"C:/PROGRA~1/Pandoc/pandoc" +RTS -K512m -RTS RepEx.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output RepEx.tex --template "C: \Users\gcb7\Documents\R\win-library\3.6\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --lua -filter "C:/Users/gcb7/Documents/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua" --lua-filter "C:/Users/gcb7/Documents/R/win-library /3.6/rmarkdown/rmd/lua/latex-div.lua" --variable "geometry:margin=1in" --variable "compact-title:yes" --filter "C:/PROGRA~1/Pandoc/pandoc- citeproc.exe" 输出文件:RepEx.knit.md

! LaTeX 错误:环境 cslreferences 未定义。

这似乎是在最近更新 pandoc 2.8.0.1 之后开始的,我刚刚在https://pandoc.org/releases.html 上发现,在 2.8 中似乎已经在 cslreferences 环境中进行了一些更改(但到目前为止似乎有pandoc-discuss 或相应的 github 错误跟踪器上没有出现任何内容)。

有什么想法吗?

【问题讨论】:

  • 谢谢!是的,ideed,但是这个例子是/不可重现的,当我发布我的问题时,与 pandoc 的关系尚未建立(由于声誉不足,我不允许发表评论),目前还没有解决方案。
  • 这个问题已经在几个月前被报告了(github.com/rstudio/rmarkdown/issues/1649),我已经修复了它。你没有提供你的xfun::session_info('rmarkdown'),但我猜你没有使用最新版本的 rmarkdownWhen in doubt, try to update your packages. BTW,由于您使用的是RStudio,因此无需单独安装Pandoc--当前版本的RStudio捆绑了较低版本的Pandoc,不会产生此问题。如果你必须自己安装 Pandoc,你可以安装 Pandoc
  • 谢谢,一辉。恢复到旧版本的 Pandoc (2.7.2) 解决了这个问题。
  • 请注意,如果您使用的是 rticles,那么要解决此问题,您必须使用 remotes::install_github('rstudio/rticles') 更新 rticles

标签: r latex r-markdown knitr pandoc


【解决方案1】:

根据您链接的发行说明,cslreferences 是在 2.8 版中引入的,包括在 pandoc 模板中对此环境的合适定义。但是,Rmarkdown 使用自己的模板(在您的情况下为C:\Users\gcb7\Documents\R\win-library\3.6\rmarkdown\rmd\latex\default-1.17.0.2.tex),它没有此定义。这已在 GitHub 上修复,c.f. https://github.com/rstudio/rmarkdown/issues/1649.

一种解决方法是将relevant lines 复制到Rmarkdown 模板的本地副本并通过template 字段指定。或者你可以添加

\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newenvironment{cslreferences}%
  {\setlength{\parindent}{0pt}%
  \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces}%
  {\par}

\newenvironment{cslreferences}%
  {}%
  {\par}

通过header-includes 或类似方法将生成的tex 文件。或者你可以使用 RStudio 附带的pandoc,如果你已经安装了。这可以通过在PATH 前添加<rstudio-dir>/bin/pandoc/ 来完成,可能在.Renviron 中,使其特定于R。

一切都未经测试,因为我没有 pandoc 2.8 ...

【讨论】:

    【解决方案2】:

    使用thesisdown 时遇到同样的问题。 这很令人困惑,因为 Ralf 的解决方案(添加 \newenvironment{cslreferences} )已经包含在 thesisdown 的 template.tex 文件中。

    一段时间后,我想通了:

    改变 \newenvironment{cslreferences}%\newenvironment{CSLReferences}% 解决问题。

    特别是如果您也遇到了 thesisdown 问题,您必须更改 template.tex 文件。 template.tex 中的部分应如下所示:

    $if(csl-refs)$
    \newlength{\cslhangindent}
    \setlength{\cslhangindent}{1.5em}
    \newenvironment{CSLReferences}%
      {$if(csl-hanging-indent)$\setlength{\parindent}{0pt}%
      \everypar{\setlength{\hangindent}{\cslhangindent}}\ignorespaces$endif$}%
      {\par}
    $endif$ 
    

    here 所述。

    似乎默认的 Pandoc 模板也使用 \newenvironment{CSLReferences} 自版本 2.11 (see Commit)

    【讨论】:

    • 在使用 bookdown::pdf_document2 和自定义 tex 模板更新 MacTex 后,我遇到了这个问题。这个答案解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2017-02-26
    • 2019-03-11
    • 2014-08-10
    • 2023-04-07
    • 2016-07-11
    相关资源
    最近更新 更多