6.5 Publishers

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”.

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).

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:

documentclass: krantz
lot: yes
lof: yes
fontsize: 12pt
monofont: "Source Code Pro"
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.

\setmainfont[
  UprightFeatures={SmallCapsFont=AlegreyaSC-Regular}
]{Alegreya}

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

\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\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.

\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.

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

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

bookdown::pdf_book:
  includes:
    in_header: latex/preamble.tex
    before_body: latex/before_body.tex
    after_body: latex/after_body.tex
  keep_tex: yes
  dev: "cairo_pdf"
  latex_engine: xelatex
  citation_package: natbib
  template: null
  pandoc_args: --top-level-division=chapter
  toc_unnumbered: no
  toc_appendix: yes
  quote_footer: ["\\VA{", "}{}"]
  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:

\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

\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).

We printed the index in latex/after_body.tex (Section 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.

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.

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.

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

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.