作者:Yihui Xie
译者:郑宝童
日期:2021.05.21


3.2 LaTeX/PDF.

We strongly recommend that you use an HTML output format instead of LaTeX when you develop a book, since you will not be too distracted by the typesetting details, which can bother you a lot if you constantly look at the PDF output of a book. Leave the job of careful typesetting to the very end (ideally after you have really finished the content of the book).

The LaTeX/PDF output format is provided by pdf_book() in bookdown. There is not a significant difference between pdf_book() and the pdf_document() format in rmarkdown. The main purpose of pdf_book() is to resolve the labels and cross-references written using the syntax described in Sections 2.4, 2.5, and 2.6. If the only output format that you want for a book is LaTeX/PDF, you may use the syntax specific to LaTeX, such as \label{} to label figures/tables/sections, and \ref{} to cross-reference them via their labels, because Pandoc supports LaTeX commands in Markdown. However, the LaTeX syntax is not portable to other output formats, such as HTML and e-books. That is why we introduced the syntax (\#label) for labels and \@ref(label) for cross-references.

There are some top-level YAML options that will be applied to the LaTeX output. For a book, you may change the default document class to book (the default is article), and specify a bibliography style required by your publisher. A brief YAML example:

我们强烈建议您在开发一本书时使用HTML输出格式,而不是LaTeX,因为这样您就不会被排版细节分心。如果您经常查看一本PDF格式的书,排版细节会给您带来很大的麻烦。.把仔细的排版工作留到最后(最好是在你真正完成书的内容之后)。

LaTeX/PDF输出格式是由bookdown里的 pdf_book()函数实现. 在rmarkdownpdf_book()pdf_document() 没有显著差别. pdf_book() 的主要目的是为了解决Sections 2.4, 2.5, 2.6所描述的标签和交叉引用的书写问题。如果您想要的一本书的输出格式是LaTeX/PDF,也因为Pandoc支持Markdown中的LaTeX命令,所以您可以使用LaTeX特有的语法,例如\label{} 来标记figures/tables/sections, \ref{} 通过它们的标签来进行交叉引用。但是,LaTeX语法不能移植到其他输出格式,比如HTML和电子书。这也是为何我们为label引入语法(\#label) and 交叉引用引入语法\@ref(label).

有一些顶层的YAML选项将应用于LaTeX输出。对于图书,您可以将默认文档类更改为book (默认为article)),并指定出版商所需的书目样式。下方是一个简短的YAML例子:

  1. ---
  2. documentclass: book
  3. bibliography: [book.bib, packages.bib]
  4. biblio-style: apalike
  5. ---

There are a large number of other YAML options that you can specify for LaTeX output, such as the paper size, font size, page margin, line spacing, font families, and so on. See http://pandoc.org/MANUAL.html#variables-for-latex for a full list of options.

您可以为LaTeX输出指定大量其他YAML选项,例如纸张大小、字体大小、页边距、行间距、字体系列等等。查看http://pandoc.org/MANUAL.html#variables-for-latex以获得完整的选项列表。
The pdf_book() format is a general format like html_book(), and it also has a base_format argument:

  1. pdf_book(toc = TRUE, number_sections = TRUE, fig_caption = TRUE,
  2. pandoc_args = NULL, ..., base_format = rmarkdown::pdf_document,
  3. toc_unnumbered = TRUE, toc_appendix = FALSE, toc_bib = FALSE,
  4. quote_footer = NULL, highlight_bw = FALSE)

You can change the base_format function to other output format functions, and bookdown has provided a simple wrapper function tufte_book2(), which is basically pdf_book(base_format = tufte::tufte_book), to produce a PDF book using the Tufte PDF style (again, see the tufte package).

您可以将base_format 函数更改为其他输出格式函数,并且bookdown提供了一个简单的封装函数 tufte_book2(),它基本上是pdf_book(base_format = tufte::tufte_book),以使用tufte PDF样式生成PDF图书(同样,请参阅tufte包)