knitr 子文档中的参考书目

Bibliography in knitr child documents

我在 knitr 子文档中包含参考书目时遇到问题。我希望能够在子文档中引用我的主要参考书目中的文章,但参考书目出现在主要文档之后,而不是在子文档之后。如果我只在主文档中包含 \bibliography 命令,则子文档中的引用不会被正确解析。示例:

main.Rnw:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\begin{document}
This is the main doc.

<<child-demo, child='child.Rnw'>>=
@
\bibliography{mylib}
\end{document}

child.Rnw:

This is the child \cite{myref}.

mylib.bib:

@article{myref,
 title = {frobnosticating froo filters}
 volume = {21},
 journal = {Frobnification},
 author = {John Q. Smith}
 month = jan,
 year = {2004}
}

我的 compile 脚本包含:

#!/usr/bin/env Rscript
library(knitr)
knit('main.Rnw', tangle=TRUE)
knit('main.Rnw', tangle=FALSE)
for ( i in c(1,2,3)) {
  system('pdflatex main')
  system('bibtex main')
}

运行 compile 产生:

如何使子文档包含主参考书目中的参考文献?

首先,您在 mylib.bib:

中遗漏了几个逗号
@article{myref,
 title = {frobnosticating froo filters},
 volume = {21},
 journal = {Frobnification},
 author = {John Q. Smith},
 month = {jan},
 year = {2004}
}

那你没有指定参考书目风格:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\begin{document}
This is the main doc.

<<child-demo, child='child.Rnw'>>=
@
\bibliography{mylib}
\bibliographystyle{plain}
\end{document}