How to automatically create BibTex citations for R

2019-03-18 02:37发布

问题:

I am not sure whether this an R, LaTeX, or BibTex problem.

I am trying to automatically generate a .bib file containing citations for R packages and then list them at the end. I am able to generate the BibTex file and I don't see anything wrong with the BibTex file, but the entries don't appear when I compile the PDF.

I'm not sure if R is not producing a BibTex file correctly, if some LaTeX syntax is wrong, or if the BibTex file needs to be pre-compiled or whatever. I noticed that \bibliography{NOT A REAL FILENAME} will produce a References section without complaining, but I don't think that is the problem.

Minimal working example:

\documentclass[10pt]{amsart}
\usepackage[margin=1in, headheight=20pt, footskip=20pt]{geometry}

\begin{document}

<<label='Create References'>>=
require(knitr) # Needed for write_bib()

# Load some packages to the session:
require(xtable)
require(ggplot2)

# Select packages to cite:
citPkgs <- names(sessionInfo()$otherPkgs)
# Write the bibtex file:
write_bib(citPkgs, file="R-Pckgs.bib")
@

\nocite{*}
\bibliographystyle{plain}
\bibliography{R-Pckgs.bib}

\end{document}

Any help or suggestions would be appreciated.

回答1:

Just replace \bibliography{R-Pckgs.bib} with \bibliography{R-Pckgs}, and it should work fine.

On Windows 7, with an up-to-date MikTeX installation and current R and R packages, the following worked:

  1. Put your reproducible example in a file named "eg.Rnw" and edit to remove the extraneous ".bib"
  2. Launch R and navigate to the directory in which "eg.Rnw" is located.
  3. Do library(knitr); knit2pdf("eg.Rnw")

Note: There are obviously many workflows for going from *.Rnw to *.pdf, but if you want to use knit2pdf() (at least), make sure that you run it from the directory containing the *.Rnw to be processed.



回答2:

Add a \nocite{dummycite} to your document - to create a citation so that the bibliography is printed.

Note that this is obsolete if you already have other citation in your document.