R markdown knitr using the LaTeX environment minted when rendering to PDF - r

I am using the fabulous knitr() package in R to render rmarkdown to PDF via LaTeX. Since the default syntax highlighting is not what I want, and the LaTeX environment listings has severe limitations in highlighting R code, I wanted to use the LaTeX minted package, simply embedding the knitted code into the minted environment.
Did anybody make it work to use the LaTeX package minted with knitr?
I already am failing loading the minted package with a weird error :D
---
title: "Test"
author: "me"
date: "10. March 2019"
output:
pdf_document
header-includes:
- \usepackage{minted}
---
```{r}
library(knitr)
minted_hook = function(x, options) { paste0("\\begin{minted}{R}", x, "\\end{minted}\n") }
knit_hooks$set(
source = minted_hook,
warning = minted_hook,
message = minted_hook,
error = minted_hook
)
```
# Test
```{r}
# this is a comment
a <- 1:10
a
```
As soon as I load minted I get the error
"C:/PROGRA~1/PANDOC~1.1/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.tex --template "[path not shown]\R\WIN-LI~1\3.5\RMARKD~1\rmd\latex\DEFAUL~3.TEX" --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" --variable "compact-title:yes"
tlmgr search --file --global "/(.sty"
Fehler in grep(paste0("/", x[j], "$"), l)
> invalid regular expression '/(.sty$', Reason 'Missing ')
I did install pygminted as outlined here.

Related

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.

R Markdown knit fails when path includes "~" for html/pdf document but works for ioslides

As of knitr 1.12, there is the function include_graphics. From ?include_graphics:
The major advantage of using this function is that it is portable in
the sense that it works for all document formats that knitr supports,
so you do not need to think if you have to use, for example, LaTeX or
Markdown syntax, to embed an external image.
It seems that include_graphics() handles paths with "~" in them (shortcut to user directory) when knitting an ioslides Rmarkdown presentation, but fails when knitting a word/html document.
Reproducible example for ioslides (note that these will make a copy of the Rlogo.png onto your desktop:
---
title: "Rlogo ioslides"
output: ioslides_presentation
---
## Images?
```{r}
require(knitr)
rlogo <- paste0(.libPaths(), "/png/img/Rlogo.png")
include_graphics(rlogo)
file.copy(from = rlogo, to = "~/Desktop")
include_graphics("~/Desktop/Rlogo.png")
```
processing file: rlogo.Rmd
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m
-RTS rlogo.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
--output rlogo.html --smart --email-obfuscation none --self-contained --variable transition=0.4 --template /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rmarkdown/rmd/ioslides/default.html
--include-in-header /var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpKAAz4I/rmarkdown-str91efb7c6c16.html
--mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
output file: rlogo.knit.md
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m
-RTS rlogo.utf8.md --to ioslides_presentation.lua --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
--output /var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpKAAz4I/ioslides-output91ef159c1e3e.html
--slide-level 2
Output created: rlogo.html
And reproducible failure for word/html document:
---
title: "Rlogo word/html"
output:
html_document: default
word_document: default
---
## Images?
```{r}
require(knitr)
rlogo <- paste0(.libPaths(), "/png/img/Rlogo.png")
include_graphics(rlogo)
file.copy(from = rlogo, to = "~/Desktop")
include_graphics("~/Desktop/Rlogo.png")
```
processing file: rlogo.Rmd
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m
-RTS rlogo.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash
--output rlogo.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rmarkdown/rmd/h/default.html
--no-highlight --variable highlightjs=1 --variable 'theme:bootstrap' --include-in-header /var/folders/5_/l71sk6kn29z17n011g8kld5m0000gp/T//RtmpCR2Vrn/rmarkdown-str91681de2b0f3.html
--mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
output file: rlogo.knit.md
pandoc: Could not fetch ~/Desktop/Rlogo.png ~/Desktop/Rlogo.png:
openBinaryFile: does not exist (No such file or directory) Error:
pandoc document conversion failed with error 67 Execution halted
I'm using MacOS Sierra.
Why is this failing in one case but not the other?
Should work if you use path.expand('~/path') instead, I would expect. The problem is the resolution of the ~. Some functions call path.expand for you - others might require you to do it yourself. That or relative paths are an option if things are set up relatively. I prefer the relative path approach because it works nicely in R and with github, etc.
EDIT: Per #yihui's response - normalizePath is another tool to keep in mind.
In your example:
...
include_graphics(path.expand("~/Desktop/Rlogo.png"))
...

RStudio Knitr failed to convert Rmd to PDF (Windows 7)

I wanted to use RStudio (Version 0.99.903) and knitr to convert a .Rmd file (please see below) to a PDF file.
---
title: "test"
output: pdf_document
---
Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
When I run "Knit PDF" I got the following error message:
processing file: test.Rmd
output file: test.knit.md
! Undefined control sequence.
l.87 Roses are \textcolor
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\fuq\R\R-3.3.1\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43
Execution halted
However, if I add a R code chunk anywhere in that .Rmd file, for example:
---
title: "test"
output: pdf_document
---
Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
```{r}
summary(cars)
```
I could then successfully get a PDF that I expected.
But what was weird was that if set echo=FALSE in the above .Rmd file (see below)
---
title: "test"
output: pdf_document
---
Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
```{r, echo=FALSE}}
summary(cars)
```
Then again, I got the same error message:
! Undefined control sequence.
l.87 Roses are \textcolor
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.pdf --template "C:\Users\fuq\R\R-3.3.1\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43
Execution halted
I worked on Windows 7 64bit, and have got the latest version of MiKTex. Please could anyone give me some idea what was going wrong?
You have to include the package color, then it works. I don't know why this works just with an R chunk in it. But this solution will solve this problem:
---
title: "test"
output: pdf_document
header-includes: \usepackage{color}
---
Roses are \textcolor{red}{red}, violets are \textcolor{blue}{blue}.
If you want to knit colors to HTML, you have to use this code:
---
title: "test"
output: html_document
---
Roses are <span style="color:red">red</span>,
violets are <span style="color:blue">blue</span>.

error 43 in pandoc: render Rmd with rmarkdown include LaTeX qtree

I am trying to produce a tree diagram in a Rmd file I expect to look like this:
Using rmarkdown's render function.
But get an error 43 I do not know how to interpret. How can I get the pdf to render? What's causing the error?
Rmd file
---
title: "testtree"
header-includes:
- \usepackage{qtree}
output:
pdf_document
---
\Tree [.S [.NP LaTeX ] [.VP [.V is ] [.NP fun ] ] ]
Success
Error message
> rmarkdown::render("testtree.Rmd", "all")
processing file: testtree.Rmd
|.................................................................| 100%
ordinary text without R code
output file: testtree.knit.md
"C:/Users/trinker/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS testtree.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output testtree.pdf --template "C:\R\R-3.2.2\library\rmarkdown\rmd\latex\default-1.14.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"
! Paragraph ended before \doanode was complete.
<to be read again>
\par
l.90
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Users/trinker/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS testtree.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output testtree.pdf --template "C:\R\R-3.2.2\library\rmarkdown\rmd\latex\default-1.14.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"' had status 43
>
The following .Rnw document compiles successfully:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{qtree}
\begin{document}
Here is a code chunk.
\Tree [.S a [.NP {\bf b} c ] d ]
You can also write inline expressions, e.g. $\pi=\Sexpr{pi}$, and \Sexpr{1.598673e8} is a big number.
\end{document}
Pandoc turns the last two closing brackets ] ] as {]} {]}, a behavior you can see if you use output option keep_tex: true. I am not sure whether this is a bug, you should ask this on the pandoc mailing-list or file a report.
A quick fix is to use pandoc's ability to ignore the code inside an environment and surround your command with a dummy environment:
---
title: "testtree"
header-includes:
- \usepackage{qtree}
- \newenvironment{dummy}{}{}
output:
pdf_document:
keep_tex: true
---
\begin{dummy}
\Tree [.S [.NP LaTeX ] [.VP [.V is ] [.NP fun ] ] ]
\end{dummy}
Success

R knitr PDF problems with \includegraphics

Using a new empty .rmd document, this code works:
![](RainbowDolphin.png)
\begin{center}
\includegraphics[width=4in]{RainbowDolphin.png}
\end{center}
But without the first line, it doesn't:
\begin{center}
\includegraphics[width=4in]{RainbowDolphin.png}
\end{center}
I get an error:
! Undefined control sequence.
l.71 \includegraphics
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" Sampling_03.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Sampling_03.pdf --template "C:\PROGRA~1\R\R-31~1.2\library\RMARKD~1\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"' had status 43
Execution halted
Weird. Any help appreciated!
It's because the latex template doesn't load the graphicx package by default. You can do it manually by adding this to the yaml header:
---
title: "Untitled"
header-includes: \usepackage{graphicx}
output:
pdf_document:
keep_tex: true
---
You may let Pandoc know that you have graphics in this document by specifying the YAML metadata:
---
graphics: yes
---

Resources