How to automatically create BibTex citations for R packages in knitr file?

寵の児 提交于 2019-12-20 17:34:32

问题


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.



来源:https://stackoverflow.com/questions/19432448/how-to-automatically-create-bibtex-citations-for-r-packages-in-knitr-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!