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


4.2 Theming

Sometimes you may want to change the overall theme of the output, and usually this can be done through the in_header option described in the previous section, or the css option if the output is HTML. Some output formats have their unique themes, such as gitbook, tufte_html_book, and tufte_book2, and you may not want to customize these themes too much. By comparison, the output formats html_book() and pdf_book() are not tied to particular themes and more customizable.

有时您可能想要更改输出的整体主题,这通常可以通过上一节中描述的in_header选项来完成。如果输出格式是HTML,则可以使用css选项。有些输出格式有其独特的主题,如gitbooktufte_html_booktufte_book2,您可能不希望过多地自定义这些主题。相比之下,输出格式html_book()pdf_book()没有绑定到特定的主题,而且更易于定制。

As mentioned in Section 3.1.2, the default style for html_book() is the Bootstrap style. The Bootstrap style actually has several built-in themes that you can use, including default, cerulean, journal, flatly, darkly, readable, spacelab, united, cosmo, lumen, paper, sandstone, simplex, and yeti. You can set the theme via the theme option, e.g.,

3.1.2节所述,html_book()的默认样式是Bootstrap样式。Bootstrap风格实际上有几个内置的主题可以使用,包括default, cerulean, journal, flat, dark, readable, spacelab, united, cosmo, lumen, papersandstonesimplexyeti。你可以通过主题选项设置主题,例如:

  1. ---
  2. output:
  3. bookdown::html_book:
  4. theme: united
  5. ---

If you do not like any of these Bootstrap styles, you can set theme to null, and apply your own CSS through the css or includes option.

For pdf_book(), besides the in_header option mentioned in the previous section, another possibility is to change the document class. There are many possible LaTeX classes for books, such as memoir (https://www.ctan.org/pkg/memoir), amsbook (https://www.ctan.org/pkg/amsbook), KOMA-Script (https://www.ctan.org/pkg/koma-script) and so on. Here is a brief sample of the YAML metadata specifying the scrbook class from the KOMA-Script package:

如果你不喜欢这些Bootstrap样式,你可以将theme设置为null,并通过cssincludes选项应用你自己的CSS。
对于pdf_book(),除了前面提到的in_header 选项外,另一种可选的方式是更改文档类。有许多适合书籍的LaTeX类,如memoir (https://www.ctan.org/pkg/memoir), amsbook (https://www.ctan.org/pkg/amsbook), KOMA-Script (https://www.ctan.org/pkg/koma-script) 等。下面是一个简短的YAML元数据示例,它指定了来自KOMA-Script包的scrbook类:

  1. ---
  2. documentclass: scrbook
  3. output:
  4. bookdown::pdf_book:
  5. template: null
  6. ---

Some publishers (e.g., Springer and Chapman & Hall/CRC) have their own LaTeX style or class files. You may try to change the documentclass option to use their document classes, although typically it is not as simple as that. You may end up using in_header, or even design a custom Pandoc LaTeX template to accommodate these document classes. Note that when you change documentclass, you are likely to specify an additional Pandoc argument --top-level-division=chapter so that Pandoc knows the first-level headers should be treated as chapters instead of sections (this is the default when documentclass is book), e.g.,

一些发行商 (e.g., Springer and Chapman & Hall/CRC)有自己的LaTeX风格或类文件。您可以尝试更改documentclass选项以使用它们的文档类,尽管通常情况下没有那么简单。您可能最终会使用in_header,甚至设计一个自定义Pandoc LaTeX模板来容纳这些文档类。
注意,当你改变documentclass时,你可能会指定一个额外的Pandoc参数--top-level-division=chapter,这样Pandoc就知道第一级的头文件应该被当作章而不是节(chapters instead of sections )(当documentclassbook时,这是默认值)

  1. documentclass: krantz
  2. output:
  3. bookdown::pdf_book:
  4. pandoc_args: --top-level-division=chapter