For HTML output the default is to use per-chapter bibliographies. For PDF output, I have found it is best to use the LaTeX package biblatex together with biber. Since RStudio does not know about biber, it is best to install a tool like latexmk and configure RStudio to use that via Sys.setenv(RSTUDIO_PDFLATEX = "latexmk"). These programs might have to be installed separately, e.g. on Debian/Ubuntu/...
sudo apt-get install texlive-bibtex-extra biber latexmk
For configuring biblatex the solution provided at https://tex.stackexchange.com/questions/199336/biblatex-reference-both-by-chapter-and-at-the-end-of-the-book is appropriate.
In the end the following settings are necessary in _output.yml:
bookdown::pdf_book:
citation_package: biblatex
In Index.Rmd:
biblio-style: authoryear
biblatexoptions: [refsegment=chapter]
At the end of every chapter:
\printbibliography[segment=\therefsegment,heading=subbibliography]
There is no need to escape this raw LaTeX command, since pandoc ignores such commands for other output formats.
One can see the entire solution at
Original solution
I managed to get chapter bibliographies with PDF output using the following steps:
- Start with a copy of https://github.com/rstudio/bookdown-demo
- copy
<R-library-path>/rmarkdown/rmd/latex/default-1.17.0.2.tex as book.tex to the working directory
- update
book.tex to use the LaTeX package bibunits (diff below)
- update
_output.yml to refer to book.tex as template (diff below)
- set YAML options in
index.Rmd (diff below)
- add code to some
Rmd files to write \putbib command (diff below)
After these changes, a PDF file could be produced, but all references where missing, since bookdown does not know about the generated bu?.aux files. After executing bibtex bu1 and bibtex bu2, reproducing the PDF file via bookdown produced a PDF with chapter bibliographies. It is probably best to automate this step with Makefile.
Here the diff between the templates:
$ diff -u /usr/local/lib/R/site-library/rmarkdown/rmd/latex/default-1.17.0.2.tex book.tex
--- /usr/local/lib/R/site-library/rmarkdown/rmd/latex/default-1.17.0.2.tex 2017-12-11 19:14:54.643867696 +0100
+++ book.tex 2018-01-16 11:43:46.182542634 +0100
@@ -93,8 +93,11 @@
\fi
$endif$
$if(natbib)$
-\usepackage{natbib}
+\usepackage[$natbiboptions$]{natbib}
\bibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
+\usepackage{bibunits}
+\defaultbibliographystyle{$if(biblio-style)$$biblio-style$$else$plainnat$endif$}
+\defaultbibliography{$for(bibliography)$$bibliography$$sep$,$endfor$}
$endif$
$if(biblatex)$
\usepackage$if(biblio-style)$[style=$biblio-style$]$endif${biblatex}
@@ -235,6 +238,7 @@
$endfor$
\begin{document}
+\bibliographyunit[\chapter]
$if(title)$
\maketitle
$endif$
And the diff of the files from bookdown-sample:
$ git diff
diff --git a/01-intro.Rmd b/01-intro.Rmd
index 6b16e73..1a5f9de 100644
--- a/01-intro.Rmd
+++ b/01-intro.Rmd
@@ -19,3 +19,5 @@ knitr::kable(
```
You can write citations, too. For example, we are using the **bookdown** package [@R-bookdown] in this sample book, which was built on top of R Markdown and **knitr** [@xie2015].
+
+`r if (knitr:::is_latex_output()) '\\putbib'`
diff --git a/02-literature.Rmd b/02-literature.Rmd
index 00745d0..983696e 100644
--- a/02-literature.Rmd
+++ b/02-literature.Rmd
@@ -1,3 +1,6 @@
# Literature
Here is a review of existing methods.
+[@R-knitr]
+
+`r if (knitr:::is_latex_output()) '\\putbib'`
diff --git a/_output.yml b/_output.yml
index 342a1d6..cc8afb1 100644
--- a/_output.yml
+++ b/_output.yml
@@ -14,4 +14,5 @@ bookdown::pdf_book:
latex_engine: xelatex
citation_package: natbib
keep_tex: yes
+ template: book.tex
bookdown::epub_book: default
diff --git a/index.Rmd b/index.Rmd
index 4e21b9d..2fdb813 100644
--- a/index.Rmd
+++ b/index.Rmd
@@ -7,6 +7,8 @@ output: bookdown::gitbook
documentclass: book
bibliography: [book.bib, packages.bib]
biblio-style: apalike
+natbiboptions: sectionbib
+graphics: yes
link-citations: yes
github-repo: rstudio/bookdown-demo
description: "This is a minimal example of using the bookdown package to write a book. The output format for this example is bookdown::gitbook."