### How to Integrate Zotero Citations with R Markdown

Monday, 25 February 2019

There are plenty of options for citation management, and everybody has their own favorite. My favorite is Zotero, and as a result I wanted to be able to integrate my Zotero library into R Markdown so I can cite articles using BibTeX files. A quick google search lead me down a (short) rabbit hole of how to integrate Zotero and R Markdown. Below are the simplified instructions in a one-stop shop.

2. Download the Better Bibtex (clicking the hyperlink will start the download) file into a folder that you know where it is. This is an add on for the Zotero Standalone software that was written by Emiliano Heyns, a faculty from the Netherlands in Mathematics.
3. In Zotero follow this path: Tools>Add-Ons>Gear Icon>Install Add-On From File…. Then go to the folder you installed the Better Bibtex file in. Select the Better Bibtex .xpi file, click “Install Now” in the Zotero pop-up, and follow the system setup.

### Step 2: Check your BibTeX keys

In Zotero you can change the BibTeX key if you wish by following this path in Zotero: Tools>Add-Ons and clicking on the “Options” button next to “Better BibTex for Zotero.” Then, edit the citation key format to whatever your preference is. Mine is: [auth:lower][year]. If you change the BibTeX key here you will need to refresh the BibTeX key: right-click a source, go to “Better BibTeX”, and click “Refresh BibTeX Key”. You can select your entire library and do this at once, but I have not discovered a way in the menu to auto-update all of them after changing the format of the BibTeX key.

### Step 3: Export your .bib file from Zotero to your directory in R

Now that you have your BibTeX keys set up you’re ready to cite. Put all works that you want to cite into the directory you are working out of in R. I do this by right-clicking a collection on the left-hand panel, selecting “Export Collection”, and make sure you select BibTeX format from the top dropdown menu. Then, simply place it in the directory when your file explorer pops up.

### Step 4: Set up a template that will use BibTeX keys for citation and format the header

Now that your .bib file is in your directory you need to have your rmarkdown document setup to cite. This is very simple. Your directory needs to include a .tex template that recognizes the “natbib” citation package. I use Fred Solt’s adaptation1 of Steven V. Miller’s template. I’m sure it will work on other templates, but your mileage may vary as I have only confirmed it works under these settings. But, this template works for pretty much all academic writing, so it should be versatile. The template must be in the directory you are working in, as the header bellow relies on it.

Lastly, the header of your .Rmd file needs to look like this:

---
title: "Test Citation"
author: "Christopher Junk"
date: "February 24, 2019"
output:
pdf_document:
citation_package: natbib
template: svm-latex-ms2.tex
bibliography: \dummy{r file.path(getwd(), list.files(getwd(), "bib\$"))}
---

Once all of this is done you are ready to cite. You can see the minimal requirements for setup needed for this to work on my GitHub page which is in my Website repository titled “Test_Citation.Rmd”.

### Bonus Section: Why Zotero is wonderful

Zotero has one major advantage that for Windows users I have found to be unmatched among other software for managing citations. That is the Chrome extension that directly extracts all information and the document itself from journal’s websites into Zotero with correct citation information. Simply add the extension to Chrome and click it when you are on an article/webpage you want to cite and it imports the citation and the document/URL automatically. This saves a couple of clicks for each citation you aquire, which when studying for qualifying exams can feel like a life saver.

Also, the Better BibTeX extension allows you to drag and drop a citation from the Zotero window into your Rmarkdown document. This seems wildly inefficient to me, and you still have to make sure your .bib file is in your directory for it to work. But, it is still a neat perk if you just cannot seem to type the BibTeX key correctly.

1. The .tex template is in the “Paper” folder and is called “svm-latex-ms2.tex”