Knitting PDF Documents with tinytex

Rmarkdown Tutorials Getting Started

This tutorial walks you through the process of getting RStudio to knit PDF documents with Rmarkdown

Matt Worthington twitter.com/mrworthington (LBJ School Data Initiatives)lbjdata.org
09-22-2021

Using Rmarkdown to produce different kinds of documents is one of the most incredible strengths of Rmarkdown. For grad students in particular who may want to design a template format based on analysis type and then modify that template for different assignments and requirements about submission format, “knitting”1 to various formats (PDF, HTML, Word, and Others*) based on a single document allows for some incredibly efficient workflows.

That said, while HTML documents and Word documents right out of the box, “knitting” to a PDF document requires a few extra (but not difficult) extra steps. So what do you need?

Prerequisites for this Tutorial

For this tutorial, we assume that you’ve done Task 1 and Task 2 of the “Prework” for LBJ Data Studio, which can be found on this page of our website. Alternatively, we’ve organized the instructions as a slide deck which can be viewed here. After completing those tasks, please return to this tutorial.

Steps for Knitting to PDF

Once you’ve completed the prework, it’s pretty easy to get setup whether you’re on a Mac or PC. Just follow these three steps. One is an installation, the second is restarting RStudio, and the last is testing the “Knit to PDF” button. Anyway, here they are:

Step 1: Install tinytex in RStudio

The TLDR version of tinytex is that makes loading everything you need for knitting to PDFs pretty quick and painless. To install tinytex, paste the following code in your RStudio console and hit enter:

install.packages('tinytex')
tinytex::install_tinytex()
# to uninstall TinyTeX, run tinytex::uninstall_tinytex() 

From that point, you should see some activity in the console that shows tinytex is being downloaded and installed on your computer Once that’s done, you’re ready for the next step. In case you’re not familiar with the console, look at this guide for the RStudio Interface.

Step 2: Restart RStudio

Once you’re done with Step 1, just restart RStudio. To restart RStudio, go to the Session tab and select “Restart R”:

Step 3: Knit to PDF

Create an R Markdown Document

After you’ve restarted, create a new R Markdown document by selecting the new file button at the top right. After you’ve selected that button, choose the R Markdown option, select “PDF” as your output format, and use the default settings of the R Markdown Document.

Knit the R Markdown Document to PDF

Once the R Markdown document has been created, you’ll want to select the “Knit” button and then choose “Knit to PDF”. Once you’ve selected “Knit to PDF”, you should see a prompt to save the PDF with a specific filename. Save the R Markdown using the default name of “Untitled” and, after hitting save, you’ll see activity in the console. This the “knitting” magic you’ve heard everyone talk about again and again.

Once the knitting is done, check the location where you saved the file. If you’ve followed these steps, you should see a PDF document called “Untitled.PDF”.

Creating an R Markdown Doc

Knitting R Markdown to PDF

And that’s it! Now, you’re all ready to make PDF docs using R Markdown. If you have any issues, please reach out to us and let us know what kind of error codes or issues you’re dealing with. We can schedule a time to help you.

Photo Credit

Photo by Margarida Afonso on Unsplash


  1. “knitting” is another word for compiling. When I was first trying to wrap my head around it, I thought of it as “printing to”, like “printing to (PDF/HTML/Word) format”. I know you can belabor the translation but, for whatever reason, this kinda langauge helped me understand that “knitting” to something is just a technical term for compiling or “printing to” a particular format.↩︎

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. Source code is available at https://github.com/utexas-lbjp-data/tx_vaccine_site, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Worthington (2021, Sept. 22). LBJ Data Initiatives: Knitting PDF Documents with tinytex. Retrieved from /posts/tinytex/

BibTeX citation

@misc{worthington2021knitting,
  author = {Worthington, Matt},
  title = {LBJ Data Initiatives: Knitting PDF Documents with tinytex},
  url = {/posts/tinytex/},
  year = {2021}
}