Rmarkdown add footnote to figure caption
Asked Answered
B

1

7

I would like to include a footnote within a figure caption in an R markdown report rendered to both PDF and HTML (report is based on bookdown/thesisdown/huskydown).
The ideal would be to use text references:

(ref:foo-footnote) Text in the footnote.
Can span multiple lines, but has to start on the line of the text reference.

(ref:foo-caption) My text with a footnote.^[(ref:foo-footnote)]

What I tried

---
title: "Footnote in Caption"
author: "Test"
output: html_document
#output: pdf_document
---

## Figure with caption which includes a footnote
<!-------------------------------------------------------------------->
<!-- Reference the figure "by number" as usual with \@ref(fig:foo) -->
<!-- Reference the figure "by name" by adding an anchor above the figure: -->
<!-- \hypertarget{fig:foo}{} -->
<!-- which can be referenced by: -->
<!-- [my linked text](#fig:foo) -->

(ref:foo-caption) My caption^[Footnote text.].
(ref:foo-scaption) My short caption

```{r foo, echo=FALSE, out.width='100%', fig.align = "center", fig.cap='(ref:foo-caption)', fig.scap='(ref:foo-scaption)'}
knitr::include_graphics(paste0(fig_path,"foo.png"), auto_pdf = TRUE)
# if auto_pdf = TRUE: includes PDF version of figure if available in same folder
```
Bouley answered 10/11, 2020 at 15:8 Comment(0)
B
2

Here is a reproducible example for PDF outputs from bookdown, but I haven't figured out how to make it work for HTML.

Essentially you need to include \\protect\\footnotemark in the figure caption, and then afterwards in the Rmd text (outside the chunk) include \footnotetext{Here is the footnote text.} with your text.

---
title: "Footnote in caption"
output: 
  bookdown::pdf_document2: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(bookdown)
```

```{r pressure, echo=FALSE, fig.cap="This is a figure caption with a footnote.\\protect\\footnotemark", out.width="100%"}
plot(pressure)
```

\footnotetext{Here is the footnote text.}

Here is a plot in Figure \@ref(fig:pressure). 

The original solution for raw LaTeX came from Tex StackExchange (https://tex.stackexchange.com/questions/10181/using-footnote-in-a-figures-caption) and I've just adapted for Rmd which requires you to use a double slash for LaTeX commands inside captions.

Billbug answered 28/2, 2022 at 9:35 Comment(1)
This is only sort of working for me. It makes the Rmarkdown file skip a footnote number and puts the corresponding footnote on the page above the figure.Held

© 2022 - 2024 — McMap. All rights reserved.