Writing a Master's thesis with R Markdown and Bookdown

I finished my Master’s thesis very recently, and I wrote it with R Markdown, and more precisely with the bookdown package. It was really comfortable to do absolutely everything with R: data treatment, use of econometric methods, redaction with chunk of codes, and even the slides for the presentation! However, I have also spent a non-negligible part of my time trying to have a correct layout, essentially for the first pages. I found that some things were not as easy to do as they should be. This post contains some solutions to these problems.

Why bookdown?

A small preamble before starting to list these problems and solutions: why did I use bookdown and not rmarkdown? bookdown has a few advantages that are very important when writing a Master’s thesis (or an academic paper in general), such as cross-references between sections, figures, tables, etc. See the bookdown book for all the details.

While it is possible to create a bookdown project with RStudio, I “manually” made my own, because I have the impression there are a lot of files created automatically by RStudio that would just confuse myself. Therefore, I had a file containing the YAML and the chunks necessary to load all the packages needed and to run the child documents. Child documents are .Rmd files that contain only some Markdown text and code chunks (no YAML). They make it much easier to write a thesis since it is possible to divide it in several pieces (introduction, literature review, method, etc.).

In addition to Global.Rmd (which contains YAML and setup chunks), I used two .tex files: preamble.tex is where I put all the LaTeX commands and packages I used, and titlepage.tex to make my custom titlepage, acknowledgments and abstract before the table of contents.

LaTeX packages and commands

In preamble.tex, I put all the LaTeX commands, many of them being \usepackage. For example, the R package kableExtra provides a list of LaTeX packages required to be able to customize the tables (see here).

Here’s a small list of the commands I used for my thesis.

  • \renewcommand{\baselinestretch}{1.3} to change the space between lines;

  • \pagenumbering{gobble} to remove page numbering for the first pages, that contain the title, the table of content (TOC), the lists of figures and tables (LOF and LOT) and some blank pages. I used \pagenumbering{arabic} after the YAML to start the page numbering at the right place.

  • \usepackage{caption}; \captionsetup[table]{name=Tableau}; \captionsetup[figure]{name=Figure}. These three commands (to put on three separate lines) are here to change the name of tables and figures. If you’re writing in English, you probably won’t need them, but they were necessary to write in French.

  • the following lines create a new command to create a blank page after the titlepage. Strangely, \pagebreak or \newpage didn’t work inside the titlepage and I had to find an alternative:

Command to create a blank page


Titlepage, acknowledgements and abstract

Concerning these three components, I put them in another file named titlepage.tex. This is the layout I wanted:

  • a titlepage with some logos and some information on my thesis and my university;

  • a blank page;

  • acknowledgements on a new page;

  • abstract on a new page;

  • TOC, LOF and LOT on a new page.

To do so, I started the titlepage with \begin{titlepage} and customized it as I wanted. But before putting \end{titlepage}, I placed \afterpage{\blankpage}, which is the command we define in preamble.tex. With this, I had a titlepage and a blank page.

The next step was to create two pages containing the acknowledgements and the abstract This was easily done in LaTeX, and this time I could use \pagebreak at the end of the acknowledgements to create a new page for the abstract. I also put another \pagebreak to finish titlepage.tex, so that TOC, LOF and LOT (created in the YAML) could start on a new page.

Include LaTeX files in YAML

As explained, I had two .tex files to run when compiling the R Markdown file:

  • preamble.tex is a list of commands that should be placed before \begin{document} when this .Rmd file will be converted to .tex. Therefore, I used in_header in the YAML to compile it.

  • titlepage.tex contains some elements that should be in the final PDF document. Since this should appear first (before the rest of the .Rmd document), I used before_body in the YAML.


Here are examples of the three files: Global.Rmd, preamble.tex and titlepage.tex.


      in_header: preamble.tex
      before_body: titlepage.tex
    keep_tex: true
    toc: yes
    toc_depth: 3
indent: true
link-citations: yes
lot: true
lof: true

<!-- Start the redaction on a new page -->

<!-- Start page numbering where the redaction starts -->

```{r globaloptions, include=FALSE}
# Include here chunk options

```{r packages}
# Load here the packages

<!-- Call the child documents -->
```{r body, child = c('01-Intro.Rmd', '02-Literature.Rmd', '03-Data-and-method.Rmd', '04-Results.Rmd', '05-Discussion.Rmd', '06-Conclusion.Rmd')}

<!-- Placement of bibliography -->
# References {-}

<div id="refs"></div>

<!-- Place the appendix after the bibliography -->
```{r appendix, child = c('07--Appendix.Rmd')}


% Line spacing

% Page number and chapter at the top of the page

% Important packages

% Figure and table names 

% Packages for kableExtra

% Remove page numbering before start of redaction

% Command to make a blank page


Title of my thesis

Thanks everyone

Bla bla bla...

Hope this helps!

PhD Student in Economics