Search code examples
rr-markdownbookdowncross-reference

Adding figures and tables after bibliography in Rmarkdown


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().

Solution

  • 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