4.5 Bibliographies and citations

For an overview of including bibliographies in your output document, you may see Section 2.8 of Xie (2016). The basic usage requires us to specify a bibliography file using the bibliography metadata field in YAML. For example:

---
output: html_document
bibliography: references.bib  
---

where the BibTeX database is a plain-text file with the *.bib extension that consists of bibliography entries like this:

@Manual{R-base,
  title = {R: A Language and Environment for Statistical
           Computing},
  author = {{R Core Team}},
  organization = {R Foundation for Statistical Computing},
  address = {Vienna, Austria},
  year = {2019},
  url = {https://www.R-project.org},
}

Items can be cited directly within the documentation using the syntax @key where key is the citation key in the first line of the entry, e.g., @R-base. To put citations in parentheses, use [@key]. To cite multiple entries, separate the keys by semicolons, e.g., [@key-1; @key-2; @key-3]. To suppress the mention of the author, add a minus sign before @, e.g., [-@R-base].

4.5.1 Changing citation style

By default, Pandoc will use a Chicago author-date format for citations and references. To use another style, you will need to specify a CSL (Citation Style Language) file in the csl metadata field, e.g.,

---
output: html_document
bibliography: references.bib
csl: biomed-central.csl
---

To find your required formats, we recommend using the Zotero Style Repository, which makes it easy to search for and download your desired style.

CSL files can be tweaked to meet custom formatting requirements. For example, we can change the number of authors required before “et al.” is used to abbreviate them. This can be simplified through the use of visual editors such as the one available at https://editor.citationstyles.org.

4.5.2 Add an item to a bibliography without using it

By default, the bibliography will only display items that are directly referenced in the document. If you want to include items in the bibliography without actually citing them in the body text, you can define a dummy nocite metadata field and put the citations there.

---
nocite: |
  @item1, @item2
---

4.5.3 Add all items to the bibliography

If we do not wish to explicitly state all of the items within the bibliography but would still like to show them in our references, we can use the following syntax:

---
nocite: '@*'
---

This will force all items to be displayed in the bibliography.

4.5.4 Include appendix after bibliography (*)

By default, the bibliography appears at the very end of the document. However, there can be cases in which we want to place additional text after the references, most typically if we wish to include appendices in the document. We can force the position of the references by using <div id="refs"></div>, as shown below:

# References

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

# Appendix

Although <div> is an HTML tag, this method also works for other output formats such as PDF.

We can improve this further by using the bookdown package (Xie 2020c), which allows you to insert a special header # (APPENDIX) Appendix {-} before you start the appendix, e.g.,

# References

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

# (APPENDIX) Appendix {-} 

# More information

This will be Appendix A.

# One more thing

This will be Appendix B.

The numbering style of appendices will be automatically changed in LaTeX/PDF and HTML output (usually in the form A, A.1, A.2, B, B.1, and so on).

References

———. 2016. Bookdown: Authoring Books and Technical Documents with R Markdown. Boca Raton, Florida: Chapman; Hall/CRC. https://github.com/rstudio/bookdown.
———. 2020c. Bookdown: Authoring Books and Technical Documents with r Markdown. https://github.com/rstudio/bookdown.