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


    Besides publishing your book online, you can certainly consider publishing it with a publisher. For example, this book was published with Chapman & Hall/CRC, and there is also a free online version at https://bookdown.org/yihui/bookdown/ (with an agreement with the publisher). Another option that you can consider is self-publishing (https://en.wikipedia.org/wiki/Self-publishing) if you do not want to work with an established publisher. Pablo Casas has written two blog posts that you may find useful: “How to self-publish a book” and “How to self-publish a book: customizing bookdown”.

    除了在线出版您的书,您当然可以考虑与出版商一起出版。例如,这本书是与 Chapman & Hall/CRC 一起出版的,在 https://bookdown.org/yihui/bookdown/ 也有免费的在线版本(与出版商有协议)。如果您不想与成熟的出版商合作,您可以考虑的另一种选择是自行出版 (https://en.wikipedia.org/wiki/Self-publishing)。 Pablo Casas 写了两篇您可能会觉得有用的博客文章: “How to self-publish a book” and “How to self-publish a book: customizing bookdown”.

    It will be much easier to publish a book written with bookdown if the publisher you choose supports LaTeX. For example, Chapman & Hall provides a LaTeX class named krantz.cls, and Springer provides svmono.cls. To apply these LaTeX classes to your PDF book, set documentclass in the YAML metadata of index.Rmd to the class filename (without the extension .cls).

    如果您选择的出版商支持 LaTeX,出版用 bookdown 编写的书会容易得多。例如,Chapman & Hall 提供了一个名为 krantz.cls 的 LaTeX 类,而 Springer 提供了 svmono.cls。要将这些 LaTeX 类应用于您的 PDF 书籍,请将 index.Rmd 的 YAML 元数据中的 documentclass 设置为类文件名(不带扩展名 .cls )。

    The LaTeX class is the most important setting in the YAML metadata. It controls the overall style of the PDF book. There are often other settings you want to tweak, and we will show some details about this book below. The YAML metadata of this book contains these settings:

    LaTeX 类是 YAML 元数据中最重要的设置。它控制着 PDF 书的整体风格。您通常还需要调整其他设置,我们将在下面展示有关本书的一些详细信息。 本书的 YAML 元数据包含以下设置:

    1. documentclass: krantz
    2. lot: yes
    3. lof: yes
    4. fontsize: 12pt
    5. monofont: "Source Code Pro"
    6. monofontoptions: "Scale=0.7"

    The field lot: yes means we want the List of Tables, and similarly, lof means List of Figures. The base font size is 12pt, and we used Source Code Pro as the monospaced (fixed-width) font, which is applied to all program code in this book. In the LaTeX preamble (Section 4.1), we have a few more settings. First, we set the main font to be Alegreya, and since this font does not have the Small Capitals feature, we used the Alegreya SC font.

    字段 lot: yes 表示我们想要表格列表,类似地, lof 表示数字列表。基本字体大小是 12pt,我们使用 Source Code Pro作为等宽(固定宽度)字体,适用于本书中的所有程序代码。 在 LaTeX 序言(第 4.1 节)中,我们还有一些设置。首先,我们将主字体设置为 Alegreya,由于该字体没有 Small Capitals 功能,我们使用了 Alegreya SC 字体。

    1. \setmainfont[
    2. UprightFeatures={SmallCapsFont=AlegreyaSC-Regular}
    3. ]{Alegreya}

    The following commands make floating environments less likely to float by allowing them to occupy larger fractions of pages without floating.

    下面的命令使浮动环境不容易浮动,允许它们占据更大的几分之几的页面而不浮动

    1. \renewcommand{\textfraction}{0.05}
    2. \renewcommand{\topfraction}{0.8}
    3. \renewcommand{\bottomfraction}{0.8}
    4. \renewcommand{\floatpagefraction}{0.75}

    Since krantz.cls provided an environment VF for quotes, we redefine the standard quote environment to VF. You can see its style in Section 2.1.

    由于krantz.cls为引用提供了一个环境VF,我们将标准quote环境重新定义为VF。你可以在第 2.1节看到它的风格。

    1. \renewenvironment{quote}{\begin{VF}}{\end{VF}}

    Then we redefine hyperlinks to be footnotes, because when the book is printed on paper, readers are not able to click on links in text. Footnotes will tell them what the actual links are.

    然后我们将超链接重新定义为脚注,因为当书印在纸上时,读者无法点击文本中的链接。脚注会告诉他们实际的链接是什么。

    1. \let\oldhref\href
    2. \renewcommand{\href}[2]{#2\footnote{\url{#1}}}

    We also have some settings for the bookdown::pdf_book format in _output.yml:

    我们在_output.yml中也有一些bookdown::pdf_book格式的设置:

    1. bookdown::pdf_book:
    2. includes:
    3. in_header: latex/preamble.tex
    4. before_body: latex/before_body.tex
    5. after_body: latex/after_body.tex
    6. keep_tex: yes
    7. dev: "cairo_pdf"
    8. latex_engine: xelatex
    9. citation_package: natbib
    10. template: null
    11. pandoc_args: --top-level-division=chapter
    12. toc_unnumbered: no
    13. toc_appendix: yes
    14. quote_footer: ["\\VA{", "}{}"]
    15. highlight_bw: yes

    All preamble settings we mentioned above are in the file latex/preamble.tex, where we also specified that the front matter starts:

    我们上面提到的所有序言设置都在latex/preamble.tex文件中,我们也在这里指定了前言的开始。

    1. \frontmatter

    In latex/before_body.tex, we inserted a few blank pages required by the publisher and wrote the dedication page. Before the first chapter of the book, we inserted

    latex/before_body.tex中,我们插入了出版商要求的几页空白页,并写了献词页。在该书的第一章之前,我们插入了

    1. \mainmatter

    so that LaTeX knows to change the page numbering style from Roman numerals (for the front matter) to Arabic numerals (for the book body).

    这样,LaTeX就知道要把页码样式从罗马数字(用于封面)改为阿拉伯数字(用于书主体)。

    We printed the index in latex/after_body.tex (Section 2.9).

    我们在latex/after_body.tex中打印了索引(2.9节)。

    The graphical device (dev) for saving plots was set to cairo_pdf so that the fonts are embedded in plots, since the default device pdf does not embed fonts. Your copyeditor is likely to require you to embed all fonts used in the PDF, so that the book can be printed exactly as it looks, otherwise certain fonts may be substituted and the typeface can be unpredictable.

    保存绘图的图形设备(dev)被设置为cairo_pdf,以便将字体嵌入绘图中,因为默认设备pdf不嵌入字体。你的copyeditor 很可能要求你嵌入PDF中使用的所有字体,这样书就可以完全按照它的样子打印出来,否则某些字体可能会被替换掉,而且字体可能无法预测。

    The quote_footer field was to make sure the quote footers were right-aligned: the LaTeX command \VA{} was provided by krantz.cls to include the quote footer.

    quote_footer 字段用于确保引用页脚右对齐:LaTeX 命令 \VA{}krantz.cls 提供以包含引用页脚。

    The highlight_bw option was set to true so that the colors in syntax highlighted code blocks were converted to grayscale, since this book will be printed in black-and-white.

    highlight_bw 选项设置为 true,以便将语法高亮代码块中的颜色转换为灰度,因为本书将以黑白打印。

    The book was compiled to PDF through xelatex to make it easier for us to use custom fonts.

    这本书通过xelatex编译成PDF,使我们更容易使用自定义字体。

    All above settings except the VF environment and the \VA{} command can be applied to any other LaTeX document classes. In case you want to work with Chapman & Hall as well, you may start with the copy of krantz.cls in our repository (https://github.com/rstudio/bookdown/tree/master/inst/examples) instead of the copy you get from your editor. We have worked with the LaTeX help desk to fix quite a few issues with this LaTeX class, so hopefully it will work well for your book if you use bookdown.

    除了 VF 环境和\VA{}命令之外的所有上述设置都可以应用于任何其他 LaTeX 文档类。 如果您也想与 Chapman & Hall 合作,您可以从我们存储库 (https://github.com/rstudio/bookdown/tree/master/inst/examples) 中的 krantz.cls 副本开始你从你的编辑那里得到的副本。我们已经与 LaTeX 帮助台合作解决了这个 LaTeX 类的很多问题,所以希望如果你使用 bookdown,它会很好地适用于你的书。