Bookdown unable to compile to pdf with external images

Hi,

I have been using Bookdown with no problems until I changed my figures to images on Imgur. The book compiles to gitbook with no problem. And individual files in the book can be rendered correctly with Pandoc to pdf. However, when I try to compile the entire book using:

bookdown::render_book('index.Rmd', 'bookdown::pdf_book')

compile fails and I get this error.

! Unable to load picture or PDF file 'https://imgur.com/Kt5CR1H.jpg'.

}
l.95 ...udegraphics{https://imgur.com/Kt5CR1H.jpg}

I am guessing that bookdown times out when trying to retrieve the images from imgur. Is there a parameter that can be used with bookdown that will allow the integration of remote images, as pandoc does for single files?

Thank you!

1 Like

I couldn't figure out a quick solution to your issue, but I did some investigating to figure out where things break. My main finding was that this is not a bookdown-specific issue, but appears to be a generic rmarkdown issue.

As you mentioned, pandoc can handle external images when converting from markdown to pdf. I verified this by creating a minimal markdown file that imports the external image. The output test.pdf displays the external image.

$ cat test.md
![An external image](https://imgur.com/Kt5CR1H.jpg)
$ pandoc -o test.pdf test.md

However, this breaks when using rmarkdown::pdf_document():

$ cat test.Rmd
---
output: pdf_document
---


```{r ext-fig}
knitr::include_graphics("https://imgur.com/Kt5CR1H.jpg")
```
$ Rscript -e 'rmarkdown::render("test.Rmd")'


processing file: test.Rmd
  |...................................                                   |  50%
  ordinary text without R code

  |......................................................................| 100%
label: ext-fig

output file: test.knit.md

/usr/bin/pandoc +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.tex --self-contained --highlight-style tango --latex-engine pdflatex --variable graphics --variable 'geometry:margin=1in'
! Package pdftex.def Error: File `https://imgur.com/Kt5CR1H.jpg' not found: usi
ng draft setting.

Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
Execution halted

So this implies that one of the arguments that is passed to pandoc by rmarkdown::pdf_document() is breaking the default behavior.

Thank you very much for clarifying this. Perhaps an rmarkdown specialist could help us with identifying the parameter(s) at fault and suggesting a fix for this, or a workaround.

1 Like

Doing some more investigating. It appears that external images are not supported by LaTeX by default, but there are workarounds.

Something strange is going on though. I can create the PDF using knitr::knit() followed by rmarkdown::pandoc_convert(), which is the general strategy used by rmarkdown::render(). The output test.pdf contains the image, just as expected.

Here's a reprex to demonstrate the issue:

rmd <- tempfile(fileext = ".Rmd")
md <- tempfile(fileext = ".md")
pdf <- tempfile(fileext = ".pdf")
writeLines(c(
  "```{r ext-fig}",
  "knitr::include_graphics(\"https://imgur.com/Kt5CR1H.jpg\")", 
  "```"),
  con = rmd)

# This works
knitr::knit(rmd, output = md)
rmarkdown::pandoc_convert(md, to = "latex",
                          output = pdf,
                          verbose = TRUE)

# This fails
rmarkdown::render(rmd, rmarkdown::pdf_document())

When I run it in Ubuntu, I see the following:

Click to display results
> rmd <- tempfile(fileext = ".Rmd")
> md <- tempfile(fileext = ".md")
> pdf <- tempfile(fileext = ".pdf")
> writeLines(c(
+ "```{r ext-fig}",
+ "knitr::include_graphics(\"https://imgur.com/Kt5CR1H.jpg\")", 
+ "```"),
+ con = rmd)
> # This works
> knitr::knit(rmd, output = md)
processing file: /tmp/Rtmptr2FWv/file19c84de330be.Rmd
 |.................................................................| 100%
label: ext-fig
output file: /tmp/Rtmptr2FWv/file19c84d78fb80.md
[1] "/tmp/Rtmptr2FWv/file19c84d78fb80.md"
> rmarkdown::pandoc_convert(md, to = "latex",
+ output = pdf,
+ verbose = TRUE)
/usr/lib/rstudio-server/bin/pandoc/pandoc +RTS -K512m -RTS /tmp/Rtmptr2FWv/file19c84d78fb80.md --to latex --output /tmp/Rtmptr2FWv/file19c83c964008.pdf 
> # This fails
> rmarkdown::render(rmd, rmarkdown::pdf_document())
processing file: file19c84de330be.Rmd
 |.................................................................| 100%
label: ext-fig
output file: file19c84de330be.knit.md
/usr/lib/rstudio-server/bin/pandoc/pandoc +RTS -K512m -RTS file19c84de330be.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output file19c84de330be.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter /usr/lib/R/site-library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /usr/lib/R/site-library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in' 
! Package pdftex.def Error: File `https://imgur.com/Kt5CR1H.jpg' not found: usi
ng draft setting.
Error: LaTeX failed to compile file19c84de330be.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See file19c84de330be.log for more info.
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
 [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 
 [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8 
 [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C 
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C 
attached base packages:
[1] stats graphics grDevices utils datasets methods base 
loaded via a namespace (and not attached):
 [1] compiler_3.6.3 magrittr_1.5 htmltools_0.4.0 tools_3.6.3 
 [5] tinytex_0.21 Rcpp_1.0.4.6 rmarkdown_2.1 stringi_1.4.6 
 [9] highr_0.8 knitr_1.28 digest_0.6.25 stringr_1.4.0 
[13] xfun_0.13 rlang_0.4.5 evaluate_0.14

(Note: I tried using reprex, but it failed for a different reason, so I didn't want to complicate the example further)

I am having trouble following details as I am not a regular R user. Could this be an issue with Tinytex? Am also running under Ubuntu.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.