6  Gifs

In R4Epi, we use quite a few gifs. That makes rendering the book to pdf format challenging. What happens when we add a gif to a qmd document and render it to pdf?

The following code works great for HTML format, but it messes up pdf format.

![A gif about file paths](img/file_path_gif.gif){#fig-file-paths}

And I can [cross reference](https://quarto.org/docs/authoring/cross-references.html) the gif by typing `@fig-file-paths`. See @fig-file-paths

::: {.callout-important}
For cross-references to work, the image must have a caption _and_ a label.
:::

What if I use knitr::include_graphics("path")?


```{r}
#| label: fig-file-paths
#| echo: false
#| fig-cap: |
#|   A gif about file paths.
#| fig-alt: |
#|   A gif about file paths.

knitr::include_graphics("file_path_gif.gif")
```

And I can [cross reference](https://quarto.org/docs/authoring/cross-references.html) the figure by typing `@fig-file-paths`. See @fig-file-paths

::: {.callout-important}
For cross-references to work, the image must have a caption _and_ a label.
:::

No, this doesn’t work either. Here is a pretty good discussion on the topic. It looks like the easiest route may be to use conditional content. Given the limited number of gifs in R4Epi, this shouldn’t be too big of a problem.

So, start by conditionally displaying the gif if the output format is html.


::: {.content-visible when-format="html"}

```{r}
#| label: fig-file-paths
#| echo: false
#| fig-cap: |
#|   A gif about file paths.
#| fig-alt: |
#|   A gif about file paths.

knitr::include_graphics("file_path_gif.gif")
```

:::

Which renders as…

A gif about file paths.
Figure 6.1: A gif about file paths.

Then, conditionally add a thumbnail image of the gif when the output format is pdf.


::: {.content-visible when-format="pdf"}

```{r}
#| label: fig-file-paths
#| echo: false
#| fig-cap: |
#|   A thumbnail of gif about file paths.
#| fig-alt: |
#|   A thumbnail gif about file paths.

knitr::include_graphics("file_path_gif.gif")
```

:::

Which renders as…