Skip to main content

How to add citations on Markdown with Zotero and Pandoc

· loading · loading ·
Author
Ishmael

I take notes on Markdown with the Foam extension on VSCode. This is because I want to keep everything in one place, and it’s not just regular documents that I keep; sometimes I have .ipnyb files and .rmd as well. One of the obstacles I encountered is that if I write on Markdown, there needs to be a method for me to insert citations whenever I’d like. Markdown has a built-in footnotes feature, but it’s limited in the sense that it only allows superscript numbers and not other forms of in-text citations.

Here’s an example using Markdown’s footnotes:

Lorem ipsum dolor sit amet [^1]

[^1]: Rooks MG, Garrett WS. Gut microbiota, metabolites and host immunity. Nat Rev Immunol. 2016 May 27;16(6):341–52. doi:10.1038/nri.2016.42 PubMed PMID: 27231050; PubMed Central PMCID: PMC5541232.

Zotero is an open-source reference manager that works well with most word processors with a plugin, but for writing in Markdown, it isn’t as simple.

Technically, I could copy and paste references from Zotero directly through Create Bibliography from Item... and clicking Copy to Clipboard for each reference, but whoever has the time for that?

Why Pandoc?
#

Pandoc is super useful with its ability to convert .md to literally anything; you can convert it into pdf slides, html slides and properly formatted pdfs. It’s convenient to have a document converter so that I don’t need to create a separate notes file and a separate slides file.

Pandoc comes with --citeproc which will automatically format citations for you from a .bib file (which you can generate from Zotero).

How to do it
#

What you need
#

  • Markdown editor, though I personally used VSCode.
    • This offline editor is a good barebones alternative and is completely free.
  • Zotero with BetterBibTex plugin (any other reference manager that can export to .bib should otherwise work)
  • Pandoc

I’ll explain how to set these up.

Saving Citations
#

  1. Download Zotero.
  2. Setup the browser extension.
  3. Open an article. Let’s use this as an example.
  4. Click on the Zotero browser extension in your browser toolbar to save your citation.

Preparing.bib files
#

BetterBibTex on Zotero
#

Before you continue on, you need to install the BetterBibTex plugin. Follow the instructions on the installation page.

Preparing the bibliography
#

  1. Select the references you would like to include.
  2. Select Export items.
alt text
  1. If you tick Keep updated, your .bib file will automatically update itself when you add references.
  2. Check the location of your generated .bib file. Make sure it is in the same directory as your markdown file.

Adding citations to the Markdown file
#

Installing the Citation Picker
#

If you are using VSCode, you can use an extension that automatically inserts keys for you. Otherwise, you can manually insert keys.

Manually inserting keys
#

Open your .bib file. You should see something like this.

@article{rooksGutMicrobiotaMetabolites2016,
  title = {Gut Microbiota, Metabolites and Host Immunity},
  author = {Rooks, Michelle G. and Garrett, Wendy S.},
  year = 2016,
  month = may,
  journal = {Nature reviews. Immunology},
  volume = {16},
  number = {6},
  pages = {341--352},
  issn = {1474-1733},
  doi = {10.1038/nri.2016.42},
  urldate = {2026-08-09},
  abstract = {The microbiota --- the collection of microorganisms that live within and on all mammals --- provides crucial signals for the development and function of the immune system. Increased availability of technologies that profile microbial communities is facilitating the entry of many immunologists into the evolving field of host-microbiota studies. The microbial communities, their metabolites and components are not only necessary for immune homeostasis, they also influence the susceptibility of the host to many immune-mediated diseases and disorders. In this Review, we discuss technological and computational approaches for investigating the microbiome, as well as recent advances in our understanding of host immunity and microbial mutualism with a focus on specific microbial metabolites, bacterial components and the immune system.},
  pmcid = {PMC5541232},
  pmid = {27231050}
}

The key is the first string after @article.{ In this case, the key is rooksGutMicrobiotaMetabolites2016.

On your Markdown file, copy and paste the with the format [@key]:

Lorem ipsum dolor sit amet[@rooksGutMicrobiotaMetabolites2016] 

Changing the default citation format
#

Go to the CSL repository and find your preferred citation style in a .csl file. Download the file and put it in the same directory.

Linking your bibliography to your Markdown file
#

On your markdown file, make sure to insert the bibliography at the top of the Markdown file. CSL is optional if you want to change the citation style.

---
title: your title
author: you
bibliography: library.bib
csl: apa.csl
---

You should therefore have a .md document like this:

---
title: your title
author: you
bibliography: library.bib
csl: apa.csl
---

# Test
Lorem ipsum dolor sit amet[@rooksGutMicrobiotaMetabolites2016] 

Using pandoc’s citeproc
#

Using pandoc --citeproc can automatically generate citations.

  1. Download Pandoc.
  2. Open the Windows powershell terminal.
  3. Change the directory to the same directory with your .md, .bib file and .cls file.
cd C:/foldername
  1. Paste this command onto the powershell terminal.
pandoc --citeproc -s test.md -o test2.md -t markdown_strict

This generates a markdown file with rendered citations.

From this:

---
title: your title
author: you
bibliography: library.bib
csl: apa.csl
---

# Test
Lorem ipsum dolor sit amet[@rooksGutMicrobiotaMetabolites2016] 

To this:

# Test

Lorem ipsum dolor sit amet(Rooks & Garrett, 2016)

Rooks, M. G., & Garrett, W. S. (2016). Gut microbiota, metabolites and
host immunity. *Nature Reviews. Immunology*, *16*(6), 341–352.
<https://doi.org/10.1038/nri.2016.42>

Conversion from .md to .md will remove frontmatter and metadata.

If you want to generate a different file format with citations, pandoc can do it for you.

This generates a .docx file:

pandoc --citeproc -s test.md -o test.docx 

Or my favorite, generating a slideshow:

pandoc -t beamer test.md -o test_slides.pdf

Note: Any conversion involving pdfs require a pdflatex package. To enable this, download MikTeX and restart your computer.

And that’s it! Hope this is helpful.


Comments