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}
[cross reference](https://quarto.org/docs/authoring/cross-references.html) the gif by typing `@fig-file-paths`. See @fig-file-paths
And I can
::: {.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")
```
[cross reference](https://quarto.org/docs/authoring/cross-references.html) the figure by typing `@fig-file-paths`. See @fig-file-paths
And I can
::: {.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…
Then, conditionally add a thumbnail image of the gif when the output format is pdf.
- Create a duplicate of the gif in Finder.
- Add “_thumb” to the end of the file name (before the file extension).
- Open the duplicate file in preview.
- Use Preview’s sidebar to keep only one of the thumbnail images from the gif (select and delete the rest).
- Click File > Export and export to png.
- Delete the duplicate gif.
::: {.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…