tifu
tifu

Reputation: 1411

Dynamic footnote referencing in rmarkdown / bookdown

I am combining several .Rmd-files into one large document using bookdown. The individual files all contain footnotes, starting with ^[1]. This obviously leads to duplicate footnotes in the final document, with bookdown unable to assert which reference belongs to which footnote.

As a consequence, I am wondering whether there is a way to dynamically generate footnotes when the document is rendered, but I could not find anything related to that in the bookdown docs.

I have this working solution using a custom function:

---
title: "Untitled"
output:
  html_document:
---

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

footnote.nr <- 0

footnote.counter <- function(){
  footnote.nr <- footnote.nr + 1
  .GlobalEnv$footnote.nr <- footnote.nr
  return(footnote.nr)
}
```

Lorem ipsum.[^`r footnote.counter()`]

[^`r footnote.nr`]: Test

Lorem ipsum.[^`r footnote.counter()`]

[^`r footnote.nr`]: Test2

However, this would result in me having to retrofit the entire document which would be just as much manual labor as starting footnote numbering completely anew (though it is probably less error prone). Are there any other solutions? I would also be okay with footnotes being rendered for each individual chapter, meaning that the first footnote in each chapter starts with a 1.

Upvotes: 1

Views: 884

Answers (2)

Victor
Victor

Reputation: 26

In my humble opinion, @jtbayly's answer is only partially correct as using Rstudio's visual editing mode can indeed prompt it to make automatic changes to your text by adding a unique identifier before the citation (ex: [^love1]).

However, that did not initially work for me, and I had to add, for each child document the following option in the yaml header :

editor_options:
  markdown:
    references:
      prefix: "[insert the unique identifier you want for this child document]"

This should then work, and would allow you to compile multiple .Rmd files into a single large document without RMarkdown/Pandoc messing with the citations. However, don't forget to go back to "Source" mode and "Visual" mode once after editing the yaml header for the editor to make the change.

Upvotes: 1

jtbayly
jtbayly

Reputation: 303

Another option is to use Rstudio to edit your files, and turn on visual editing. If you do, it will make some automatic changes to your text, including giving your footnotes unique names.

Upvotes: 1

Related Questions