2
votes

I am using rmarkdown to generate both HTML and pdf file. In my .Rmd file, I included a GIF like this:

![](www/script.gif)

When I knit the to HTML it works fine.

rmarkdown::render(documentation_file, encoding="UTF-8")

However, when I try to knit to PDF using

rmarkdown::render(documentation_file, rmarkdown::pdf_document(latex_engine = "xelatex"), encoding="UTF-8")

I have the following problem:

! LaTeX Error: Unknown graphics extension: .gif.

I do not mind to lose the animation of the gif, a static version of it is perfectly fine.

Is there any easy way to include/convert on the fly the GIF to my PDF document?

3
Related question in TEX.SE: tex.stackexchange.com/questions/7602/…CL.
you can now include animation in pdf, resources.rstudio.com/rstudio-conf-2020/… see 8 min markuser63230

3 Answers

2
votes

It's not possible to directly include GIFs in a LaTeX document.

In general LaTeX, you can only include GIFs if you use latex to compile your document; when using pdflatex, xelatex and lualatex you need to manually convert your figure to e.g. PNG, JPG or PDF.

RMarkdown by default uses pdflatex; while you may change the LaTeX engine by specifying e.g. latex_engine: xelatex below pdf_document in the YAML header of your document, it is not possible to use latex to compile (latex would first create a DVI file, which is then converted to a PS and then in turn to a PDF).

So the easiest (and only) solution would be to convert all GIF figures to PNGs (or JPGs), and then include them as images in your RMarkdown document.

2
votes

Try this in your chunk

if (knitr:::is_latex_output()) {
  knitr::asis_output('\\url{....}')
} else {
  knitr::include_graphics("script.gif")
}

And in the url put the url to your gif

0
votes

You could create a snapshot image of the first frame of the gif, https://stackoverflow.com/a/12554723/10346727 I don't think PDF supports gif format, or any moving format for that matter.