Writing your thesis with R Markdown (5) – The thesis layout

This is the fifth and last 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.

Hi there, you’ve made it to the last post! Today we’ll be looking at making the thesis extra pretty.

Step 5: Organising the document layout with the YAML header

As you may remember, you can write a bunch of commands in the YAML header to tell knitr and LaTeX how to render the document. There are a whole range of things you can define here to adjust your layout. Here’s what I did:

    fig_caption: yes
    number_sections: yes
bibliography: library.bib
csl: methods-in-ecology-and-evolution.csl
urlcolor: black
linkcolor: black
fontsize: 12pt
geometry: margin = 1.2in
- \usepackage{placeins}
- \usepackage{setspace}
- \usepackage{chngcntr}
- \onehalfspacing
- \counterwithin{figure}{section}
- \counterwithin{table}{section}

We’ve already looked at number_sections in step 3 in the previous post, and as you may recall from tutorial #2, we used this bit:

bibliography: library.bib
csl: methods-in-ecology-and-evolution.csl

to define what library to use for the citations, and define the preferred citation style. The statements

urlcolor: black
linkcolor: black

are to indicate what colour the external (urlcolor) and internal (linkcolor) links should have. By default the linkcolor is magenta. Here I went a little crazy and opted for green and cyan:

Left: External links are now green, and internal links cyan. Right: pdf output.

The following arguments are pretty straightforward as well:

fontsize: 12pt
geometry: margin = 1.2in

to set fontsize at 12 points, and create a margin of 1.2 inches (30 mm).

All the stuff that is included under header-includes: are LaTeX arguments, such as packages that need to be loaded. So far, we’ve used the package placeins for the \FloatBarrier in step 1 of the previous post. The argument \onehalfspacing is used to set the line spacing (more info on LaTeX’s line spacing can be found here. The \onehalfspacing argument is part of the setspace package. I used the arguments - \counterwithin {figure}{section} and - \counterwithin{table}{section} to have LaTeX number the figures and tables according the section they belong to (e.g. the third figure in the thesis, but the first figure of the second chapter, will be named ‘figure 2.1’ instead of ‘figure 3’). To use these arguments, we need to use the package chngcntr.

Step 6: Headers, footers and page numbering

You’ll probably want to add some headers, footers and page numbering to the document so your reader doesn’t get completely lost. Page numbering is straightforward. You can use \pagenumbering{gobble} for pages without numbering, \pagenumbering{roman} for roman page numbering and \pagenumbering{arabic} for normal page numbering. Just include the argument wherever you want the have the page numbering changed. Default page numbering is arabic.

For headers and footers we can use the package ‘fancyhdr’. You can do some really cool stuff with it, see here for some info. Here’s what I did with it:


This bit of code states that from now on pages have fancy headers (\pagestyle{fancy}). Lines 2 and 3 state that headers on the left even (LE), right odd (RO), left odd (LO) and right even (RE) pages are empty by default. I set the line below the header to a 0.4pt width (\renewcommand{\headrulewidth}{0.4pt}), and removed the footer line by setting it to 0pt. I then manually set the header title for each page by writing, for example, \fancyhead[CO,CE]{Abstract} (CO, CE means centrally displayed on odd and even pages).

When pages are fully covered with a picture, graph or table, you might prefer to turn off page numbering and headers or footers. You can do this by adding this bit of code under the header-includes in the YAML header:

- \usepackage{floatpag}
- \floatpagestyle{empty}

If you’d like to know what the files should like by now, you can see what the THESIS.rmd and THESIS.pdf files will look by now.

Spelling checking and commenting

One thing we did not look at is spelling checking and commenting. There various ways to do this, but none of them are as elegant and easy to use as the ones in editors like MS Word. I tried a bunch of things, but decided that the best for me to deal with this was to convert .rmd to .pdf files as usual, and then use a pdf to word converter (it is also possible to create a .doxc file with knitr, but because I didn’t optimise the .rmd file for conversion to word it creates quite messy word documents. This makes it hard for others (supervisors) to get an idea what the final document is supposed to look like).

If you are working with the sublime editor, you can turn the spell checker on in there: http://juristr.com/blog/2014/11/enable-spell-check-sublime-markdown/.

If you need to comment to yourself while you’re writing in the .rmd file, you can use this syntax <--your comment here-->. It works alright, although you have to be careful with it: if you accidentally remove the last bit of the comment (the --> bit), but leave the first bit, and then try to compile your thesis, some bits may dissappear as knitr thought they were still part of the comment. I recommend making sure you’ve removed all comments when you compile the final thesis.

A note on pandoc

You might have heard some people talk about pandoc, and you might be wondering what it is. There is actually no need to undertstand what pandoc does, but if you’re curious: knitr depends on pandoc for its .md to .tex conversion. Basically what happens, is that knitr convert your .Rmd file to a .md (markdown file). That means that nothing changes, apart from the R code chunks, that are rendering and transformed into plain markdown. This includes creating the figures and storing them. Pandoc and pdflatex than come into play to convert the .md file to a .pdf, .doxc or .html file:

knitr, pandoc, pdflatex

The role of knitr, pandoc and pdflatex in converting R Markdown to a pdf file.

That’s it!

I hope these tutorials were helpful! If you’d like some more examples, I’ve uploaded an example thesis to github. See here for all the files, and here for the final pdf output. Good luck!

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