Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use custom citation style in markdown using YAML header

I am trying to use a custom citation style in a markdown file, but the citation uses the default (Chicago) style each time I knit. I have tried changing the output format from a JS reveal presentation to an HTML document to a PDF document, but it still does not work. I am using the knitcitations package to cite using the document's DOI, and the bibliography() function to write the bibliography. I have also tried using the apa.csl style found on Zotero, yet the citation is still done in the default styple. The apa.csl file is stored in the same folder as the file that I am trying to use citations in, as is the newbiblio.bib file, in which I have stored the bibliographical information for the item I want to cite.

Below is my markdown code:

---
title: "htmlcitetest"
citation_package: natbib
csl: "apa.csl"
output:
  pdf_document:
    pandoc_args: ["--natbib"]
biblio-style: unsrt
bibliography: newbiblio.bib
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(bibtex)
library(knitcitations)
options("citation_format" = "pandoc")
library(RefManageR)
cleanbib()
```

## R Markdown

- This is a citation [^1]

[^1]: `r citet("10.1098/rspb.2013.1372")`


```{r, message=FALSE}
bibliography()
```

This link (http://rmarkdown.rstudio.com/authoring_bibliographies_and_citations.html) says that I should be able to format my YAML header like this:

---
title: "Sample Document"
output: html_document
bibliography: newbiblio.bib
csl: apa.csl
---

However, when I do that, the file knits to a markdown (.md) file, but it is not processed into the output. I recieve this error:

pandoc-citeproc: 23:3-23:10: Expected end element for: Name {nameLocalName = "category", nameNamespace = Just "http://purl.org/net/xbiblio/csl", namePrefix = Nothing}, but received: EventEndElement (Name {nameLocalName = "info", nameNamespace = Just "http://purl.org/net/xbiblio/csl", namePrefix = Nothing})
pandoc: Error running filter        /Applications/RStudio.app/Contents/MacOS/pandoc/pandoc-citeproc
Filter returned error status 1
Error: pandoc document conversion failed with error 83
Execution halted

The contents of my .bib file are:

@Article{Boettiger_2013,
  doi = {10.1098/rspb.2013.1372},
  url = {http://dx.doi.org/10.1098/rspb.2013.1372},
  year = {2013},
  month = {jul},
  publisher = {The Royal Society},
  volume = {280},
  number = {1766},
  pages = {20131372--20131372},
  author = {C. Boettiger and A. Hastings},
  title = {No early warning signals for stochastic transitions: insights from large deviation theory},
  journal = {Proceedings of the Royal Society B: Biological Sciences},
}

I also do not understand why the biblio-style option in the YAML header does not to do anything. Essentially, all I need is a way to use a custom citation style I have already made with a markdown document. Any help would be greatly appreciated!

like image 978
J. Carew Avatar asked Jun 28 '16 13:06

J. Carew


1 Answers

Without a reproducible example, it is hard to know exactly what is happening, but it looks like you are mixing two different configurations.

Method 1: Specifying a custom CSL file

The method of using a CSL file only works if you are using pandoc-citeproc. For example, I have downloaded the IEEE style, and saved it in the same directory as my RMarkdown file as ieee.csl. This MWE builds a separate bibliography file:

---
output: pdf_document
bibliography: test.bib
csl: ieee.csl
---

```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```

Some ref [@R-knitr]

Some again [@R-knitr]

Another ref [@R-rmarkdown]

# References

enter image description here

Method 2: Specifying styles in Natbib

If you want to use natbib to build the citations and bibliography, you have to use the biblio-style option. This following example should work without downloading anything:

---
output: 
  pdf_document:
    citation_package: natbib
bibliography: test.bib
biblio-style: humannat
---

```{r}
knitr::write_bib(x = c("knitr", "rmarkdown") , file = "test.bib")
```

Some ref [@R-knitr]

Another ref [@R-rmarkdown]

# References

enter image description here

Unless you have a particular reason, I would probably go down the route of using pandoc-citeproc and a csl file. It integrates well with the RMarkdown world. Using Natbib just gets a bit more confusing, and from my experience is more prone to throwing errors.

like image 195
Michael Harper Avatar answered Nov 18 '22 22:11

Michael Harper