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:

```{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 ‘<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):

header-includes:
– \usepackage{placeins}

<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&quot; 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:

“`{r child = ‘titlepage.Rmd’}
“`

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&quot; target="_blank">here for the .rmd file</a> and <a href="https://github.com/rosannav/thesis_in_rmarkdown/blob/master/titlepage.pdf&quot; 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&quot; target="_blank">this</a>,</strong> </em>which produces<a href="https://github.com/rosannav/thesis_in_rmarkdown/blob/master/thesis-step2.pdf&quot; 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>\</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:

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):

<a href="https://rosannavanhespenresearch.files.wordpress.com/2016/03/thesis-step3a1.png&quot; 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&quot; alt="thesis-step3A" width="584" height="208" /></a> Table of Contents with numbered sections. <em>Click on image to enlarge.</em>

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&quot; 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:

\addcontentsline{toc}{section}{Abstract}

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&quot; 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&quot; target="_blank">this</a>, </strong></em>which produces <a href="https://github.com/rosannav/thesis_in_rmarkdown/blob/master/thesis-step3.pdf&quot; 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/&quot; 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:

“`{r include=FALSE}
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.

Advertisements

9 thoughts on “Writing your thesis with R Markdown (4) – Putting the thesis together

  1. Pingback: Writing your thesis with R Markdown (5) – The thesis layout | Rosanna's Research

  2. This series of blog posts is awesome! I’m really into the idea of writing my thesis with R Markdown now, since I already love how intuitive Mardown is and what can be done with R. Thank you very much for sharing this! :)

    One thing you could add is how to handle an Appendix which comes after the References section. So far, I am using
    “`
    includes:
    after_body: appendix.Rmd
    “`
    But as a result, it seems that I can only use LaTeX, not R or Markdown in this appendix section.

    Also, I am curious why your “References” section is not numbered though you wrote `# References` instead of `\section*{References}` as for, e.g., the abstract.

    • Thanks blorben! That’s a good suggestion indeed. As for the not numbering of the references section, I had a look and I think it’s a standard Rmarkdown thing to not number the last section if a bibliography is included:

      ---
      output:
      pdf_document:
      number_sections: yes
      bibliography: library.bib
      ---

      # Chapter 1
      # Chapter 2
      # References

      When you leave out the “bibliography: library.bib” bit, References is also numbered.

  3. Hello,
    thanks for this post.

    One question:
    If I use the latex command $\newpage$ it does not print a newpage but in my output documents there is just ‘\newpage’ printed in a red color.

    What am I doing wrong?

  4. Pingback: Writing your thesis with R Markdown – paulvanderlaken.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s