LearnR Rmd knit to pdf failed to compile after update of MikTex - r

After I updated MikTex to the latest version, my Rmd files don't compile anymore. I installed tinytex, reinstalled miktex, R and RStudio several times. I use some newcommands like \C, \G etc. in a tex document which I include in my Rmd file. I use windows 10, the latest versions of R, RStudio and Miktex.
I asked a colleague (who had no compiling issues) to update his MikTex as well. After updating his MikTex, he had the same issue.
Here is the Rmd code:
---
documentclass: scrartcl
fontsize: 11pt
output:
pdf_document:
includes:
in_header: commands.tex
---
This is an example:
\begin{itemize}
\item hello
\item world
\end{itemize}
And here is the tex code:
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{icomma}
\usepackage[right,official]{eurosym}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage[shortlabels]{enumitem}
\usepackage{setspace}
\singlespacing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Seitenformat und Layoutparameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\hbadness=1000
\tolerance=10000
\voffset-2.80cm
\hoffset-2.60cm
\topmargin1.50cm
\headheight0.65cm
\headsep1.0cm
\topskip0cm
\textheight24.00cm
\footskip1.5cm
\footnotesep11pt
\evensidemargin2.50cm
\oddsidemargin2.50cm
\textwidth16cm
%\parindent0cm
%\parskip1.5ex plus0.5ex minus 0.5ex
% simple letters
\newcommand{\A}{{\mathbb A}}
\newcommand{\B}{{\mathbb B}}
\renewcommand{\C}{{\mathbb C}}
\newcommand{\D}{{\mathbb D}}
\newcommand{\E}{{\mathbb E}}
And I get:
processing file: example1.Rmd
|......................................................................| 100%
ordinary text without R code
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS example1.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output example1.tex --lua-filter "C:\Users\saski\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\saski\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --include-in-header commands.tex --variable graphics
output file: example1.knit.md
! LaTeX Error: Command \C undefined.
Fehler: LaTeX failed to compile example1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See example1.log for more info.
Ausführung angehalten

The best solution would be to avoid one letter macro names
your code no longer works because \C is now only defined by hyperref under certain circumstances, see https://tex.stackexchange.com/questions/582625/command-c-already-defined-and-the-hyperref-package/582630#582630 for more information
If you use \newcommand instead of \renewcommand you can see that the error vanishes
% simple letters
\newcommand{\A}{{\mathbb A}}
\newcommand{\B}{{\mathbb B}}
\newcommand{\C}{{\mathbb C}}
\newcommand{\D}{{\mathbb D}}
\newcommand{\E}{{\mathbb E}}
(but you should still not do that, use some other macro names instead if the one letter names)
Conclusion: don't use one letter macro names

Related

rticles templates do not compile [duplicate]

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

Latex shell-escape options in YAML header don't use

I'm writing a report on Rmarkdovn but when you try to compile - pandoc_args options ignored when compile pdf.
I find this question:
Is it possible to include svg image in pdf document rendered by rmarkdown?
But it does not work because --latex-engine-opt replaced by --pdf-engine-opt. I replaced, but also did not work.
Compilation command:
Rscript -e "rmarkdown::render('test.rmd')"
test.rmd
---
title: Test
papersize: a4paper
output:
pdf_document:
latex_engine: pdflatex
pandoc_args: [
--pdf-engine-opt, -shell-escape,
--pdf-engine-opt, -interaction=nonstopmode]
header-includes:
- \usepackage{minted}
---
Output:
processing file: test.rmd
|................................ | 50%
ordinary text without R code
|.................................................................| 100%
label: unnamed-chunk-1 (with options)
List of 1
$ engine: chr "python"
output file: test.knit.md
/usr/bin/pandoc +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.tex --template /home/renesat/R/x86_64-pc-linux-gnu-library/3.5/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --pdf-engine-opt --shell-escape --pdf-engine-opt -interaction=nonstopmode --variable 'geometry:margin=1in' --variable 'compact-title:yes'
! Package minted Error: You must invoke LaTeX with the -shell-escape flag.
Error: Failed to compile test.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See test.log for more info.
Execution halted
I can use knitr -> pandoc -> xelatex, but it would be more convenient to use YAML header.
R Markdown documents are compiled to PDF via tinytex::latexmk(). To enable the -shell-escape option for your LaTeX engine, you may set the global option tinytex.engine_args, e.g., add this code chunk to your document:
```{r, include=FALSE}
options(tinytex.engine_args = '-shell-escape')
```
See the help page ?tinytex::latexmk for more information.
The LaTeX engine is called by tinytex::latexmk() instead of Pandoc, so the Pandoc argument --pdf-engine-opt won't work.

Specifying multiple simultaneous output formats in knitr (new)

Can I write a YAML header to produce multiple output formats for an R Markdown file using knitr? I could not reproduce the functionality described in the answer for the original question with this title.
This markdown file:
---
title: "Multiple output formats"
output:
pdf_document: default
html_document:
keep_md: yes
---
# This document should be rendered as an html file and as a pdf file
produces a pdf file but no HTML file.
And this file:
---
title: "Multiple output formats"
output:
html_document:
keep_md: yes
pdf_document: default
---
# This document should be rendered as an html file and as a pdf file
produces an HTML file (and an md file) but no pdf file.
This latter example was the solution given to the original question. I have tried knitting with Shift-Ctrl-K and with the Knit button in RStudio, as well as calling rmarkdown::render, but only a single output format is created, regardless of the method I use to generate the output file.
Possibly related, but I could not identify solutions:
How do I produce R package vignettes in multiple formats?
Render all vignette formats #1051
knitr::pandoc can't create pdf and tex files with a single config #769
Multiple formats for pandoc #547
An allusion to multiple output format support in a three year old RStudio blog post
Using R version 3.3.1 (2016-06-21), knitr 1.14, Rmarkdown 1.3
I actually briefly mentioned in Render all vignette formats #1051 and you missed it:
rmarkdown::render('your.Rmd', output_format = 'all')
It is documented on the help page ?rmarkdown::render.
Notwithstanding Yihui Xie's authoritative answer, and with due respect to the author of a great package, there are many cases in which output_format = 'all' is sub-optimal.
One of the issues that this solution raises is that the R script is re-processed from scratch for each format. Proof:
rmarkdown::render("new.Rmd", output_format = c("html_document", "pdf_document"))
processing file: new.spin.Rmd
|....................... | 33%
ordinary text without R code
|............................................... | 67%
label: unnamed-chunk-1
|......................................................................| 100%
ordinary text without R code
output file: new.knit.md
"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.html --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\fabrn\AppData\Local\Temp\RtmpW6Vban\rmarkdown-str3490247b1f1e.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
Output created: new.html
processing file: new.spin.Rmd
|....................... | 33%
ordinary text without R code
|............................................... | 67%
label: unnamed-chunk-1
|......................................................................| 100%
ordinary text without R code
output file: new.knit.md
"C:/Users/fabrn/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS new.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output new.tex --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\fabrn\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in"
This really is an issue when it comes to processing big data.
In real-world examples, I usually use latex output as a single rmarkdown::render output, then reprocess the .tex files using pandoc or similar tools (like prince for pdf). So my workflow is like:
rmarkdown::render('new.R', output_format = 'latex_document')
lapply(c("html", "pdf", ...),
function(form) rmarkdown::pandoc_convert("new.tex", output=paste0("new.", form)))
The bottom line is: all depends on your data. If small, output_format='all' is straightforward.
If big, you are better off with a common-ground format (latex is a good choice but html may be better in some cases) as an input to conversion tools.

rmarkdown: how to use multiple bibliographies for a document

[My environment: Win 7 Pro / R 3.2.1 / knitr_1.12.3 / R Studio Version 0.99.892]
I am trying to write an article in .Rmd format using R Studio, Knit -> PDF, and I've been following http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html for details of how to get pandoc and pandoc-citeproc to produce a References section and citations
in the text.
My BibTeX references are in several different .bib files, which, when using
LaTeX in .Rnw files, are all found in my local texmf tree via
\bibliography{statistics, graphics}
I can't seem to make this work with pandoc-citeproc. My YAML header
has the following, that works for one .bib file, as long as it is
in the same directory as the source .Rmd file:
---
title: "Notes on Testing Equality of Covariance Matrices"
author: "Michael Friendly"
date: '`r format(Sys.time(), "%B %d, %Y")`'
output:
pdf_document:
fig_caption: yes
keep_tex: yes
number_sections: yes
csl: apa.csl
bibliography: statistics.bib
---
Following advice in the link given above, I tried:
bibliography: [statistics.bib,graphics.bib]
this gives:
pandoc-citeproc.exe: "stdin" (line 50, column 12):
unexpected ":"
expecting letter, digit, white space or "="
pandoc.exe: Error running filter pandoc-citeproc
Filter returned error status 1
[Edit: For completeness, I show the generated pandoc command, where it looks like both .bib files are passed correctly]:
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS EqCov.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output EqCov.pdf --template "C:\R\R-3.2.1\library\rmarkdown\rmd\latex\default-1.15.2.tex" --number-sections --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" --bibliography statistics.bib --bibliography graphics.bib --filter pandoc-citeproc
output file: EqCov.knit.md
So do all the following forms:
bibliography: ["statistics.bib","graphics.bib"]
bibliography:
- "statistics.bib"
- "graphics.bib"
bibliography:
- statistics.bib
- graphics.bib
Ideally, I'd like to be able to use one of the following forms, and not have to copy the .bib files to the document directory.
bibliography: ["C:/Dropbox/localtexmf/bibtex/bib/statistics.bib","C:/Dropbox/localtexmf/bibtex/bib/graphics.bib"]
bibliography:
- "C:/Dropbox/localtexmf/bibtex/bib/statistics.bib"
- "C:/Dropbox/localtexmf/bibtex/bib/graphics.bib"
For the record: I raised this as an issue for pandoc-citeproc at https://github.com/jgm/pandoc-citeproc/issues/220
It turns out that there were problems in how pandoc-citeproc handles some characters in #{string={}} and non-ASCII characters in .bib files, so what I was trying now works, with hard-coded pathnames, in all the forms I tried.
To make it work more like processing of .Rnw files via Latex/bibtex it would be nice to be able to use something like
bibliography:
- `r system(kpsewhich statistics.bib)`
- `r system(kpsewhich graphics.bib)`
Those commands do find the right files in an R session, but not from a YAML header.

Garbled or missing LaTeX math formulas in RMarkdown->PDF knitr-converted document

I'm using Rstudio to write an RMarkdown document that I turn into a PDF using MiKTeX on Windows through knitr and pandoc. LaTeX engine is xelatex.
My .Rmd file YAML header:
---
title: "My Title"
author: "Me"
geometry: margin=2cm
output:
pdf_document:
fig_caption: no
fig_height: 4
fig_width: 6
latex_engine: xelatex
html_document:
css: styles.css
keep_md: no
fontsize: 10pt
---
In the text I have
...parameter $\lambda=0.2$ with...
and I get "parameter = 0 2 with" -- both the lambda and even the dot are missing.
This line
mean of $\frac{1}{\lambda}=5$ of the
turns into
EDIT: When I try to add as an experiment (I'm on Windows)
mainfont: Arial
to the YAML header I get a pandoc error
! Undefined control sequence.
\fontspec_calc_scale:n ...ec_tmpb_dim }\fp_div:Nn
\l_fontspec_tmpa_fp {\l_fo...
l.18 \setmainfont{Arial}
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
UPDATE:
Running pdflatex report1.pdf produces a correct PDF.
RStudio runs "C:/Program Files/RStudio/bin/pandoc/pandoc" report1.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output report1.pdf --template "D:\Documents\R\win-library\3.2\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine xelatex
So it seems the elsewhere recommended engine "xelatex" produces faulty output, while "pdflatex" works. Unfortunately I forgot the reasons given by some sources incl. SO that I followed a few days ago when switching to xelatex, I only remember it was highly recommended to use that with knitr in RStudio rather than pdflatex.
I'm posting this answer to increase the visibility of #user1983395 's comment regarding this error.
I was facing the same error as OP. pdflatex just produced the mentioned error, and xelatex produced an error whenever there was a call to a math formula ( $X_i$ or $$X_i$$ ).
After running C:\Program Files\MiKTeX 2.9\miktex\bin\x64\updmap.exe I was able to generate a pdf both with pdflatex and xelatex. Please notice that you may need to run it as administrator.

Resources