6 min read

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 to the thesis chapter. This time we’ll be looking at making multiple chapters, putting them together and turn 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:

[code language=“r”]


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 ‘<em>\newpage</em>’ between the chapters you add to make each chapter start on a new page. Similarly, you might want to add the LaTeX command ‘<em>\FloatBarrier</em>’ 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):

[code language=”"r"”] header-includes: - \usepackage{placeins} [/code]

<strong>Step 2: Adding a Titlepage</strong><strong>, Declaration, Abstract and Acknowledgements </strong>

The same way you added chapters to your thesis, you can also add a titlepage, declaration page, abstract and acknowledgements. LaTeX ha<span style="color:#333333;">s some fancy syntax to do </span>this (for example <a href="https://www.sharelatex.com/blog/2013/08/02/thesis-series-pt1.html" target="_blank">see here</a>), 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:

[code language=”"r"”]


to include the titlepage (for an example of titlepage you can write in R Markdown/LaTeX, see <a href="https://raw.githubusercontent.com/rosannav/thesis_in_rmarkdown/master/titlepage.rmd" target="_blank">here for the .rmd file</a> and <a href="https://github.com/rosannav/thesis_in_rmarkdown/blob/master/titlepage.pdf" target="_blank">here for the .pdf example</a>).

<em><strong>By now, the THESIS.rmd parent file will look something like <a href="https://raw.githubusercontent.com/rosannav/thesis_in_rmarkdown/master/thesis-step2.Rmd" target="_blank">this</a>,</strong> </em>which produces<a href="https://github.com/rosannav/thesis_in_rmarkdown/blob/master/thesis-step2.pdf" target="_blank"> this pdf file</a> (I've given the chapters a little bit of text so they wouldn't look so empty :).

<strong>Step 3: Adding the Table of Contents, List of Figures, List of Tables, References</strong>

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 '<em>#References</em>' 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: '<em>\tableofcontents</em>', '<em>\listoffigures</em>' or '<em>&lt;/em><em>listoftables</em>' in the THESIS.rmd file. To change the Table of Contents depth to for example headings and subheadings only, write '<em>\setcounter{tocdepth}{2}</em>'. 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 be adding 'number_sections: yes' to the YAML header:

[code language=”"r"”] output: pdf_document: fig_caption: yes number_sections: yes [/code]

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

[caption id="attachment_273" align="alignnone" width="584"]<a href="https://rosannavanhespenresearch.files.wordpress.com/2016/03/thesis-step3a1.png" rel="attachment wp-att-272"><img class="wp-image-273 size-large" src="https://rosannavanhespenresearch.files.wordpress.com/2016/03/thesis-step3a1.png?w=584" alt="thesis-step3A" width="584" height="208" /></a> Table of Contents with numbered sections. <em>Click on image to enlarge.</em>[/caption]

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 <span style="color:#333333;">chapter </span>headings (i.e. for the abstract page I just wrote 'abstract' instead of '#Abstract' - <em> <span style="color:#ff0000;"><a href="https://github.com/rosannav/thesis_in_rmarkdown" target="_blank">see here for the child documents I used in my examples</a></span>, you might have to click on 'Raw' as Github automatically renders any markdown in documents</em>). 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 '<em>\section{Abstract}</em>' 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:

[code language=”"r"”] \addcontentsline{toc}{section}{Abstract} [/code]

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

<em>Note: Although I've been talking about chapter headings here, officially there are called section headings. You can read more about it <a href="https://en.wikibooks.org/wiki/LaTeX/Document_Structure#Sectioning_commands" target="_blank">here</a>.</em>

<em><strong>By now, the THESIS.rmd parent file will look something like <a href="https://raw.githubusercontent.com/rosannav/thesis_in_rmarkdown/master/thesis-step3.Rmd" target="_blank">this</a>, </strong></em>which produces <a href="https://github.com/rosannav/thesis_in_rmarkdown/blob/master/thesis-step3.pdf" target="_blank">this pdf file</a>.

<strong>Step 4: Global R code chunk options</strong>

As we saw in <a href="https://rosannavanhespenresearch.wordpress.com/2016/03/18/writing-your-thesis-with-r-markdown-3-figures-r-code-and-tables/" target="_blank">tutorial #3</a>, 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<span style="color:#333333;"> overridden).</span> The R code chunk options I’ve found useful are:

[code language=”"r"”]

knitr::opts_chunk$set(fig.path = 'figures/',
                      echo = FALSE, warning = FALSE, message = FALSE)


Just add it 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.