I'm trying to include plots in a knitted Rmd pdf document, but the plots always come out cropped. Here's a small example of what I'm experiencing:
---
output:
pdf_document: default
---
```{r}
plot (cars)
```
```{r, fig.width=2}
plot (cars)
```
```{r, out.width='\\textwidth'}
plot (cars)
```
This Rmd produces a pdf that looks something like this (note the chopped off x axis labels):
Any ideas what causes this?
Software info:
knitr 1.23
pandoc 2.7.2
R 3.6.0
The call that compiles the document is
/usr/bin/pandoc +RTS -K512m -RTS rmdtest.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output rmdtest.tex --template /usr/lib/R/library/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --variable 'geometry:margin=1in' --variable 'compact-title:yes'
Related
I am trying to knit a PDF document using R Markdown. The markdown successfully knits to 100% but upon trying to create the output file, it produces the following error:
output file: project_2_cusack.knit.md
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS project_2_cusack.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output project_2_cusack.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in'
! Output loop---100 consecutive dead cycles.
\end#float ... \#tempdima \penalty \#floatpenalty
\else \vadjust {\penalty -...
l.385 \end{figure}
Error: LaTeX failed to compile project_2_cusack.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See project_2_cusack.log for more info.
Execution halted
I have specified to save as PDF so unsure why it is trying to knit as a tex file.
Any suggestions on troubleshooting?
I'm trying to knit the mwe below to pdf using rstudio (1.2.5019)
---
title: "Test"
author: "Me"
output:
pdf_document:
fig_caption: yes
keep_tex: yes
fig_crop: yes
---
This should be cropped
```{r}
library(knitr)
```
```{r named-chunk}
plot(x <- 0:100, dbinom(x, size=100, prob=0.6), type='l',
main="Not cropped", xlab="Looks good", ylab="Nok")
```
The output pdf looks fine except the plot has not been cropped with pdfcrop.
Here's the log output.
processing file: cropping.Rmd |..................
| 25% ordinary text without R code
|...................................
| 50% label: unnamed-chunk-1
|....................................................
| 75% ordinary text without R code
|......................................................................|
100% label: named-chunk
output file: cropping.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS
cropping.utf8.md --to latex --from
markdown+autolink_bare_uris+tex_math_single_backslash --output
cropping.tex --self-contained --highlight-style tango --pdf-engine
pdflatex --variable graphics --lua-filter
"C:/Users/marti/Documents/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua"
--lua-filter "C:/Users/marti/Documents/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua"
--variable "geometry:margin=1in"
Output created: cropping.pdf
pdfcrop works just fine from my command line but isn't called by rstudio. It is also located by rstudio.
Sys.which('pdfcrop')
pdfcrop
"C:\texlive\2019\bin\win32\pdfcrop.exe"
What am I missing?
This actually solved it.
``` {r crop-hook,cache=FALSE}
knit_hooks$set(crop=hook_pdfcrop)
```
```{r named-chunk, crop=TRUE}
plot(x <- 0:100, dbinom(x, size=100, prob=0.6), type='l',
main="Not cropped", xlab="Looks good", ylab="Nok")
```
As far as I understood from the documentation this shouldn't be necessary?
Install pdfcrop via livetex or miktex --- the one you are using to write latex documents.
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.
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"))
...
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>.