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:

  1. ---
  2. output: html_document
  3. bibliography: references.bib
  4. ---

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

  1. @Manual{R-base,
  2. title = {R: A Language and Environment for Statistical
  3. Computing},
  4. author = {{R Core Team}},
  5. organization = {R Foundation for Statistical Computing},
  6. address = {Vienna, Austria},
  7. year = {2019},
  8. url = {https://www.R-project.org},
  9. }

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.,

  1. ---
  2. output: html_document
  3. bibliography: references.bib
  4. csl: biomed-central.csl
  5. ---

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.

  1. ---
  2. nocite: |
  3. @item1, @item2
  4. ---

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:

  1. ---
  2. nocite: '@*'
  3. ---

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:

  1. # References
  2. <div id="refs"></div>
  3. # 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.,

  1. # References
  2. <div id="refs"></div>
  3. # (APPENDIX) Appendix {-}
  4. # More information
  5. This will be Appendix A.
  6. # One more thing
  7. 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.