Page Numbering in R Bookdown
Asked Answered
H

3

13

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

Hydroquinone answered 30/4, 2017 at 20:31 Comment(0)
P
15

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)$
Penelopa answered 13/1, 2018 at 20:47 Comment(0)
C
10

I think it might be easier to just follow the example @yihui used in his own bookdown krantz example:

Add the following files, perhaps in a /latex subfolder:

  • preamble.tex with the latex \frontmatter command at the end,
  • after_body.tex with the latex \backmatter command,

Then, before your first actual main body, just add the \mainmatter command (just in latex) to, say, your index.Rmd (whichever of your *.Rmds is first, conventionally index.Rmd).

Then, to amend your _output.yml like so:

bookdown::pdf_book:
  includes:
    in_header: latex/preamble.tex
    after_body: latex/after_body.tex 

This intersperses the correct \frontmatter, \mainmatter and \backmatter commands into your pandoc-ed latex book. It will be respected by most style files to make sure that arabic numbering only begins inside the main matter.

This is also documented in the publishing chapter of the bookdown book.

Cretaceous answered 22/2, 2018 at 10:51 Comment(1)
+1 since this is indeed much simpler. The only disadvantage is that the bibliography and any appendices are still part of \mainmatter. However, you can place \backmatter before the first appendix or at the end of the last chapter.Penelopa
F
2

It's actually very simple:

In the front matter part do this:

\cleardoublepage
\pagenumbering{roman}

In the main matter part:

\cleardoublepage
\pagenumbering{arabic}
Frequent answered 13/11, 2018 at 18:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.