Insert GIF in pdf using rmarkdown - r

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?

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.

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

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.

Related

Can I conditionally exclude some elements (code blocks) from rendering to the PDF and ePub outputs when compiling a bookdown document?

RMarkdown knitting creates beautiful html, including animated gifs with R codeblocks like:
{r, echo=FALSE, out.width="80%", fig.cap=""}
knitr::include_graphics(path="imagesForRmd/visualSearch/directionOfMotionPopsOut.gif")
While such codeblocks work for including images other than gifs in the PDF output of bookdown, animated gifs unsurprisingly yield an error:
Cannot find the file(s): "(imagesForRmd/visualSearch/directionOfMotionPopsOut.gif"
Is there a way to conditionally exclude specific content in my .Rmd files, such as the above codeblock, from the pdf_book and ePub rendering, so that the error does not occur? Then I could create the html version with the animated gifs, and the PDF and ePub without.
I think it's more idiomatic to do this using the chunk option eval. As already mentioned, using knitr you may check the output format on knitting. Since epub is considered an HTML output format you may do it like this:
```{r, eval = knitr::is_html_output(excludes = "epub")}
knitr::include_url(path = "your_image.gif")
```
```{r, eval = knitr::is_latex_output()}
knitr::include_graphics("your_image.png")
```
One solution is to write an if/else statement to display the animated GIF in the HTML book, and display a static PNG image in the PDF and other books:
if(knitr::is_html_output()) knitr::include_url("images/sample.gif", height = "250px") else knitr::include_graphics("images/sample.png")

R Markdown Workflow to Render a Powerpoint Presentation as PDF

I have begun using Rmd to render Powerpoint presentations consistently, using the YAML tags and more importantly, a reference Powerpoint to ensure standardized / consistent formatting:
output: powerpoint_presentation: slide: reference_doc: "reference.pptx". When I want to share a PPT document as a reference for my peers / students however, I want to be able to have my slides available as a .pdf file.
I have had success using shell commands in R using LibreOffice's soffice command, however I am not always at a workstation with that available. Is there a portable solution / executable I can call, or additional Rmd tags for rendering a document with a .pptx reference document, but rendered as a .pdf file?
I believe you have to specifiy output: beamer_presentation in the header.
Take a look at this: https://rmarkdown.rstudio.com/formats.html

Remove bookmarks in PDF created by RMarkdown

When I knit a PDF using RMarkdown, it automatically creates bookmarks in the PDF file from any headers I use.
For example a "bookmark" in the PDF is created when I do:
this is a header 2
Is there any way to prevent this from happening?
Thanks.
Looks like you have to modify the latex template.
https://github.com/rstudio/rmarkdown/blob/master/inst/rmd/latex/default.tex#L105
Set bookmarks=false at that line above. Then save as mytemplate.tex file to your working directory, and use this option in your front matter:
output:
pdf_document:
template: mytemplate.tex

knitr html to Word docx using pandoc

I have been saving some example R markdown html output to Word using pandoc. I actually only do this so I can add some page breaks for easier printing:
system("pandoc -s Exercise1.html -o Exercise1.docx")
Although the output is acceptable I was wondering if there is a way to keep the original syntax highlighting of the R chunks (just as they are in the original knit HTML document)?
Also, I seem to be loosing all images in the conversion process and have to stick them into Word by hand. Is that normal?
Using the rmarkdown package (baked into RStudio Version 0.98.682, the current preview release) it's very simple to convert Rmd to docx, and code highlighting is included in the docx file.
You just need to include this at the top of your markdown text:
---
title: "Untitled" # obviously you can change this
output: word_document # specifies docx output
---
However, it seems that page breaks are still not supported in this conversion.
Why not convert the markdown directly to Word format?
Anyway, Pandoc does not support syntax highlighting in Word: "Currently, the only output formats that uses this information are HTML and LaTeX."
About the images: the Word file would definitely include those if you'd convert the markdown to Word directly. I am not sure about the HTML source, but I suppose you might have a path issue.

How can I Insert images into .rnw document

I work in R and deliver PDF file with Sweave, sweave(.rnw document). I need to put into my text a JPEG image. I didn't find any function on the web and have no ideas about how I could do that.
You include it just like you include an image in any other LaTeX document:
http://amath.colorado.edu/documentation/LaTeX/reference/figures.html#pdf
\includegraphics{myimage.jpg}
Put it in a LaTeX block not an R code block. As the link points out, you'll need to compile with pdflatex.

Resources