Skip to Main Content

Datalab: References in R Markdown with Zotero

Referencing in Markdown

It's possible to reference and produce reference lists in Markdown if you are using Zotero as your reference manager. In order to get this working there are a few prerequisites you will need to fulfill. We will show this in detail in the sections below but here ss a quick overview of what you need to make this work.

Create a working folder for all the files that need to work together. This folder needs to have the following files in the same place to get everything working:

  • Your R Markdown-file (.rmd)
  • Your Zotero-library in BibTex-format (.bib)
  • Your citation style file (.csl)

Zotero

  • You can use the plugin Better BibTex to cite and create reference lists in R Markdown
  • Set up your CitationKey in Zotero in Better BibTex references
  • Export your Zotero-library and place it in the same folder as your R Markdown document
  • Download the citation style you want to use from Zotero Style Repository. We recommend to use American Psychological Association 7th edition but you can use whichever you want.

Rstudio

  • Create a Markdown file in RStudio and save it.
  • Make sure the Pandoc package is installed
  • Make sure that the rbbt-package is installed

 

Setting up Zotero

Download Better BibTex

First of all we will need to download and install the Better BibTex-plugin to Zotero

Install Better BibTex in Zotero

The next step is to install the plugin to Zotero.

  • Go to Zotero and choose Tools -> Add-ons -> Click the Gear Icon -> Choose Install Add-on From File


     

Set Citation Key in Better BibTex in Zotero

  • The Citation key is how the citation will look in your markdown before you knit your document
  • Go to Edit -> Preferences -> Better BibTex
  • You can set your citation style in the field Citation key format. I prefer to use something similar to Author + Year (example Reve, 2017) which is used in APA and the code for this is auth.lower + year. This coding will be show citations on the form reve2017 (i.e. First Author starting with lower letter and the publication year) in your markdown.
  • You can of course choose another Citation Key if you want. Read more about this in the documentation.

Export your Zotero library as a .bib-file

  1. Choose the references you want to export. You can choose a specific group or the whole library


     
  2. Choose File -> Export Library... Choose the Format: Better BibTex -> Click Ok and save the .bib-file in the same directory as your RMarkdown file

 

Setting up RStudio

Prerequisites for RStudio

  • pandoc
  • rbbt 
  • tinytex::install_tinytex()

Install packages needed to make things work

install.packages("pandoc")

rbbt 

remotes::install_github("paleolimbot/rbbt")

tinytex

tinytex::install_tinytex() 

Working with Markdown in RStudio

Now it's time to create your markdown document in RStudio

  • Go to File -> New File -> R Markdown


     
  • Example of a Rmarkdown shown as Source view



     
  • Example of a RMarkdown shown in Visual view

Inserting citations and reference list

Citations can be added to your markdown in both source and visual mode by typing the @sign and and the citation key from Zotero. I have used the key format auth.lower + year in these examples.

Explanation Format Example
Add one citation in parenthesis, i.e. (Sternberg & Wagner, 1991) [@key] [@sternberg1991]
Add several citations in same parenthesis, i.e. (Gladwell, 2022; Zuboff, 2019; Selznick, 1957) [@key1; @ke2; @key3] [@gladwell2002; @zuboff2019; @selznick1957]
Authors in text and the publishing year in parenthesis, i.e. Sternberg & Wagner (1991) @key @sternberg1991

It's also possible to add citations in visual mode by selecting Insert -> Citation
 

 

Creating the reference list

The reference list will be populated when you knit your document. The references will be inserted in the end of the document.

 

Error messages

Error reading bibliography file example2.bib:
(line 12491, column 33):
unexpected '\8217'
Error: pandoc document conversion failed with error 25
Execution halted

This error indicates that there are some characters in your .bib-file that will need to be changed. This could be ', -, or something else.