Writing your thesis with R Markdown (4) – Putting the thesis together

This is the fourth post in a short series of tutorials to write your thesis in R Markdown. You can find instructions on how to get started in the first post. These tutorials were written by a Windows user, so if you are using a different operating system some details may differ.

Welcome back! You have arrived at the fourth part of this tutorial. In the previous post we looked at including figures, R code, and tables IN the thesis chapter. This time we’ll be looking at making multiple chapters, putting them together and turnING them into a thesis. In the next (and hopefully last) post I will show you how to adjust the thesis layout. Let’s get started!

Step 1: Merging multiple chapters into one thesis

You may have found that writing your whole thesis in one document can quickly get confusing. If you are like me, you have probably written multiple chapters in multiple documents. So let’s make a ‘parent document’ called THESIS.rmd that contains all the ‘child’ chapters. In R Markdown you can easily do this using this bit of code (assuming you keep chapters in the same folder as the THESIS.rmd file) in your THESIS.rmd file:

```{r child = 'chapterxx.Rmd'}
```

You can add as many child documents to the parent document as you like. If you want to make sure that chapters start on a new page, use the LaTeX command \newpage between the chapters you add to make each chapter start on a new page. Similarly, you might want to add the LaTeX command \FloatBarrier between chapters to make sure that figures, tables and such that belong to one chapter are not accidentally moved to another chapter (this happens sometimes as LaTeX is a typesetting system that is designed to allow the author to write without having to worry about the layout of the document; LaTeX tries to figure out the best spot to place figures and tables, which depends on the space that is available). To ensure the \FloatBarrier command works, you need to load a LaTeX package. You can do this very easily by adding this bit of code to the YAML header (the bit between the dashed lines at the top of the document):

header-includes:
- \usepackage{placeins}

Step 2: Adding a Titlepage, Declaration, Abstract and Acknowledgements

The same way you added chapters to your thesis, you can also add a titlepage, declaration page, abstract and acknowledgements. LaTeX has some fancy syntax to do this (for example see here), but I didn’t look into this, as I found just straight up adding the pages as child documents was nice enough. Basically, I treated each of these pages as I would treat the chapter documents. For example, I added:

```{r child = 'titlepage.Rmd'}
```

to include the titlepage (for an example of titlepage you can write in R Markdown/LaTeX, see here for the .rmd file and here for the .pdf example..

By now, the THESIS.rmd parent file will look something like this, which produces this pdf file. I’ve given the chapters a little bit of text so they wouldn’t look so empty :).

Step 3: Adding the Table of Contents, List of Figures, List of Tables, References

R Markdown automatically places all references at the bottom of the document, but doesn’t not give it a title. To do this manually, just write #References at the bottom of THESIS.rmd document. The references will be placed below this.

LaTeX offers some inbuilt functionality to automatically generate the Table of Contents, List of Figures and List of Tables. It is fairly simple: wherever you want to include one of these, you just type: \tableofcontents, \listoffigures or \listoftables in the THESIS.rmd file. To change the Table of Contents depth to for example headings and subheadings only, write \setcounter{tocdepth}{2}. You can play around with the number for a bit to see what depth you prefer. This is probably also a good moment to add some section numbering, so chapters and sections will be shown as e.g. 1. Introduction and 1.1 Things instead of Introduction and Things. We can do this easily by adding number_sections: yes to the YAML header:

output:
  pdf_document:
    fig_caption: yes
    number_sections: yes

This is what the table of contents will look by now (see here for the full .rmd and .pdf files):

Table of Contents with numbered sections.
Table of Contents with numbered sections.

As you can see, the titlepage, declaration, abstract, acknowledgements, list of figures and list of tables are not shown in the table of contents. This is because these pages don’t have chapter headings (i.e. for the abstract page I just wrote abstract instead of #Abstract – see here for the child documents I used in my examples – you might have to click on Raw as Github automatically renders any markdown in documents). You can include them by writing #Abstract, but that way they will appear numbered in the table of contents as well. To avoid this, you can instead write \section*{Abstract} at the place where you’d like the title to appear. Using the \section*{} command will not automatically add the section to the table of contents, you can do this by writing this bit of code:

\addcontentsline{toc}{section}{Abstract}

Similarly, you can write \addcontentsline{toc}{section}{List of Figures} to add the list of figures to the table of contents.

Note: Although I’ve been talking about chapter headings here, officially they are called section headings. You can read more about it here.

By now, the THESIS.rmd parent file will look something like this, which produces this pdf file.

Step 4: Global R code chunk options

As we saw in tutorial #3 that you can set knitr options that apply to all code chunks in your document. If you define your defaults in the parent document, these will be applied to all code chunks in the child documents (unless a code chunk has its own settings I’m pretty sure they won’t be overridden). The R code chunk options I’ve found useful are:

```{r include=FALSE}
knitr::opts_chunk$set(fig.path = 'figures/', 
                      echo = FALSE, warning = FALSE, message = FALSE)
```

Just add this chunk at the top of the document, right below the YAML header and you’re good to go.

Note: all the stuff you define in the parent document (i.e. YAML header, global R code chunks settings) will not be included if you try to render a child document seperately. So if you have a THESIS.rmd file that includes the package ‘placeins’, chapter1.rmd will not have this, unless you are rendering chapter1.rmd as a child document of THESIS.rmd.

By now you have pretty much got your whole thesis put together. All that is left now is the layout! If you found anything confusing, don’t hesitate to leave a comment or email me, and hopefully I’ll see you again in the last post.

Leave a Reply

Your email address will not be published. Required fields are marked *

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top