Automatic references in RMarkdown

RAP group Writing Workflow

A quick tutorial on how to cite references in RMarkdown.

Joseph V. Casillas
10-11-2019

Overview

Using RMarkdown to generate reproducible documents has a lot of advantages. One of them is the ability to import .bib files to cite sources and automagically generate a bibliography. In this post we will go over a simple workflow for importing a bib file, citing some articles, and generating a references section.

Getting started

First, we’ll need a .bib file to import. For this post we will use a simple example that can be downloaded here. This file should live in the same directory as your .Rmd file (or you can specify a custom path in the YAML frontmatter). The file in this case is sample_refs.bib and contains two references: flege1995 and kuhl1992.

If we open the file (it’s just a normal text file with a .bib ending) we will see LaTeX/bibtex syntax:

@article{flege1995,
  title={Second language speech learning: Theory, findings, and problems},
  author={Flege, James E},
  journal={Speech perception and linguistic experience: Issues in cross-language research},
  volume={92},
  pages={233--277},
  year={1995}
}

@article {kuhl1992,
  author = {Kuhl, PK and Williams, KA and Lacerda, F and Stevens, KN and Lindblom, B},
  title = {Linguistic experience alters phonetic perception in infants by 6 months of age},
  volume = {255},
  number = {5044},
  pages = {606--608},
  year = {1992},
  journal = {Science}
}

The advantage of using a .bib file for citing references is that all we have to do is supply the important information, i.e., author, year, etc., and Rmarkdown will tell pandoc-citeproc how to format them. In our case we will use the default, but you can use any format you want (even custom formats). For example, you can download an APA Citations Style Language file (apa.csl) from here. If you do this make sure to put this file in the same directory as your .Rmd file (or specify a custom path). Once you have done that you need to update the YAML frontmatter of your document so that it looks something like this:

---
title: "A title"
output: 
  pdf_document
  html_document
bibliography: sample_refs.bib
---

And if you are going to use a specific CSL you add another line after bibliography: sample_refs.bib specifying the path. Like this:

csl: apa.csl

Now we are ready to cite some sources. We can use standard markdown syntax for this (see here for more details). For example, we can cite the Flege article as a parenthetical reference using [@flege1995]:

The Speech Learning Model (Flege 1995) is pretty cool.

To render the above citation I typed this:

The Speech Learning Model [@flege1995] is pretty cool.

Note that the formatting of this blog uses numbered citations (see the References section at the bottom). Now I can also use in-text citations by typing @kuhl1992 (without the brackets):

Kuhl et al. (1992) has a great TED talk.

@kuhl1992 has a great TED talk. 

At the end of your document you need to add a section header called References:

# References

and then your bibliography will be generated automatically. You can download a working example to test this and use as a template here.

Flege, James E. 1995. “Second Language Speech Learning: Theory, Findings, and Problems.” Speech Perception and Linguistic Experience: Issues in Cross-Language Research 92: 233–77.
Kuhl, PK, KA Williams, F Lacerda, KN Stevens, and B Lindblom. 1992. “Linguistic Experience Alters Phonetic Perception in Infants by 6 Months of Age.” Science 255 (5044): 606–8.

References

Citation

For attribution, please cite this work as

Casillas (2019, Oct. 11). RAP Group: Automatic references in RMarkdown. Retrieved from https://rap-group.github.io/posts/2019-10-11-references_in_rmarkdown/

BibTeX citation

@misc{casillas2019automatic,
  author = {Casillas, Joseph V.},
  title = {RAP Group: Automatic references in RMarkdown},
  url = {https://rap-group.github.io/posts/2019-10-11-references_in_rmarkdown/},
  year = {2019}
}