Error outputting png from TikZ in bookdown (R-pdftools issue?)

I am trying to run some very basic TikZ code, such as:

```{r TikZtest, engine="tikz"}
\begin{tikzpicture}
\draw (0,0) -- (1,1);
\end{tikzpicture}
```

However, if I try to compile using gitbook in RStudio (e.g. by adding this code to 01-intro.Rmd in a new Bookdown project) then it exits:

==> rmarkdown::render_site(output_format = 'bookdown::gitbook', encoding = 'UTF-8')



processing file: _main.Rmd
  |.....                                                                 |   7%
   inline R code fragments

  |.........                                                             |  13%
label: unnamed-chunk-1 (with options) 
List of 1
 $ eval: logi FALSE

  |..............                                                        |  20%
  ordinary text without R code

  |...................                                                   |  27%
label: unnamed-chunk-2 (with options) 
List of 1
 $ eval: logi FALSE

  |.......................                                               |  33%
  ordinary text without R code

  |............................                                          |  40%
label: unnamed-chunk-3 (with options) 
List of 1
 $ include: logi FALSE

  |.................................                                     |  47%
  ordinary text without R code

  |.....................................                                 |  53%
label: TikZtest (with options) 
List of 2
 $ engine : chr "tikz"
 $ fig.ext: chr "png"


Exited with status 134.

This also crashes when using bookdown::pdf_book and the fig.ext=png option, which suggests a problem with RMagick. Running these rendering commands directly in R either on this same project or on just the TikZ chunk crashes R with the following message

/usr/include/c++/11/bits/basic_string.h:1136: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::back() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&]: Assertion '!empty()' failed.
Aborted (core dumped)

which is the same message when trying to use the magick library to read the (successfully rendered) pdf figure from bookdown::pdf_book:

library(magick)
x <- image_read_pdf("_main_files/figure-html/TikZtest-1.pdf")

and now I'm stumped! I don't even know where to look for help! I am running:

  • Fedora Linux 35
  • R version 4.1.2
  • RStudio version 2021.09.1 Build 372
  • RMagick version 2.7.3 which uses ImageMagick version 6.9.12.28

I would appreciate any help!

I understand you are getting this message even when using magick alone ?

If so, I don't think this has anything to do with bookdown or knitr. It seems you have an issue with the magick installation. You can try reinstalling the package making sure it compiles correctly. Do you have the last version of the package ?

Yes, the error comes up when trying to use image_read_pdf. I have (re)installed the R-magick package from Fedora repositories, which is version 2.7.3 (the latest version - see https://cran.r-project.org/web/packages/magick/index.html) and it still fails. I've also tried removing the Fedora package and installing magick from within R and I still have the same error message.

Don't know if this is directly related but you may need to have available on your system the requirement

SystemRequirements: ImageMagick++: ImageMagick-c++-devel (rpm) or libmagick++-dev (deb)

From CRAN - Package magick

You could try installing a Linux binary build from RSPM if your OS is supported. See package page RStudio Package Manager and setup page RStudio Package Manager

If you have an issue installing the CRAN version on your system, you could open an issue in magick github repo: Issues · ropensci/magick · GitHub

Yes, when I first tried to install the magick package within R it complained about ImageMagick-c++-devel, but after I installed that from the Fedora repositories, the magick package installed fine. However the error message still came up.

I've tried experimenting with the older versions, it seems that this issue was introduced in v3 of the pdftools package and I've submitted a bug report on their GitHub page. I guess I'll have to wait and use an older version for the time being.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.