Include Rmd appendix after references
In order to have appendices after the references, you simply include a div with id="refs" which tells pandoc where to include the references section. For example:
```{r, child = "08_discussion.Rmd"}
```
# References
<div id="refs"></div>
```{r, child = "09_appendix.Rmd"}
```
Credit: https://twitter.com/tjmahr/status/763435602935095296
R Markdown: place an Appendix after the References section?
This is explained in the R Markdown Cookbook (section 3.5.4). We can force the bibliography to be printed at a particular place with:
# References
<div id="refs"></div>
# Appendix
Note that:
- this works if we cite the papers with
@id_of_paper
(which is the recommended way in R Markdown) but not with\cite{id_of_paper}
. - this does not work if we use
citation_package: biblatex
in YAML
Here's my adapted example:
---
title:
author:
date:
abstract:
output:
pdf_document:
template: NULL
number_sections: true
bibliography: references.bib
biblio-style: bwl-FU
---
# Partie 1
@greenwood_financial_1989
# References
<div id="refs"></div>
# Appendix
bla bla
Adding figures and tables after bibliography in Rmarkdown
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")
```
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
Pandoc insert appendix after bibliography
Eventually reference handling will change to make it possible to put the references wherever you like (https://github.com/jgm/pandoc/issues/771), but right now there's no easy way to do it.
As suggested here, you could put your appendix in a separate file, use pandoc to convert it to a LaTeX fragment, then include that fragment using the --include-after-body
flag. It would then come after the bibliography.
Including citation and references when using appendix on RMarkdown
So, I did actually find a solution that does use some minor trickery.
---
bibliography: bb.bib
fontsize: 11pt
nocite: '@*'
output:
pdf_document:
keep_tex: true
includes:
after_body: Demo2.tex
link-citations: true
---
```{r,include=FALSE}
library(tidyverse)
rmarkdown::render('Demo2.Rmd')
a <- readChar('Demo2.tex', file.size('Demo2.tex'))
a <- a %>% str_remove('[[:space:]]*\\\\hypertarget[[\\w\\W]]+\\z') %>%
str_remove('\\A[[\\w\\W]]+begin.document.')
writeChar(a, 'Demo2.tex',eos = NULL)
```
\newpage
\section{Testing}\label{sec1}
```{r}
summary(cars)
```
\section{Demo}
This was done using @shiina and we will use some info from Section \ref{sec1} to do.
```{r}
summary(iris[,1:2])
```
\section{References}
And your Appendix-file:
---
bibliography: bb.bib
fontsize: 11pt
output:
pdf_document:
keep_tex: yes
link-citations: true
---
\appendix
\section*{Appendix}
\section{Additional info}
In this section we also follow @shiina to explain concepts.
# References
results in:
The way it works is to render the Demo2.Rmd
- file before rendering the actual file and to keep the associated .tex
- file.
Then the non included R-chunk cuts of all the parts we don't want to have at the end of the main file and overwrites the Demo2.tex
-file.
What remains is the exact tex
-code you need to have your references working.
Feels pretty dirty, but should work.
Placing literature references in a table in Rmd
ftExtra
may be solution here:
---
title: "test"
author: "David"
date: "27/01/2021"
output: html_document
bibliography: test.bib
---
test[@guan_clinical_2020]
``` {r, echo = F}
library("flextable")
library("ftExtra")
dat <- dplyr::tibble("study_id" = c(1,2),
"lead_author" = c("Guan, Ni", "Guan, Liang"),
"sample_size" = c(1099, 1590),
"refs" = c("[@guan_clinical_2020]",
"[@guan_comorbidity_2020]"))
dat %>%
flextable() %>%
colformat_md() %>%
autofit()
```
Related Topics
R - Download Filtered Datatable
Highlight Minimum and Maximum Points in Faceted Ggplot2 Graph in R
R Multiple Conditions in If Statement
How to Scrape a Table with Rvest and Xpath
Dplyr: Mutate_At + Coalesce: Dynamic Names of Columns
Obtaining Percent Scales Reflective of Individual Facets with Ggplot2
Ordering Factors in Number Order for Ggplot
Remove Multiple Patterns from Text Vector R
Highlight Areas Within Certain X Range in Ggplot2
Percentage of Overlap Between Polygons
How to Force Seasonality from Auto.Arima
Why Does ".." Work to Pass Column Names in a Character Vector Variable
How to Calculate the Median on Grouped Dataset
Adding Missing Dates to Dataframe
Creating a More Continuous Color Palette in R, Ggplot2, Lattice, or Latticeextra