Page Numbering in R Bookdown

bookdown, latex, r

Solution

Please also look at the answer by @maxheld. It is simpler and for many use cases sufficient. Another simple solution that does not require editing the template would be:

---
title: Page Numbering in R Bookdown
documentclass: book
output:
  bookdown::pdf_book: default
header-includes:
- \AtBeginDocument{\frontmatter}
---
\mainmatter

# Header 1

Some text 

\backmatter

# Appendix 1

Some text 

This inserts `\frontmatter`, `\mainmatter` and `\backmatter` in the right places.

Original answer

PDF output is produced with LaTeX using the `book` document class. In this class, you can use `\frontmatter`, `\mainmatter` and `\backmatter` to separate different 'parts' of the book. In order to use this with `bookdown` I did the following:

- start with a copy of https://github.com/seankross/bookdown-start

- copy `<R-library-path>/rmarkdown/rmd/latex/default-1.17.0.2.tex` as `book.tex` to the working directory

- update `book.tex` to include `\frontmatter`, `\mainmatter` and `\backmatter` (diff below)

- update `_output.yml` to refer to `book.tex` as `template` (diff below)

With these changes the PDF produced with `bookdown` used (loweercase) roman numerals for the ToC and restarted with arabic numbers for the actual body. Here the diff:

 diff --git a/_output.yml b/_output.yml
index 112cf5b..b211ba7 100644
--- a/_output.yml
+++ b/_output.yml
@@ -13,5 +13,6 @@ bookdown::pdf_book:
     in_header: preamble.tex
   latex_engine: xelatex
   citation_package: natbib
+  template: book.tex
 bookdown::epub_book:
   stylesheet: style.css
diff --git a/book.tex b/book.tex
index 0f9979d..3d03540 100644
--- a/book.tex
+++ b/book.tex
@@ -235,6 +235,9 @@ $header-includes$
 $endfor$

 \begin{document}
+$if(book-class)$
+\frontmatter
+$endif$
 $if(title)$
 \maketitle
 $endif$
@@ -263,8 +266,14 @@ $endif$
 $if(lof)$
 \listoffigures
 $endif$
+$if(book-class)$
+\mainmatter
+$endif$
 $body$

+$if(book-class)$
+\backmatter
+$endif$
 $if(natbib)$
 $if(bibliography)$
 $if(biblio-title)$

Problem

How do you achieve roman numerals for things like preface, acknowledgement etc and restart Arabic numbering at 1 for first page of chapter one in R Bookdown. I am wanting to render to pdf in bookdown, but have not found any good information on how to change page numbering like this thanks

Original source