Adding figures and tables after bibliography in Rmarkdown
Asked Answered
L

1

9

I want to add tables and figures after the bibliography in an R Markdown document. However, R Markdown documents will by default always add the bibliography to the end of the report.

Is there an easy way to add content to the document after the references?

Attempted Solutions

A previous answer showed that there is a way to put the appendix after the bibliography in R Markdown. This appendix is a separate file and is added to the document with after_body in the YAML header. I tried 2 different possible solutions and none of them worked.

  1. I tried to put my appendix in a different file, but I ran into the problem of losing my references in the main file, as all the appendices are cross-referenced in the body of the paper. All references turn to ?? once I put them in a different file.
  2. I put all my figures and tables in a different file while also keeping them in the main file. Then, I used results = "hide" to hide them in the main file. The idea was to create 2 separate PDFs and to merge them. Unfortunately, when the figures are hidden, the references also turn to ??.

Additional information

  • I am using the output format bookdown:pdf_document2
  • My figures are created by an .R file and imported into my R Markdown file with include_graphics().
Logroll answered 14/7, 2018 at 4:3 Comment(0)
S
12

Rather than trying to include things in the after_body, you are better off just repositioning where the bibliography appears in the document. As explained in this answer, you can choose where the bibliography appears using <div id="refs"></div>.

Secondly, you can use bookdown to easily add appendices within a document. Using the header # (APPENDIX) Appendix {-} will change all following secton numbers from numbers to letters. Check out the bookdown book

Used in a full example:

---
title: "Untitled"
output: bookdown::pdf_document2
references:
- id: fenner2012a
  title: One-click science marketing
  author:
  - family: Fenner
    given: Martin
  container-title: Nature Materials
  volume: 11
  URL: 'http://dx.doi.org/10.1038/nmat3283'
  DOI: 10.1038/nmat3283
  issue: 4
  publisher: Nature Publishing Group
  page: 261-263
  type: article-journal
  issued:
    year: 2012
    month: 3
---

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

# Introduction

Here is a citation [@fenner2012a]

# References {-}

<div id="refs"></div>

\newpage 

# (APPENDIX) Appendix {-}

# Appendix A

Here is your table

```{r table, fig.pos="H"}
knitr::kable(mtcars[1:5, 1:5], caption = "A table") %>%
  kableExtra::kable_styling(latex_options = "HOLD_position")
```

enter image description here

Note: this will only work if you use pandoc's built-in citation package and won't work if you set citation_package: natbib in the YAML

Squash answered 19/7, 2018 at 0:9 Comment(5)
I copied and pasted your code and it worked. But, for some reason, when I try it on my actual work, it's not working. And by "it", I mean <div id="refs"></div>. I have noticed that the main difference is that my references are contained within a bib file, which is imported via the YAML header meanwhile the reference in your example is directly inside the YAML header. Do you think this is what causes the issue?Logroll
I just removed the following line from the YAML header and it worked! citation_package: natbibLogroll
Thanks for the update. I've altered the answer to highlight this. Also, there is no difference between the way this loads the bib file: it just makes the example self-contained and easily reproducibleSquash
@MichaelHarper is there a way to do this without removing the citation package? I really don't want to use pandoc for the bibliography, as it doesn't look good... I have to enclose some pdf's as an appendix after the bibliography, but my reference list looks really ugly with pandoc. Suggestions?Swashbuckling
What doesn't look good exactly? If you dislike the citation style you could change this by providing a different CSL file: rmarkdown.rstudio.com/… . Not immediately sure of a solution though as I don't use natbib, sorry!Squash

© 2022 - 2025 — McMap. All rights reserved.