Is there a way to add chapter bibliographies using bookdown?

前端 未结 2 1780
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 14:10

I am trying to write my PhD Thesis with bookdown and am mainly using pdf output. I have easily added a bibliography at the end of the document but would rather have a biblio

相关标签:
2条回答
  • 2020-12-24 14:44

    For a latex-specific solution see this post, i.e. use the sectionbib option for natbib and load the chapterbib package. You will need to edit The bookdown template to set the required options for natbib and tell bookdown to use your custom template in the yaml. See this post for more info on the bookdown template.

    I swear I saw instructions in the bookdown docs for doing this with gitbook format, but I can't seem to find it...

    EDIT I went and looked at an old bookdown project of mine. For gitbook output, you specify the following in _output.yml:

    bookdown::gitbook:
      split_by: chapter
      split_bib: yes
    

    Which will (you guessed it) split the bibliography by chapter. I'm actually a bit surprised that bookdown doesn't support the equivalent options for bookdown::pdf_book yaml options, but should be easy enough to do by setting up sectionbib/chapterbib in the LaTeX preamble.

    0 讨论(0)
  • 2020-12-24 14:56

    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 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."
    
    0 讨论(0)
提交回复
热议问题