Ongoing list of PhD resources I like

I am a big sucker for productivity and love finding tools to get thing done better and quicker. So when I feel like taking a break from my research work or straight out want to procrastinate, I often find myself browsing the web looking for tools to improve my writing, R code, or some advice that helps me with my research life. And there is a lot out there! Here’s as ongoing list of resources I like. Because why reinvent the wheel if someone else has already done the work before you?

Raul Pacheco-Vega’s blog

This is currently my favourite blog. Raul Pacheco-Vega is a Mexico-based assistant professor political science, doing some incredibly work sharing his advice on all things academia in many series of blog posts. What I think is particularly great, is that he makes a lot of the ‘implied, gutfeelingtype’ knowledge about the research process explicit. Whether it is paper writing, organising your day or work-life balance, fair chance he’s got you covered.

The Thesis Whisperer

The blog is aimed at PhD students, and discusses all kind of issues you can come across in the PhD journey: feeling stuck with your writing or your project, doing a good literature review, dealing with feedback and much more. The blog is run by Inger Mewburn, an assistant professor and research training director based in Australia. What I really like about this blog, is that Inger Mewburn provides a platform for other (ex-)graduate student to share their experiences. It gives a very ‘real’ insight into PhD life.

How to write a better minor thesis

I read this book when I wrote my master’s thesis, and it was just super helpful so I think everyone should know about it. It helped me shaping my arguments and building the structure of a long text. I haven’t used major thesis version of this book yet, but definitely plan to.

Our World in Data

Because data is awesome, and it’s good to be informed about the world around you. Website by Max Roser and team.

Fundamentals of Data Visualization

Which plot to make and how to make it readable? So many good ideas on this page! And upon further inspection it turns out that Clause Wilke, the author of this blog and professor of integrative biology in the US, has written many blog posts with questions you may find yourself asking as a researcher. Recommended!


What I’m learning about mangrove forests

Pretty mangrove stock photo

Photo by Maxwell Ridgeway on Unsplash

I’ve been wanting to write about my new research topic for a while, but it turns out it’s surprisingly hard! I didn’t want to just rehash Wikipedia. Anyway, today I want to give it a try. So dear reader, please let me introduce you to the wonderful world of mangrove forest research.

Coastal vegetation fascinates me. It’s weird. How can plants survive in such a rough environment? It’s salty, you’re flooded twice a day and on top of that you have to deal with waves bumping in to you all the time. And let’s not even get started on the sediment that you’re rooted in: if you’ve got bad luck you might just get washed away.

Yet somehow coastal plants of all shapes and sizes have managed to adapt, and actually thrive, in these environments. And what’s more, they protect coasts in the process. Because of their structures, they can reduce the energy of the waves crashing into them – and their roots can catch sediment and trap it there.

Mangrove forests are such dependable coastal ecosystems. There’s just two issues with them (not with the mangroves themselves, they’re pretty cool): we don’t know how well these forests protect our coasts exactly, because forest cover is variable and tends to change over time. But if you’re going to use a mangrove forest to protect a coast, you’d like to know how much it will protect you exactly, right? And how much it will protect you 5, 10, 20 years from now – especially now that the climate is changing, sea levels rising and storminess increasing.

The second issue is this: mangroves are disappearing at rates higher than tropical rainforests and restoration projects are not as successful as we’d like them to be. A great deal of these projects involve planting nursery-raised seedlings. If these seedlings are placed too low in the intertidal zone they drown or can’t handle the wave impact and get washed away. These thresholds are known for some species, but there is much more to be learned.

How can we help seedlings to establish and flourish into mature, coast-protecting trees? What causes mature trees to break or die? How much forest is left to protect you after a big storm? That’s what my research it all about. In the next three years and a bit, I hope to add a bit of new understanding to what drives all these changes in mangrove cover. Hopefully I can pull it off. Look forward to a big thesis in 3.5 years ;)

Some references
Koch et al. (2009) Non-Linearity in Ecosystem Services: Temporal and Spatial Variability in Coastal Protection. Frontiers in Ecology and the Environment

Narayan et al. (2016) The effectiveness, costs and coastal protection benefits of natural and nature-based defences. PLoS ONE

Kodikara et al. (2017) Have mangrove restoration projects worked? An in-depth study in Sri Lanka. Restoration Ecology

Grasp that topic! Getting started with a new area of research

When I started my PhD – about three months ago – I found myself having to learn the ins and outs of a whole new topic. Really fun, as I love learning new things, but also kind of like being shipwrecked and floating in the middle of the ocean with no idea which way to swim or where the nearest land might be. Help!

Introductory books or guides are often a good way to get started with a new topic, but there’s one small issue with that: it requires actually having that introductory book or guide (or at least knowing what and where it is). So how to find your way in the vast sea of information? Here’s 5 things that worked for me:

1. Good old Wikipedia
Even though academia did a thorough job at training me to trust only peer-reviewed literature (and even then be skeptic), I don’t find reading peer-reviewed articles particularly helpful when I am new to a field. It always kind of feels like trying to read the blueprint for a coffee machine, when all I really want is the quick start guide. Just show me where the on-button is so I can make my coffee! Wikipedia is like that quick start guide. Just type in your general area of research and poof, there you go.

#sciencetwitter is a thing and used by scientists all over the world. It’s a great resource to find out about things that are happening right now in your field. Allow me to demonstrate. Here’s 4 tweets that show up on top results page of Twitter if I just type in ‘mangrove’:

Result: a new, very relevant paper, a video that shows how mangroves protect us (though I find it rather puzzling that it’s posted by an account named Destroying Stuff), someone speaking about mangroves at a conference, and a passionate post about mangroves in Kenya. Pretty cool right? Don’t think I would have found these right way with my standard literature search.

3. YouTube
You can read as much as you want, a picture tells a thousand words and a video even more. I found YouTube a great resource to understand the new ecosystem I am studying. The videos of the different aspects of the mangrove forest were illustrative in a way that a scientific paper could never be.

youtube mangrove search results

Not bad for an intro to mangrove ecology

I am not sure if YouTube can work as well for non-ecology topics, but in any case, give it a try. You might stumble upon some useful lectures

4. Google Scholar: download all the things!
I’m the kind of person who prefers to have a broad overview before I dive in, and I love collecting bits and pieces of new information. Two days in to my first literature search (with basic search terms like mangrove ecology and coastal protection) I had downloaded over 200 papers, books and book chapters. This may seem a little excessive, but I was quite happy with my fresh stack of pdf’s. Here’s what I did with them:

  • I loaded all the papers into my reference manager (Mendeley), which automatically renamed and sorted them for me. Then, I had a look at the their title, journal and, if really curious, the abstract. This helped me to paint a mental image of the various topics in my research area, and also helped me identify materials that I wanted to started reading more thoroughly.
  • Sorting the papers by author gave me some idea of the key players in the field, and sorting by journal pointed me to the journals where I should keep an eye out for new publications (tip: sign up to their mailing list).
  • Finally, I sorted the papers by year, so I had rough idea of the way the field has changed over the past decades and when the current hot topics starting emerging. I continue to repeat these steps as I collect more articles to refine my data on major players, big journals and important years in the field.

5. Mindmap it
This tool is not as useful on day 1 as the tips above, because you kind of need to know your area of research a tiny, tiny bit (I did this three weeks in) but I think it is still worth mentioning. I love mind mapping, because it forces me to think about the stuff I read and I often end up making connections (not always sensible ones, but still, it’s good to get creative) I wouldn’t have thought of if I just left all the info floating around in my brain.

Mindmap in powerpoint

Mindmapping in PowerPoint

What I did: based on all the stuff I had read, I came up with a new list of keywords (about 40 words or so). I fired up PowerPoint and wrote each of the words in a text box and then starting drawing and redrawing arrows all over the place. Quite fun, and a nice way to take a break from the endless early-PhD reading sessions for a while.

I’d love to hear what works for you. What do you do when starting out with a new topic? What do you think of these tools?

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:

example of how internal and external links work

Left: External links are now green, and internal links cyan. Right: pdf output. Click on image to enlarge.

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:

If you need to comment to yourself while you’re writing in the .rmd file, you can use this syntax ‘<!– your comment here–>’ (there’s actually two dashes but you might not seem them – try copy pasting to make sure it works). 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. If you have any questions or found a mistake somewhere, feel free to contact me. Good luck!

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

– \usepackage{placeins}

<strong>Step 2: Adding a Titlepage</strong><strong>, 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 ha<span style="color:#333333;">s some fancy syntax to do </span>this (for example <a href="; 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="; target="_blank">here for the .rmd file</a> and <a href="; target="_blank">here for the .pdf example</a>).

<em><strong>By now, the THESIS.rmd parent file will look something like <a href="; target="_blank">this</a>,</strong> </em>which produces<a href="; 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:

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="; rel="attachment wp-att-272"><img class="wp-image-273 size-large" src="; 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="; 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:


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="; target="_blank">here</a>.</em>

<em><strong>By now, the THESIS.rmd parent file will look something like <a href="; target="_blank">this</a>, </strong></em>which produces <a href="; target="_blank">this pdf file</a>.

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

As we saw in <a href="; 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.

Writing your thesis with R Markdown (3) – Figures, R code and tables

This is the third 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 to part three of this tutorial! In the previous post we looked at including text, citations, and equations in the thesis chapter. This time we’ll look at including figures, R code and tables. Let’s start with the figures:

Step 4: Including a figure

You can include a figure in an R Markdown by writing ‘![Figure caption](name_of_figure.jpg)‘ in the document. To make sure a caption shows up underneath your figure, we need to turn on the captions option, as in R Markdown it is turned off by default. You can do this by writing this bit in your YAML header:

    fig_caption: yes

The indentation of the above bit of text is important, knitr will not understand it if it is not written exactly like the above.

To refer to the figure in-text, a label needs to be added to the figure. This can be done by writing: ‘![Figure caption \label{figurelabel}](filename.png)‘ (what you are actually doing here is adding a LaTeX label to Markdown text). Then, if you canwrite, for example, ‘see figure \ref{}‘, or as you may remember from the previous post, ‘see \autoref{}‘.


Left: Adding a figure. Right: .pdf output. Click on image to enlarge.

Note: figures are seen as ‘floats’ by LaTeX, and therefore are not placed immediately where you call them. If you want to get more specific with how to place them and what height and such, I recommend you use latex syntax to include the figure instead of using the R Markdown syntax. You can read more here. I generally did not find it necessary in my thesis, but it requires some playing around to figure out what works best for your thesis.

Step 5: using R code

This is where we get to the real magic of using R Markdown: including R code and data anlaysis directly into your thesis!

Here is how you include R code in your story. It’s called a code chunk:

# some R code

There are a bunch of values you can you give your code chunks. For example, if you don’t want to include code in your text, but just the output, you can write ‘{r include = FALSE}‘. You can also suppress warnings by writing  ‘{r warning = FALSE}‘. If you don’t do this and something is wrong with your code, a warning message might be printed in your thesis. Anyway, there’s a whole list of things you can do with knitr chunks. I only discuss the ones I found necessary here, but if you’re interested, Yihui Xie, the developer of knitr, provides some great documentation on his website.

There are probably some things you want most of your code chunks to do, so you can set your own defaults for your thesis using:

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

Basically what you are saying here is firstly, you want the figures to be saved in the folder ‘figures’, secondly, the R code itself won’t be printed, just the output, thirdly, warning won’t be printed and finally messages (similar to warnings) won’t be printed either.

Note: If you are making rather big calculations in the code chunks, this can make it slow to render your thesis (i.e. knitting). You can cache files with ‘cache=TRUE‘. Note that you will need to turn this off if you want to update your calculations.

One of the great things about including R code directly in your thesis is that you can generate and change figures as you go. To create a figure in your thesis using R code, just write the R code as you would normally when making a figure, for example:

```{r fig.cap = "figure caption. \\label{figurelabel}", fig.height=12, fig.width=10}

As you can see, I’ve added some info to the R code chunk about how to render the figure. I defined a figure caption with ‘fig.cap‘ and added a LaTeX label for in-text reference. Don’t forget to write the double backslash when adding a label name.

You can also refer to stuff in your text, that you calculated inside a code chunk. For example, if the code chunk says:

```{r include = FALSE}
variable_t = 3 * 4

you can refer to that in text by simply writing: `r variable_t`.

Below is image that compares the .rmd file and the .pdf output to show what kind of output each code chunk produces.


Left: Using R code in R Markdown. Right: .pdf output. Click on image to enlarge.

Step 6: Including a table

Sometimes you find yourself needing to include a table. Markdown and kable can do the trick, but I do not recommend using it for ‘serious, thesis-grade’ tables. In a thesis, you will almost always find yourself wanting more flexibility than Markdown and kable can offer. I therefore highly recommend using LaTeX when including tables.

This is some simple LaTeX code for a neat-looking table you might want to use in your thesis:

\begin{tabular}{ p{4cm} p{4cm} p{4cm} }
\hline \\ [-1.5ex]
colname &amp; colname &amp; colname \\ [1ex]
\hline \\ [-1.5ex]
Info & info & info \\ [1ex]
Info & info & info \\ [1ex]
Info & info & info \\ [1ex]

I will not go into great detail about LaTeX tables here, but if you’d like some more information, have a look at this Wiki. If you’re finding LaTeX tables a bit confusing, you can also write your table in excel and then upload it to this website: it will make a LaTeX table for you. Also, I had to use a couple of multi-page tables in my thesis. If you find you need to do the same, have a google search for ‘latex longtable’.

Note: References are note translated to LaTeX, but into plain text. This means that you cannot add any R Markdown references (i.e. [@Author2000]) in a LaTeX table. If you want more information, it got me quite confused as well, so I wrote a question and then an answer about it on


Left: Including a LaTeX table. Right: .pdf output. LaTeX automatically places tables at the bottom of the page. Click on image to enlarge.

And that’s it! Of course this is only one chapter, so in the next post we’ll have a look a writing multiple chapter in multiple .rmd files, how to put together and turn it into a pretty thesis. If you have any questions, feel free to post them in the comments or shoot me an email!

Writing your thesis with R Markdown (2) – Text, citations and equations

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

Welcome back to part two in this short series of tutorials! In the last post we looked at setting up all software and what an R Markdown document looks like. Now we will look at including text, citations, and equations in your thesis chapter. First up, writing plain text and headings:

Step 1: Writing plain text and headings

Take your standard Rmarkdown file (Rstudio > File > New File > R markdown… > OK) and remove all the text except for the YAML header (the bit between the three dashes),. Now write a little bit of text. Click on ‘knit’ to see what the output looks like (Note: I’ll be using pdf output in the examples). You can try adding some headings by using #-signs to structure your text. Below is an example that uses different headings:


Left: Writing headings in R Markdown. Right: .pdf output. Click on image to enlarge.

Step 2: Citing someone

Make sure you have Mendeley installed (you don’t necessary need use Mendeley. You can also check if your preferred reference manager provides BibTeX output or make your own bibtex file if you like. For the sake of speed and easiness, I’m going to use Mendely in my example).

If this is your first time using Mendeley and you don’t have any articles added yet, simply find a paper on you computer and drag and drop the pdf file into Mendeley.

Open Mendeley and select Mendeley > Tools > Options > BibTeX. Select ‘Escape LaTeX special characters‘, ‘Enable BibTeX syncing‘ and ‘Create one BibTeX file for my whole library‘. Choose a path to sync your bibtex file to. Make sure you choose the same folder as where your R Markdown document lives. If you don’t want to do that, you can copy and paste the file into the  R Markdown document directory, but realise that it is not longer automatically synced if you change something in Mendeley, so you’ll have to do update the bibtex file manually.

Now go to the R Mmarkdown document we made in step 1. In the YAML header, write ‘bibliography: library.bib‘ (unless your .bib file has a different name), so your header will look like something this:

title: "Untitled"
output: html_document
bibliography: library.bib

In Mendeley, check the citation key of the paper you want to cite (in the Document Details tab). In your R Markdown document, write the citation key in square brackets, for example [@Author2000]. Below is an example of the output.


Left: Using citations in R Markdown. Right: .pdf output. Click on image to enlarge.

For information more information on citation syntax have a look at here at the official R Markdown website.

You can also change the citation style. Have a look at this online .csl file database, chances are your preferred journal is on there (these files are usually correct, but check with the actual journal to make sure). Download the file and add it to the same folder as where R Markdown document lives. In your YAML header, add ‘csl: your-preferred-citation-style.csl‘.

Step 3: Adding an equation

Inline equations can be added by writing dollar signs around an equation: ‘$ a + b = c $’. If you want to add an equation on it’s own line, simply add the LaTeX code for the equation (R Markdown will recognise the LaTeX automatically):

a + b = c

The ‘\label{}‘ bit is used to give the equation a name, that you can use to refer to the equation in text, for example by writing: ‘see equation \ref{}‘ or ‘see \autoref{}‘ .I recommend choosing a label name that’s easy to remember and clearly explains what the equation is about. Below is an example of the equation in R Markdown and .pdf output.

You can find some excellent documentation on how to write latex equations here.


Left: Writing equations in LaTeX/R Markdown. Right: .pdf output. Click on image to enlarge.

That’s it for now! In the next post we will look at including figures, tables and R code. If you have any questions, feel free to post them in the comments.