# 5 Citations and bibliographies

R markdown can add citations and a bibliography to a document. Use the bibliography field in the YAML to point to the bibliography file.

---
title: "My Manuscript"
output: html_document
bibliography: reproducibility.bib
---

The bibliography file can be in one of several formats, including bibTeX (*.bib) and endnote (*.enl). Any reference manager should be able to export to a bibTeX file.

## 5.1 Bibliography placement

By default, the bibliography is placed at the end of the document. It is a good idea to end the R markdown file with an appropriate header.

# ConclusionsR markdown is cool.# References {-}

The {-} stops R markdown from numbering the reference section header.

If you want to move the bibliography, for example because you need to finish with an appendix, you need to use this bit of magic. You also need to use the output format bookdown::html_document2 in the YAML.

# ConclusionsR markdown is very cool.# References {-}<div id="refs"></div># Appendix A

## 5.2 Citations

Use the citation keys from the bibliography to cite the reference in the text. So to cite the reference with the citation key “Cooper2017” we would use.

Reproducible code [@Cooper2017] ...
@Cooper2017 discuss ... 

The output depends on the csl file. Using the default author-date style gives

Reproducible code (Cooper and Hsing 2017)
Cooper and Hsing (2017) discuss …

In the visual editor, type @ to get suggestions from the bibliography. This is not so useful if you have many references. Alternatively, summon the import anything tool by typing ctrl + / and opening the citation menu, which lets you search for references in the bibliography or add new ones.

### Exercise

Add a citation to Töpper et al (2018) to the introduction to your R Markdown document as an example of climate change impacts on terrestrial ecosystems.

## 5.3 Citation style

If you don’t like the default citation style, or you want to submit a manuscript to a journal with an overly pedantic editor, you can change the citation style by specifying a csl file in the YAML.

---
title: "My Manuscript"
output: html_document
bibliography: reproducibility.bib
csl: plos-one.csl
---

The csl (Citation Style Language) file defines the citation style. There are thousands of citation style available from http://zotero.org/styles (there are specific formats for publishers and many journals).

### Exercise

Change the csl file from nordic-journal-of-botany.csl to plos.cls. Knit the document to see how the citations change in the text and in the bibliography.

## 5.4 Citation problems

Unfortunately, there can be problems with the bibliography and citation. Most can be solved by googling the error message. A few common problems are addressed here.

### 5.4.1 Incorrect capitalisation

Many CLS styles convert the title to title case (i.e. only first letter of the first word is capitalised). This can be annoying as names (people, countries, species) can be given in the wrong case.

A paper with the title

Population ecology of polar bears at Svalbard, Norway.

may appear as

Population ecology of polar bears at svalbard, norway.

This can be fixed by editing the bibtex file, putting braces around the word with capitals that need to be kept.

Population ecology of polar bears at {Svalbard}, {Norway}.

### 5.4.2 Accents and other special characters

The tool-chain that processes R markdown files can sometimes fall over with accented characters or other diacritic marks in the references. The error message will look something like this

! Package inputenc Error: Unicode character ́ (U+0301)
(inputenc)                not set up for use with LaTeX.

One solution is to edit the bibtex file to replace the accented character with an escaped code. For example, the “í” in “Martínez” could be replaced by “\’{i}”.