These notes describe steps to take if you want to knit your Rmarkdown files into PDF documents. While HTML documents are nice for static or dynamic documents that don’t need a page structure, PDFs are nice for static documents that need a page structure. This document borrows from two Medium posts by Søren L Kristiansen (Windows, Mac) and Yihui Xie’s excellent R Markdown: The Definitive Guide. (Xie’s website is wonderful, with many meditations on life and programming. Worth a visit.)

LaTeX

The main thing you’ll need to knit your Rmd files into PDFs is some LaTeX tools. LaTeX (wiki, site, pronounced lay-tech or lah-tech) is a typesetting software used extensively in academia, particularly subjects like mathematics, economics, political science, and computer science. The idea is simple: writers have a comparative advantage in writing rather than formatting, so it would be nice if writers could use a writing tool which handled the formatting for them programmatically. Such a tool would save writers time so they could focus on writing.

A little history

In the old days, LaTeX always lived on your computer. There were/are multiple systems (a TeX distribution, a package manager like MikTeX (pronounced mick-tech) or MacTeX (pronounced mack-tech), and then an IDE like TeXStudio). In some ways the ecosystem is similar to what we use with R and RStudio: R (TeX) is the underlying program, RStudio (TeXStudio) is the IDE where we actually do work, and new capabilities can be added by downloading and installing packages (there is no R equivalent of MikTeX or MacTeX because R itself tracks which packages are installed and handles installation). Nowadays you don’t need to have LaTeX on your computer at all, as websites like Overleaf have moved it all to the cloud. This is quite helpful, as managing LaTeX packages locally could get annoying at times.

What you’ll actually need for knitting PDFs in Rmarkdown

To knit Rmarkdown files into PDFs, you don’t need an IDE like TeXStudio. All you need is to download and install MikTeX (Windows) or MacTeX (Mac)!

Installing MikTeX on Windows

Most of you will likely have 64-bit operating systems these days, but the steps will be similar regardless.

  1. Go to http://miktex.org/download

  2. By default, it should be on the Windows tab and below it, on the installer tab. Click the download button.

  3. When the download is complete, run the downloaded installer. Windows may ask if you want to allow the app to make changes to your PC; if it does, click Yes.

  4. Go through the installation. I recommend keeping the default settings. Once you Next through all the menus and click Start, you can go make some tea or something while it installs.

  5. When it’s done, Next through till you get to the Close button. Click that, and you’re done!

Installing MacTeX on Mac

MacTeX 2020 requires Mac OS X 10.13 or above (High Sierra, Mojave, or Catalina). If you don’t have one of those, you’ll need to get a different installer but the steps will be similar.

  1. Go to http://www.tug.org/mactex/mactex-download.html

  2. Click the MacTeX.pkg link. It’s big—around 4GB—so you may want to stretch or go for a walk or something while it downloads.

  3. When the download is complete, move the file MacTeX.pkg somewhere convenient (e.g. the desktop) and double-click it to install.

  4. Go through the installation. I recommend keeping the default/standard options.

  5. When it’s done, it’ll say “Success”. If it gets stuck on “Verifying…”, just reboot your computer and install MacTeX again.

Generating a PDF in Rmarkdown

To generate a PDF, you’ll update the “header” (formally, the “front-matter”) of your Rmd file. You’ll change the line where it says output: html_document to read output: pdf_document. That is, you’ll go from something like this:

to something like this:

and then knit like you usually would!

Putting math into Rmarkdown

As a side benefit, having MikTeX/MacTex installed will allow you to create beautifully typeset equations in your Rmd files, like the (maybe false) equation below:

\[\forall (x,y) \in \mathbb{R}^2, \exists (a,b)\in \mathbb{R}^2_{++} : ~~ e^x + e^y = \frac{a}{b}\] Groovy!

To put math equations inline (e.g. \(f(k) = {n \choose k} p^{k} (1-p)^{n-k}\)), use a single pair of dollar signs like so: $f(k) = {n \choose k} p^{k} (1-p)^{n-k}$. To put math expressions on a new line like the maybe-false example above, put the equation between a pair of double dollar signs like so: $$f(k) = {n \choose k} p^{k} (1-p)^{n-k}$$, which produces \[f(k) = {n \choose k} p^{k} (1-p)^{n-k}\] Make sure you don’t leave any spaces between the dollar signs and the first/last piece of the equation.