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

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?
回答1:
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:
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.
来源:https://stackoverflow.com/questions/52275963/insert-gif-in-pdf-using-rmarkdown