Search code examples
rlatexpandocbookdownblogdown

Can't insert plots in bookdown


I'm using bookdown to generate notes for an R. A minimal bookdown example that exhibits the error:

https://github.com/DavisBrian/bookdown_error

Everything worked great until I tried to add a plot (dataviz.Rmd). When building the book I get the following error:

! LaTeX Error: File `dataviz_files/figure-latex/plotxyx-1' not found.

Error: Failed to compile rclassnotes.tex. See rclassnotes.log for more info.
In addition: Warning message:
In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
  Failed to find a package that contains dataviz_files/figure-latex/plotxyx-1
Execution halted

Exited with status 1.

I've run this under:

> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

loaded via a namespace (and not attached):
 [1] compiler_3.4.4  backports_1.1.2 bookdown_0.7    magrittr_1.5    rprojroot_1.3-2 htmltools_0.3.6 tools_3.4.4    
 [8] yaml_2.1.18     Rcpp_0.12.16    stringi_1.1.7   rmarkdown_1.9   knitr_1.20      xfun_0.1        stringr_1.3.0  
[15] digest_0.6.15   evaluate_0.10.1

Any ideas on where to go from here?

Edit:

It seems very similar to: https://github.com/rstudio/bookdown-demo/issues/3 and https://github.com/rstudio/bookdown/issues/342

Note I have this issue with both Windows and Mac OSX. I don't think it's a simple software installation error of one of the components.

The files seem to be created in _bookdown_files/dataviz_files/figure-latex/ and temporarily in _dataviz_files/figure-latex/


Solution

  • The problem was due to the fact that you set the graphical device to svg in _common.R: https://github.com/DavisBrian/bookdown_error/blob/9f4078a/_common.R#L13

    knitr::opts_chunk$set(
      comment = "#>",
      collapse = TRUE,
      cache = FALSE,
      out.width = "70%",
      fig.align = 'center',
      fig.width = 6,
      fig.asp = 0.618,  # 1 / phi
      fig.show = "hold",
      dev = "svg"
    )
    

    LaTeX does not support SVG images natively (at least for now). You have to either use special LaTeX packages, or use an image format that LaTeX can recognize, such as pdf or png.