Search code examples
rr-markdownkableextra

How to cite a reference in kableExtra footnote?


I'm writing an R Markdown document in which there is a table. In the footnote of this table, I would like to cite an author. However, using @citationkey does not work in this situation.

Does anybody know how to do it?

Here's a reproducible example (I included all LaTeX packages needed in kableExtra but some of them may be unnecessary here):

---
title: "Untitled"
author: ""
date: ""
output: 
  bookdown::pdf_document2
bibliography: refs.bib
toc: false
header-includes:
  - \usepackage[utf8]{inputenc}
  - \usepackage[T1]{fontenc}
  - \usepackage{booktabs}
  - \usepackage{longtable}
  - \usepackage{array}
  - \usepackage{multirow}
  - \usepackage{wrapfig}
  - \usepackage{float}
  - \usepackage{colortbl}
  - \usepackage{pdflscape}
  - \usepackage{tabu}
  - \usepackage{threeparttable}
  - \usepackage{threeparttablex}
  - \usepackage[normalem]{ulem}
  - \usepackage{makecell}
  - \usepackage{xcolor}
---

Here, I can cite @abel2018.

```{r}
library(kableExtra)

kable(head(mtcars)) %>%
  footnote(general = paste0("A footnote in which I would like to cite @abel2018."))
```

# References {-}

And the reference in refs.bib:

@article{abel2018,
  title = {Estimates of {{Global Bilateral Migration Flows}} by {{Gender}} between 1960 and 2015},
  author = {Abel, Guy J.},
  date = {2018-09},
  journaltitle = {International Migration Review},
  shortjournal = {International Migration Review},
  volume = {52},
  pages = {809--852},
  doi = {10.1111/imre.12327},
  url = {http://journals.sagepub.com/doi/10.1111/imre.12327},
  urldate = {2019-12-14},
  langid = {english},
  number = {3}
}

Solution

  • The problem with your approach is that paste() does not recognize the key but simply treats it as text.

    For writing to HTML, one could use knitr::asis_output("@abel2018").

    Unfortunately, this does not work for bookdown. However, you may use a text reference, which is documented in bookdown:

    The syntax for a text reference is (ref:label) text, where label is a unique label throughout the document for text. It must be in a separate paragraph with empty lines above and below it. The paragraph must not be wrapped into multiple lines, and should not end with a white space.

    Then you can use (ref:foo) in your figure/table captions

    For example:

    (ref:abel-citation) @abel2018 
    
    ```{r}
    library("kableExtra")
    
    kable(mtcars) %>%
        footnote(general = paste0("A footnote in which I would like to cite (ref:abel-citation)"))
    ```