# 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‘.

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

\label{eq-abc}
a + b = c

[\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.

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.

## 16 thoughts on “Writing your thesis with R Markdown (2) – Text, citations and equations”

• Excellent recommendation!

• Warning in eval(expr, envir, enclos) :
Unable to read bibliography file(s) ‘library01.bib’, ‘library02.bib’, ‘3m.bib’

why???

1. Hi, I have a question about citations.
Say I have a citation labelled “cit1”. If I write in my .rmd file “[…] @cit1 […]” all works, however if I insert the citation in a latex itemize, it doesn’t. So, if I type in my .rmd file
“[…] \begin{itemize}
\item see @cit1
\end{itemize} […]”
it does not work. Do you know why and how to circumvent this? THX

2. Hello!

I have problems with the characters inside Mendeley bibliography file, how can I avoid this problem? Any thoughts?

Thanks.

3. Hi there, I have a question regarding \ref{}‘ or ‘see \autoref{}, in my YAML I’ve typed \usepackage[spanish]{babel} so my figure captions are in spanish as I wanted, however in the the text it keeps saying e.g. Figure 2.1; I’m guessing that I not the only one with this issue, but I don’t know if it’s related to latex,rmarkdown or knitr. Any thoughts?

4. Hello! As a rmarkdown and mendeley user, I found this post super helpful, so thank you! One issue I’m having is with citing authors that have two part surnames that aren’t hyphenated. I’m pretty sure I have the citation right in Mendeley, and I have the apa.csl file in the same folder as my bib file that’s synced with my Mendeley library, but when I knit to PDF, only the second part of the surname shows up. Did you run into this issue at any point? Any suggestions? Thanks for your help!

• Hi Aki, nice to hear it’s helpful! I don’t remember having an issue like that, but my first guess is that it may have something to do with the .csl or .bib file (though I may be way off here). Do you know what the citation looks like in the .bib file?

5. Rosanna, Thank you very much for this blog. It’s a little late for my thesis, which I wrote in 1970 using an IBM Golfball typewriter (with maths), and a bucket of corrector fluid, but you’ve convinced me that I should use R Markdown for my current project.