Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown add footnote to figure caption

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
```
like image 579
mavericks Avatar asked Nov 10 '20 15:11

mavericks


People also ask

How do you add a footnote in R markdown?

To create a footnote in R Markdown, you use the carrot ^ followed immediately by square brackets []. Put the text inside of the [] and it'll print that at the bottom of the page. Code for a footnote will look like this: ^[This sentence will be printed as a footnote.] .

What is Knitr in RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.


1 Answers

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.

like image 167
Nina Avatar answered Sep 22 '22 16:09

Nina