4 min read

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:

[caption id=“attachment_231” align=“alignnone” width=“584”]heading_example Left: Writing headings in R Markdown. Right: .pdf output. Click on image to enlarge.[/caption]

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:

[code]

title: “Untitled” output: html_document

bibliography: library.bib

[/code]

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.

[caption id=“attachment_234” align=“alignnone” width=“584”]citation-example Left: Using citations in R Markdown. Right: .pdf output. Click on image to enlarge.[/caption]

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

[code][/code] \begin{equation} \label{eq-abc} a + b = c \end{equation} [\code]

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.

[caption id=“attachment_237” align=“alignnone” width=“584”]equation-example Left: Writing equations in LaTeX/R Markdown. Right: .pdf output. Click on image to enlarge.[/caption]

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.