有没有办法使用 bookdown 添加章节书目? [英] Is there a way to add chapter bibliographies using bookdown?
问题描述
我正在尝试用 bookdown 写我的博士论文,并且主要使用 pdf 输出.我很容易在文档的末尾添加了一个参考书目,但我宁愿在每章的末尾都有一个参考书目.我尝试使用允许这样做的 LaTeX 包调整 .tex 输出,但这与 bookdoown 的默认设置相冲突.有没有办法调整 .yaml 选项来启用它?
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 bibliography at the end of each chapter. I have tried adjusting the .tex output with LaTeX packages that allow this but this fights with the bookdoown defaults. Is there a way of adapting the .yaml options to enable this?
推荐答案
对于 HTML 输出,默认使用每章参考书目.对于 PDF 输出,我发现最好将 LaTeX 包 biblatex
与 biber
一起使用.由于 RStudio 不了解 biber,因此最好安装类似 latexmk
的工具,并通过 Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")
配置 RStudio 以使用该工具.这些程序可能必须单独安装,例如在 Debian/Ubuntu/...
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
对于配置 biblatex
,https://tex.stackexchange.com/questions/199336/biblatex-reference-both-by-chapter-and-at-the-end-of-the-book 是合适的.
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.
最后在_output.yml
中需要进行以下设置:
In the end the following settings are necessary in _output.yml
:
bookdown::pdf_book:
citation_package: biblatex
在Index.Rmd
:
biblio-style: authoryear
biblatexoptions: [refsegment=chapter]
在每一章的结尾:
printbibliography[segment= herefsegment,heading=subbibliography]
没有必要转义这个原始的 LaTeX 命令,因为 pandoc
会忽略其他输出格式的此类命令.
There is no need to escape this raw LaTeX command, since pandoc
ignores such commands for other output formats.
您可以在
- https://github.com/rstub/bookdown-chapterbib
- https://rstub.github.io/bookdown-chapterbib/
- https://rstub.github.io/bookdown-chapterbib/bookdown-章节书目.pdf
我设法通过以下步骤获得了带有 PDF 输出的章节书目:
I managed to get chapter bibliographies with PDF output using the following steps:
- 从 https://github.com/rstudio/bookdown-demo 的副本开始
- 将
<R-library-path>/rmarkdown/rmd/latex/default-1.17.0.2.tex
作为book.tex
复制到工作目录李> - 更新
book.tex
以使用 LaTeX 包bibunits
(差异如下) - 更新
_output.yml
以将book.tex
引用为template
(差异如下) - 在
index.Rmd
中设置 YAML 选项(差异如下) - 在一些
Rmd
文件中添加代码以编写putbib
命令(差异如下)
- 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 writeputbib
command (diff below)
在这些更改之后,可以生成一个 PDF 文件,但缺少所有引用,因为 bookdown
不知道生成的 bu?.aux
文件.执行 bibtex bu1
和 bibtex bu2
后,通过 bookdown
复制 PDF 文件会生成带有章节书目的 PDF.最好使用 Makefile 自动执行此步骤.
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}
ibliographystyle{$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$
egin{document}
+ibliographyunit[chapter]
$if(title)$
maketitle
$endif$
以及 bookdown-sample
中文件的差异:
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."
这篇关于有没有办法使用 bookdown 添加章节书目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!