One can create chapter bibliographies using BibLaTeX and straight .tex files, as shown in this MWE:
% main.tex
\documentclass{report}
\usepackage{natbib}
\usepackage{chapterbib}
\begin{document}
\include{chap1}
\include{chap2}
\end{document}
% chap1.tex
\chapter{one chapter}
text~\cite{paper1}
text~\cite{paper2}
\bibliographystyle{plainnat}
\bibliography{biblio}
% chap2.tex
\chapter{another chapter}
text~\cite{paper2, paper3}
\bibliographystyle{plainnat}
\bibliography{biblio}
% biblio.bib
@Article{paper1,
author = {John Smith},
title = {A title},
journal = {A Journal},
year = {2010}
}
@Article{paper2,
author = {John Doe},
title = {A paper},
journal = {Another journal},
year = {2009}
}
@Article{paper3,
author = {Yuppie Networking},
title = {My paper},
journal = {The best journal},
year = {2000}
}
The above files can be successfully compiled with the following script:
% compile.bash
#!/bin/bash
pdflatex main.tex
for auxfile in chap*.aux
do
bibtex `basename $auxfile .aux`
done
pdflatex main.tex
pdflatex main.tex
I wish to re-create the above functionality using knitr, BibLaTeX, and .Rnw files (not .tex files). Here's a small non-working example. (I know this example lacks R code chunks, but my use case will have R code chunks.)
% main.Rnw
\documentclass{report}
\usepackage{natbib}
\usepackage{chapterbib}
\begin{document}
\Sexpr{knitr::knit_child("chap1.Rnw")}
\Sexpr{knitr::knit_child("chap2.Rnw")}
\end{document}
% chap1.Rnw
\chapter{one chapter}
text~\cite{paper1}
text~\cite{paper2}
\bibliographystyle{plainnat}
\bibliography{biblio}
% chap2.Rnw
\chapter{another chapter}
text~\cite{paper2, paper3}
\bibliographystyle{plainnat}
\bibliography{biblio}
% knit_script.R
knitr::knit(input = "main.Rnw")
% compileRnw.bash
/usr/local/bin/Rscript knit_script.R
pdflatex main.tex
for auxfile in chap*.aux
do
bibtex `basename $auxfile .aux`
done
pdflatex main.tex
pdflatex main.tex
When I run the compileRnw.bash script, the resulting document lacks bibliographies.
What should main.Rnw and compileRnw.bash look like to create a document with chapter bibliographies from the .Rnw files?
Thanks in advance for any help!