EDIT_THIS ADD_JOURNAL ADD_ISSUE ADD_ARTICLE PUBLISH ?

Markdown bibliographic referencing workflow

article⁄Markdown bibliographic referencing workflow
contributor⁄
in issues⁄
abstract⁄This article outlines a workflow for incorporating bibliographic citations/references in Markdown documents, and exporting new documents with compiled bibliographies according to specific citation styles. All tools involved in this pipeline are free, open source, and cross platform.
keywords⁄guidemarkdown guide

1. Introduction

This guide outlines a workflow for incorporating bibliographic referencing (citations and references) in article⁄Markdown documents. The general idea is incorporating citation keys from a bibliography in a Markdown document, and then rendering citations and references by generating a new document against a bibliographic collection (bib file), and a citation style (csl file).
The steps described in the following are:

  1. Installing required tools and software;
  2. creating and exporting a bibliography collection with www⁄Zotero;
  3. using citation keys in a Markdown document;
  4. getting a citation style file;
  5. exporting new documents (e.g. PDF, DOCX, MD, etc.) with www⁄Pandoc, where citations and bibliographic references are formatted as per any specific citation style.

All tools involved in this workflow are free, open source, and cross-platform.


Note: A citation is used in the body of the text to refer to some external source. A reference is placed in the “References” or “Bibliography” section of the document providing the full information for a citation. Citations and references are formatted according to a citation style. For example, for an ‘author-year’ citation style like Chicago in-text citations look like: Khaled et al. (2018), or (Khaled et al. 2018). According to the same citation style, the reference for that citation is:

2. Requirements

  1. Some Markdown editor for authoring Markdown documents (see article⁄Markdown guide).
  2. www⁄Zotero – free and open source cross platform citation management software.
  3. www⁄Better Bibtex – “an extension for Zotero … that makes it easier to manage bibliographic data, especially for people authoring documents using text-based toolchains (e.g. based on www⁄LaTeX / www⁄Markdown”).
  4. www⁄Pandoc – “universal document converter”; command line tool for converting between different document formats (e.g. Markdown, PDF, DOCX, epub, etc.), and compiling bibliographic references.

2.1. Pandoc installation & upgrade

Pandoc is a command line tool that can be installed via a downloadable installer or through the terminal (see www⁄Installing pandoc). To check if pandoc is installed, and which version is installed, open a terminal and do:

pandoc --version

If pandoc is not recognized as a valid command, go ahead and install pandoc. Otherwise, upgrade pandoc to the latest release.

2.1.1. Installation

Installing via a terminal requires a package manager (e.g. www⁄Chocolatey for Windows, or www⁄Homebrew for macOS).

2.1.2. Upgrade

To upgrade www⁄Pandoc, if it was installed via an installer, download and run the latest installer.
If it was installed via terminal, it can be upgraded with a single command.

Verify the version of your Pandoc installation with the command:

pandoc --version

3. Create and export a bibliography collection

This section outlines how to create a bibliography collection in Zotero and export it to a bib file.

  1. In Zotero, create a new collection (titled say mycollection).
  2. Add to your collection all items you want to reference (by drag and drop to collection, or right click > Add to Collection).
  3. With www⁄Zotero and www⁄Better Bibtex installed, each entry in your Zotero database should have a unique citation key displayed at the top of its Info section (image below). Use this key as in @key (or @khaled2018) to cite this item in your Markdown document (§⁄further instructions below).
  4. To export your reference collection, select the collection, right-click > Export Collection, select Better BibLaTeX (uncheck all options; background export optional):
  5. Save your collection (e.g. mycollection.bib). The resulting file is a dictionary of citation keys, each with its own citation metadata (you can preview the file with a text editor).

Notes:

4. Using citation keys in Markdown documents

To add a citation in a Markdown file, just prefix a citation key (from Zotero) with the @ sign, as in @khaled2018.

Eventually, as described in the following steps, when the document is compiled (against a bibliography and a citation style), citation keys will be rendered according to the specified citation style, and a bibliographic reference will be added to the bibliography section of the document, per each unique cited document.

For author-year type citation styles:

5. Get a citation style file (CSL)

Use a provided citation style file (CSL), or download one from www⁄the official citation style repository (GitHub repository), or from the www⁄Zotero Style Repository (search interface).

Notes:

6. Gather files and paths

The next step is to gather all required files that will be used to compile a new document (Markdown document, citation style, and bibliography files) and make a note of their file paths. In addition, make sure that the path of any image referenced in the Markdown document is correct.

Good practices:

Notes:

7. Test Pandoc and Markdown document

Open a terminal:

8. Compile document with bibliographic references

To compile a new document including citations and bibliography add the options:

Pandoc option Description
--citeproc option for rendering citations/references
--csl option to specify citation style, followed by the path to the CSL file
--bib option to specify bibliography followed by the path to the bib file

To compile to PDF do:

pandoc "path/to/myarticle.md" --citeproc --bibliography "/path/to/mycollection.bib" --csl "/path/to/reference-style.csl" -o "/path/to/export-document.pdf"

Note: Conversions to PDF format require a LaTeX installation.


To compile to DOCX do:

pandoc "path/to/myarticle.md" --citeproc --bibliography "/path/to/mycollection.bib" --csl "/path/to/reference-style.csl" -o "/path/to/export-document.docx"

To compile to DOCX using a DOCX document as reference/template add the option:

--reference-doc="path/to/reference-document.docx"

Note: Conversions to DOCX format will use Footnotes, not Endnotes.


To compile to a new Markdown document (with references) do:

pandoc "path/to/myarticle.md" --citeproc --bibliography "/path/to/mycollection.bib" --csl "/path/to/reference-style.csl" -o "/path/to/export-document.md" --atx-headers --strip-comments --wrap=preserve -t markdown_mmd-citations --standalone

The additional options

Pandoc option Description
--atx-headers use common (ATX) formatting for Markdown headings
--strip-comments remove HTML comments(<!-- comment-->)
--wrap=preserve preserve line wrapping (by default pandoc will introduce line breaks)
-t markdown_mmd-citations -t (to) for specifying output format; markdown_mmd-citations for markdown_mmd format with citations (by default Markdown exports don’t render citations/bibliography)
--standalone export standalone (will also render frontmatter)

9. Common Pandoc compilation errors

Pandoc will not compile in case of errors. Some common errors include:

10. Pandoc command reference

See also:

A basic pandoc command example is:

pandoc "path/to/input.txt" -o "path/to/output.docx"

Additional options can be added to this command pattern to specify export parameters. Here is a brief list of some commonly used pandoc options (enclosing file paths inside quotation marks is optional):

Pandoc option Description
-i "path/to/input/file.ext" input file, followed by path to file including file extension (usually -i is omitted when input file is specified at the beginning of the command)
-o "/path/to/output/file.ext" output file, followed by path to file, including file extension
-f (from) for specifying input format, followed by option, see www⁄pandoc options manual
-t (to) for specifying output format, followed by option, see www⁄pandoc options manual
-s--standalone to produce standalone file, which will render title, abstract, keywords, using data from front matter of the Markdown document
-C--citeproc option to render citations; will expect bibliography and csl files
--bibliography "path/to/bibliography.bib" specify bibliography file
--csl "path/to/citation-style.csl" specify citation style file
--atx-headers use common (ATX) formatting for Markdown headings
--wrap=preserve option to preserve line wrapping for plain text output formats (other options: auto|none)
--strip-comments remove HTML comments (<!--a comment-->)
--no-highlight Disables syntax highlighting for code blocks and inlines

11. Specify bibliography placement

By default, www⁄pandoc will insert the bibliography at the end of the document. To force the bibliography to render at a specific location if needed – for example before endnotes or appendix sections – add the following to the target location for your bibliography:

... article body ...

# Bibliography 

::: {#refs}
:::

# Some other appendix
Lorem ipsum. 

or

... article body ...

# Bibliography 

<div id="refs"></div>

# Some other appendix
Lorem ipsum.