rstudio / rmarkdown

Dynamic Documents for R
https://rmarkdown.rstudio.com
GNU General Public License v3.0
2.87k stars 975 forks source link

Knit PDF won't compile if all plots have fig.align="center" #325

Closed pitakakariki closed 8 years ago

pitakakariki commented 9 years ago

The simple document,


---
output: pdf_document

---
```{r, fig.align='center'}
plot(cars)

fails to compile, giving this error:

! Undefined control sequence. l.95 \begin{center}\includegraphics


If there's a plot without `fig.align` then everything works fine, e.g.

output: pdf_document


plot(cars)
plot(cars)

I think the problem comes up in the template `default.tex`, where the variable `graphics` isn't set:

$if(graphics)$ \usepackage{graphicx} ... $endif$


NB: I'm using RStudio Version 0.98.1091, this is my session information:

sessionInfo() R version 3.1.2 (2014-10-31) Platform: x86_64-w64-mingw32/x64 (64-bit)

locale: [1] LC_COLLATE=English_New Zealand.1252 LC_CTYPE=English_New Zealand.1252
[3] LC_MONETARY=English_New Zealand.1252 LC_NUMERIC=C
[5] LC_TIME=English_New Zealand.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] knitr_1.8.4

loaded via a namespace (and not attached): [1] bitops_1.0-6 devtools_1.6.1 digest_0.6.4 evaluate_0.5.5 formatR_1.0
[6] htmltools_0.2.6 httr_0.5 RCurl_1.95-4.4 rmarkdown_0.3.13 stringr_0.6.2
[11] tools_3.1.2 yaml_2.1.13

yihui commented 9 years ago

You need to add graphics: yes to your YAML metadata in this case, because fig.align = 'center' means we have to write raw LaTeX code to center your plot (there is no native syntax for figure alignment in Markdown), then Pandoc will not be able to know you generated figures in this document. It only knows there are figures when the native Markdown syntax is used, e.g. ![]().

At some point in the future, we may need to automate this, or just always pass the graphics variable to Pandoc, or always use \usepackage{graphicx} in the template.

pitakakariki commented 9 years ago

Some sort of automatic fix would be great, it's pretty confusing that you need to do something special if all of your figures are aligned, but not if it's only some of them.

Is there any downside to always including \usepackage{graphicx}? I'd considered doing that to the template myself, but I'd have to remember every time it was updated.

yihui commented 9 years ago

I guess the only downside is that it may waste a few milliseconds (life is short...) if your markdown document really contains no graphics :)

If you consider fixing the problem by yourself, I'd strongly recommend using graphics: yes in YAML instead of modifying the template, because it is much more complicated to maintain the LaTeX template than adding a parameter field in YAML.

bas-atucker commented 8 years ago

Sneaky! I made a change to the custom template and referenced the new (altered) template in my local folder, and then this old issue came back to life. I'm glad the fix was documented here. graphics: yes added.

pata-eth commented 7 years ago

Hello everyone, I am trying to render a PDF report from a Shiny App and it seems that this problem persists. When I remove the following code chunk options, the report renders with no issues.

{r , echo = FALSE, fig.width=8, fig.height=4, fig.align='center'} => Does not work

{r , echo = FALSE} => Works

This is the error I'm getting from pandoc:

"C:/Users/hector.herrada/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS DecayReport.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc59c43c471eef.pdf --template "C:\Users\hector.herrada\R-3.3.3\library\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --include-in-header headerfooter.tex --variable graphics=yes --variable "geometry:margin=1in" ! Undefined control sequence. \grffile@filename ->C:\Users\HECTOR ~1.HER\AppData \Local \Temp \RtmpU 1WmYl.123.tex/Decay Forecast-1} pandoc.exe: Error producing PDF

I can remove the chunk options and change the default figure settings in the YAML header. This works although it changes the settings for ALL figures in the doc and therefore does not provide control on the individual settings of the chunks.

Any idea what's going on and how we could fix it?

Thanks

nklein23 commented 6 years ago

@hherrada Have you found a solution? I experience the same issue.

yihui commented 6 years ago

@hherrada @NiklasDL Please make sure you have the latest versions of R packages (it seems you were using a very old version of the rmarkdown package).

nklein23 commented 6 years ago

@yihui Thank you for your immediate response. I got my new computer four days ago, so I have brand new R version and latest packages as well - nevertheless I tried updating but it did not help. The week before with my old computer it worked nicely without an error.

Here is a code example and the resulting error (unfortunatelly some parts are in german):


title: "hyperbandr tutorial" author: "Niklas" date: "23 März 2018" output: pdf_document header-includes:

  • \usepackage{graphicx}

first code chunk:

{r, echo = TRUE, message = FALSE} library("smoof") library("data.table") library("ggplot2")

second code chunk producing the error:

{r, echo = TRUE, fig.height = 3.6, fig.width = 6, fig.align ="center"} problem = makeBraninFunction()

opt = data.table(x1 = getGlobalOptimum(problem)$param$x1, x2 = getGlobalOptimum(problem)$param$x2)

(vis = autoplot(problem) + geom_point(data = opt, aes(x = x1, y = x2), shape = 20, colour = "red", size = 5))

the error message:

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS tutorial.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pandoc277c28975d48.tex --template "C:\Users\Niklas\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" output file: tutorial.knit.md

! Undefined control sequence. \grffile@filename ->C:\Users \Niklas \AppData \Local \Temp \RtmpCGG 8ZR\prev... l.186 ...ial_files/figure-latex/unnamed-chunk-2-1} \end{center} Here is how much of TeX's memory you used: 12827 strings out of 493333 185851 string characters out of 3139191 287090 words of memory out of 3000000 16163 multiletter control sequences out of 15000+200000 44090 words of font info for 52 fonts, out of 3000000 for 9000 1141 hyphenation exceptions out of 8191 32i,5n,38p,450b,336s stack positions out of 5000i,500n,10000p,200000b,50000s

Fehler: Failed to compile C:/Users/Niklas/AppData/Local/Temp/RtmpCGG8ZR/preview-b0c68af5db6.dir/tutorial.tex. See tutorial.log for more info. Zusätzlich: Warnmeldung: Ausführung von Kommando '"pdflatex" -halt-on-error -interaction=batchmode "C:/Users/Niklas/AppData/Local/Temp/RtmpCGG8ZR/preview-b0c68af5db6.dir/tutorial.tex"' ergab Status 1 Ausführung angehalten

yihui commented 6 years ago

@NiklasDL I believe your issue is a totally different one. How exactly did you compile the document? Is that a package vignette?

nklein23 commented 6 years ago

@yihui It is supposed to be a tutorial for my package someday. This is the current Rmd file: https://github.com/ja-thomas/hyperbandr/blame/master/vignettes/tutorial_branin.Rmd and I actually use "knit to pdf" in RStudio to compile the document. The error is caused by line 37. If I leave out the fig.align = "center" argument I am able to compile the file.

I am really sorry if I'am off the track.

yihui commented 6 years ago

@NiklasDL It is a different issue, but it is our fault (you could blame either the RStudio IDE or the rmarkdown package or probably both). You are not the first person who has had problems with compiling PDF vignettes (on Windows), but we haven't had a chance to investigate it yet. Sorry. For now, I guess the only way is R CMD build and install your package to view the PDF vignette (if you use devtools, keep in mind that install() does not build vignettes by default).

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.