Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandoc's environment cslreferences undefined when knitting RMarkdown to PDF in RStudio

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?

like image 659
Gerrit Avatar asked Jan 25 '23 13:01

Gerrit


2 Answers

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

like image 113
Ralf Stubner Avatar answered Jan 28 '23 01:01

Ralf Stubner


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)

like image 21
Steffen Moritz Avatar answered Jan 28 '23 01:01

Steffen Moritz