Is there a way to add chapter bibliographies using bookdown?
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
- https://github.com/rstub/bookdown-chapterbib
- https://rstub.github.io/bookdown-chapterbib/
- https://rstub.github.io/bookdown-chapterbib/bookdown-chapterbib.pdf
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
asbook.tex
to the working directory - update
book.tex
to use the LaTeX packagebibunits
(diff below) - update
_output.yml
to refer tobook.tex
astemplate
(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."
Include after_body in toc with bookdown
I managed to do this by changing the \textbf to \chapter, and include this in the preamble.tex:
\usepackage{fancyhdr}
\pagestyle{fancy}
\setlength{\headheight}{13.6pt} % as requested by fancyhdr's warning
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\makeatletter
\renewcommand{\chaptermark}[1]{%
\if@mainmatter
\markboth{Chapter \thechapter{}: #1}{}%
\else
\markboth{#1}{}%
\fi
}
As taken from this post
How to put citations (using BibTex) at the last chapter only (instead of at the end of every chapters)?
I assume you areusing the default bookdown::gitbook
for HTML output. In that case you can adjust the split_bib
variable via YAML headers, typically in _output.yml
:
bookdown::gitbook:
split_bib: no
See ?bookdown::gitbook
for more options.
Gitbook chapter bibliography not in alphabetical order
$(function(){ var elems = $('#refs').children('div').remove(); elems.sort(function (a, b) { return b.id > a.id ? -1 : 1; }); $('#refs').append(elems);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="refs" class="references"> <div id="ref-Goloboff2016"> <p>Goloboff, P. A., and S. A. Catalano. 2016: TNT version 1.5, including a full implementation of phylogenetic morphometrics. Cladistics 32:221–238.</p> </div> <div id="ref-Goloboff1999"> <p>Goloboff, P. 1999: Analyzing large data sets in reasonable times: solutions for composite optima. Cladistics 15:415–428.</p> </div> <div id="ref-Nixon1999"> <p>Nixon, K. C. 1999: The Parsimony Ratchet, a new method for rapid parsimony analysis. Cladistics 15:407–414.</p> </div> <div id="ref-Goloboff1997"> <p>Goloboff, P. A. 1997: Self-weighted optimization: tree searches and character state reconstructions under implied transformation costs. Cladistics 13:225–245.</p> </div></div>
Related Topics
Clustering List for Hclust Function
More Efficient Means of Creating a Corpus and Dtm with 4M Rows
Select Unique Values with 'Select' Function in 'Dplyr' Library
Change the Index Number of a Dataframe
How to Convert Ensembl Id to Gene Symbol in R
How Does the Removesparseterms in R Work
Hiding Personal Functions in R
How to Create Textarea as Input in a Shiny Webapp in R
Comparison Between Dplyr::Do/Purrr::Map, What Advantages
R Plot Color Combinations That Are Colorblind Accessible
R Sequence of Dates with Lubridate
Difference Between As.Data.Frame(X) and Data.Frame(X)
Loop Over Rows of Dataframe Applying Function with If-Statement
Read Lines by Number from a Large File