Citations in markdown using knitr
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I am finding myself more and more drawn to markdown rather then tex/Rnw as my standard format (not least of which is the ease of displaying the files on github, particularly now that we have automatic image uploading). One thing I miss from latex is the citation commands. (I understand these can be provided to markdown via Pandoc, but I’d like to simply have to knit the document, and not then run it through pandoc, latex, or another interpreter). I’ve taken a little whack at generating in-text citations using knitr and other R tools.
DOI Approach
I’ve put some simple functions in a knitcitations
package. The functions use the crossref API to grab citation information given a doi, so I don’t have to generate a bibtex file for papers I’m reading, (inspired by the kcite package for WordPress). One can grab my package from github
library(devtools) install_github("knitcitations", "cboettig")
and load the package
require(knitcitations)
Then we can generate a citation given a doi with the ref
function:
Bibtex Approach
If we have a bibtex file, we can use this for the citations as well. Let’s start off by getting ourselves a bibtex file from some of R’s packages:
library(bibtex) write.bib(c('bibtex', 'knitr', 'knitcitations'), file="example.bib")
Now we can simply read in the bibtex files:
biblio <- read.bib("example.bib") biblio[[1]]
Francois R (2011). bibtex: bibtex parser. R package version 0.3–1/r332,
(This would be much more awesome if we could generate keys on write.bib and use those bibtex keys, instead of the index value, [[1]]
, to generate the citation.)
Using the inline citations
Now that we can get citation information from bibtex files or dois, we need a way to insert these citations into the text. I’ve written a simple citep
print inline citations that would just use a given shortened format (e.g. author-year) and add the citation to a works_cited
object, which we could then use to generate the full citation information at the end. We can generate inline citations by giving a doi, bibentry object, or a list thereof, into inline knitr code block. Thus we can use the line citep("10.1111/j.1461-0248.2005.00827.x")
to generate a parenthetical citation, (Halpern et. al. 2006). We can alos generate textual citations with citet(biblio[1])
, such as Francois, (2011). Parenthetical citations can take more than one entry, such as citep[biblio[2:3]
, which produces (Xie, 2012; Boettiger, 2012).
Generating the final bibliography
As we go along adding inline citations, R stores the list of citation info. Then at the end of the document, use this command to print the bibliography generated by the use of our inline citations.
bibliography()
Halpern B, Regan H, Possingham H and McCarthy M (2006). “Accounting for uncertainty in marine reserve design.” Ecology Letters, 9. ISSN 1461-023X,
Francois R (2011). bibtex: bibtex parser. R package version 0.3–1/r332,
Xie Y (2012). knitr: A general-purpose package for dynamic report generation in R. R package version 0.4.1,
Boettiger C (2012). knitcitations: Citations for knitr markdown files. R package version 0.0–1.
I hope to add markup to format this a bit more nicely later. For instance, we want the links to appear as real links. Additionally, we may want to add markup around the citations, such as the reason for the citation into the link using the Citation Typing Ontology.
This entry was, of course, produced in knitr with my knitcitations package. See the original source.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.