我正在尝试使用bookdown编写我的博士论文,主要使用pdf输出。我很容易在文档末尾添加了参考文献,但我更想在每个章节末尾添加参考文献。我已经尝试了使用LaTeX包调整.tex输出以允许此操作,但这与bookdown的默认设置冲突。有没有一种适应.yaml选项以实现此目的的方法?
我正在尝试使用bookdown编写我的博士论文,主要使用pdf输出。我很容易在文档末尾添加了参考文献,但我更想在每个章节末尾添加参考文献。我已经尝试了使用LaTeX包调整.tex输出以允许此操作,但这与bookdown的默认设置冲突。有没有一种适应.yaml选项以实现此目的的方法?
对于HTML输出,默认情况下使用每章参考文献。对于PDF输出,我发现最好使用LaTeX包biblatex
以及biber
。由于RStudio不知道biber,最好安装像latexmk
这样的工具,并通过Sys.setenv(RSTUDIO_PDFLATEX = "latexmk")
配置RStudio使用它。这些程序可能需要单独安装,例如在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 。在最后需要在 _output.yml
中进行以下设置:
bookdown::pdf_book:
citation_package: biblatex
在 Index.Rmd
文件中:biblio-style: authoryear
biblatexoptions: [refsegment=chapter]
每个章节结尾处:
\printbibliography[segment=\therefsegment,heading=subbibliography]
这个原始的 LaTeX 命令不需要转义,因为 pandoc
会忽略其他输出格式中的这些命令。
完整的解决方案可以在以下网址中查看:
我使用以下步骤成功地获得了带有 PDF 输出的章节参考文献:
<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
命令(请参见下面的差异)进行这些更改后,可以生成 PDF 文件,但是所有引用都会丢失,因为 bookdown
不知道生成的 bu?.aux
文件。 执行 bibtex bu1
和 bibtex bu2
后,通过 bookdown
重新生成 PDF 文件可以得到带有章节参考文献的 PDF。最好使用 Makefile 自动化执行此步骤。
以下是模板之间的差异:
$ 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$
文件的差异来自于 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."
针对LaTeX的特定解决方案,请参见this post,即使用natbib
的sectionbib
选项并加载chapterbib
包。您需要编辑bookdown模板以设置natbib
所需的选项,并告诉bookdown使用您的自定义模板yaml。有关bookdown模板的更多信息,请参见this post。
我发誓在bookdown文档中看到过使用gitbook格式进行此操作的说明,但我似乎找不到它...
编辑 我去看了我的一个旧的bookdown项目。对于gitbook
输出,您需要在_output.yml
中指定以下内容:
bookdown::gitbook:
split_by: chapter
split_bib: yes
这将(你猜对了)按章节拆分参考文献。我实际上有点惊讶于bookdown不支持bookdown::pdf_book
yaml选项的等效选项,但是通过在LaTeX前言中设置sectionbib
/chapterbib
应该很容易做到。
chapterbib
希望每个章节都是一个包含bibliographystyle
和\bibliography
命令的\include
文件,而bookdown生成单个.tex文件。 - mspices