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?

5
  • 1
  • 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 5, 2019 at 13:22
  • 3
    This issue has been reported a few months ago (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. 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 5, 2019 at 15:04
  • Thx, Yihui. Reverting to an older version of Pandoc (2.7.2) solved the problem.
    – Gerrit
    Dec 11, 2019 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, 2020 at 15:52

2 Answers 2

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

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

3

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)

1
  • I had this issue after updating MacTex with bookdown::pdf_document2 and a custom tex template. This answer solved the issue.
    – Mikko
    Dec 9, 2021 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.