In-text citations
With {knitcitations}
, you use R commands to both create
an entry in the .bib
file as well as create the in-text
citation.
Cite by DOI
You can cite an article by its DOI, using either citep()
or citet()
, and the full citation information will be
gathered automatically. For example, the inline command
`r citep("10.1890/11-0011.1")`
will do two things:
create an (Author[s] Year)
inline citation that
looks like “(Abrams et al.
2012)”;
create the following BibTeX reference that can
be cited elsewhere using [@Abrams_2012]
or
@Abrams_2012
:
@Article{Abrams_2012,
doi = {10.1890/11-0011.1},
url = {https://doi.org/10.1890/11-0011.1},
year = {2012},
month = {feb},
publisher = {Wiley},
volume = {93},
number = {2},
pages = {281--293},
author = {Peter A. Abrams and Lasse Ruokolainen and Brian J. Shuter and Kevin S. McCann},
title = {Harvesting creates ecological traps: consequences of invisible mortality risks in predator{\textendash}prey metacommunities},
journal = {Ecology},
}
On the other hand, the inline command
`r citet("10.1098/rspb.2013.1372")`
will do two things:
create an Author[s] (Year)
inline citation that
looks like “Boettiger and Hastings
(2013)”;
create the following BibTeX reference that can
be cited elsewhere using [@Boettiger_2013]
or
@Boettiger_2013
:
@Article{Boettiger_2013,
doi = {10.1098/rspb.2013.1372},
url = {https://doi.org/10.1098/rspb.2013.1372},
year = {2013},
month = {sep},
publisher = {The Royal Society},
volume = {280},
number = {1766},
pages = {20131372},
author = {Carl Boettiger and Alan Hastings},
title = {No early warning signals for stochastic transitions: insights from large deviation theory},
journal = {Proceedings of the Royal Society B: Biological Sciences},
}
Cite BibTeX objects directly
We can also cite BibTeX objects directly, such as
those that R provides for citing packages using the
citation()
function. So, for example, here are the
citations for the {knitr}
package
citation("knitr")
## To cite package 'knitr' in publications use:
##
## Xie Y (2025). _knitr: A General-Purpose Package for Dynamic Report
## Generation in R_. R package version 1.50, <https://yihui.org/knitr/>.
##
## Yihui Xie (2015) Dynamic Documents with R and knitr. 2nd edition.
## Chapman and Hall/CRC. ISBN 978-1498716963
##
## Yihui Xie (2014) knitr: A Comprehensive Tool for Reproducible
## Research in R. In Victoria Stodden, Friedrich Leisch and Roger D.
## Peng, editors, Implementing Reproducible Computational Research.
## Chapman and Hall/CRC. ISBN 978-1466561595
##
## To see these entries in BibTeX format, use 'print(<citation>,
## bibtex=TRUE)', 'toBibtex(.)', or set
## 'options(citation.bibtex.max=999)'.
We can use the following inline command
`r citep(citation("knitr"))`
to generate the 3 BibTeX objects as
@InCollection{Xie_2014,
booktitle = {Implementing Reproducible Computational Research},
editor = {Victoria Stodden and Friedrich Leisch and Roger D. Peng},
title = {knitr: A Comprehensive Tool for Reproducible Research in {R}},
author = {Yihui Xie},
publisher = {Chapman and Hall/CRC},
year = {2014},
note = {ISBN 978-1466561595},
url = {http://www.crcpress.com/product/isbn/9781466561595},
}
@Book{Xie_2015,
title = {Dynamic Documents with {R} and knitr},
author = {Yihui Xie},
publisher = {Chapman and Hall/CRC},
address = {Boca Raton, Florida},
year = {2015},
edition = {2nd},
note = {ISBN 978-1498716963},
url = {https://yihui.org/knitr/},
}
@Manual{Xie_2020,
title = {knitr: A General-Purpose Package for Dynamic Report Generation in R},
author = {Yihui Xie},
year = {2020},
note = {R package version 1.30},
url = {https://yihui.org/knitr/},
}
and also create the formatted in-text citation
(Xie 2014, 2015, 2025).
We can now cite the package with normal BibTeX
entries and pandoc will correctly avoid duplicating the author’s name.
For example,
[@Xie_2014; @Xie_2015]
will produce a formatted in-text citation
(Xie 2014, 2015).
Similarly, we can cite references that are already included in our
.bib
file using the standard notation, such that
[@Boettiger_2013]
or @Boettiger_2013
will render as
(Boettiger and Hastings 2013) or Boettiger and Hastings (2013).
Re-using BibTeX keys
When citep()
or citet()
are called, they
automatically generate a BibTeX key in the format
AuthorLastName_Year
. We can then use this key to cite a
reference without remembering its DOI. For example,
`r citep("Abrams_2012")`
will simply create the citation
(Abrams et al. 2012).
Creating the References section
Creating the References section is straightforward
with {knitcitations}
.
At the end of your document, insert a code block with a call to
write.bibtex(file = "filename.bib")
, where
filename.bib
is the name of the .bib
file to
which you would like the references written.
The function will then create the reference cited section using the
formatting specified in the .csl
file supplied in the
document’s YAML.
For example, we can generate the references cited section from the
citations in this tutorial with the following:
## create ref cited section
write.bibtex(file = "references.bib")