5

Knitting (in RStudio version 1.2.1335) an RMarkdown file to PDF fails when trying to create citations (for pandoc version 2.8.0.1, and R version 3.6.1). (This does not happen when knitting to HTML, for example.)

Here is a small rep. ex. in 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

Knitting this yields as final output (on my machine):

"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" output file: RepEx.knit.md

! LaTeX Error: Environment cslreferences undefined.

This seems to have started after a recent update to pandoc 2.8.0.1, and I just found on https://pandoc.org/releases.html that in 2.8 a few changes seem to have been made in the cslreferences environment (but up to now there seems to have nothing appeared on pandoc-discuss or on the respective github bug tracker).

Any ideas?

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
Gerrit
  • 51
  • 1
  • 3
  • 1
    maybe related https://stackoverflow.com/questions/59190193/rticles-templates-do-not-compile – samcarter_is_at_topanswers.xyz Dec 05 '19 at 13:14
  • Thanks! Yes, ideed, but the example there was/is no reproducible, the relation to pandoc was not yet established when I posted my question (and I was not allowed to comment due to insuffcient reputation), and there is no solution, yet. – Gerrit Dec 05 '19 at 13:22
  • 3
    This issue has been reported a few months ago (https://github.com/rstudio/rmarkdown/issues/1649) and I have fixed it. You didn't provide your `xfun::session_info('rmarkdown')`, but I guess you are not using the latest version of **rmarkdown**. [When in doubt, try to update your packages.](https://yihui.org/en/2017/05/when-in-doubt-upgrade/) BTW, since you are using RStudio, there is no need to install Pandoc separately---the current version of RStudio bundles a lower version of Pandoc, which won't create this problem. If you have to install Pandoc by yourself, you may install Pandoc < v2.8. – Yihui Xie Dec 05 '19 at 15:04
  • Thx, Yihui. Reverting to an older version of Pandoc (2.7.2) solved the problem. – Gerrit Dec 11 '19 at 17:37
  • 1
    Note that if you are using rticles then to solve this problem you have to update rticles using `remotes::install_github('rstudio/rticles')` – ben Sep 18 '20 at 15:52

2 Answers2

7

According to the release notes you linked, cslreferences was introduced in version 2.8, including a suitable definition of this environment in the pandoc template. However, Rmarkdown is using its own template (C:\Users\gcb7\Documents\R\win-library\3.6\rmarkdown\rmd\latex\default-1.17.0.2.tex in your case), which does not have this definition. This has been fixed on GitHub, c.f. https://github.com/rstudio/rmarkdown/issues/1649.

One workaround would be to copy the relevant lines to a local copy of Rmarkdown's template and specify that via the template field. Alternatively you could add

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

or

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

to the resulting tex file via header-includes or similar. Or you could use the pandoc that comes with RStudio, if you have that installed. This can be accomplished by prepending <rstudio-dir>/bin/pandoc/ to the PATH, possibly within .Renviron to make it R specific.

Everything untested, since I do not have pandoc 2.8 ...

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
5

Had the same issue when using thesisdown. Which was confusing, since the solution from Ralf (adding \newenvironment{cslreferences} ) is already included in the template.tex file form thesisdown.

After some while I figured out:

Changing \newenvironment{cslreferences}% to \newenvironment{CSLReferences}% solves the problem.

Specifically if you are also having this problem with thesisdown, you must alter the template.tex file. The section in template.tex should look like this then:

$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$ 

As also described here.

Seems like the default Pandoc template also uses \newenvironment{CSLReferences} since Version 2.11 (see Commit)

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
  • I had this issue after updating MacTex with `bookdown::pdf_document2` and a custom tex template. This answer solved the issue. – Mikko Dec 09 '21 at 14:12