Rtex with Overleaf

Rtex with Overleaf: A Beautiful Alternative to Rmd

Everything you need to know about Rtex files, how to make them, and how Rtex compares with R Markdown

An open letter to my fellow statistics students,

I decided to major in statistics during my sophomore fall while taking Stat 110: Introduction to Probability because I loved solving fun problems. Coin flipping! Gambling! Game show hosts with a strange affinity for goats! (Oh my!) As the semester progressed, I also began to love writing my own solutions. Each week, I enjoyed learning how to use LaTeX to turn pen-and-paper sketches into typeset documents. By December, I had even created my own custom LaTeX template, complete with macros for different distributions and green boxes for solutions.

Then came Stat 111: Introduction to Statistical Inference which required students to “submit math and R code combined into a single pdf file.” During the course’s R Bootcamp, a few well-meaning teaching fellows told me that I’d only have to write a few lines of code per problem set and that I could use R Markdown to make my documents. Piece of cake, right?

Not exactly. With both R and markdown syntax completely new to me, I ended up all kinds of confused. On the first problem set, I was up until the early hours of the morning, desperately trying to figure out why my R Markdown file wouldn’t knit to pdf.

The teaching fellows weren’t wrong — R Markdown is a great tool to integrate R code, LaTeX, and markdown syntax in documents, and it’s definitely useful if you’re trying to make a document that includes a lot of R code. However, if you’re trying to make a math document and include a few chunks of R code as an enhancement like I was, there’s a much better way to do it, especially if you’re already familiar with LaTeX.

Enter Rtex. Rtex files are excellent if you want to create a LaTeX document with a few executable chunks of R code as an enhancement.

This article explains what Rtex files are, complete with a tutorial on how to create a basic document using Rtex in Overleaf. I’ll also touch on the differences between Rmd and Rtex files so that you can make an informed choice on which document type is best suited to your particular project or problem set.

If you’d like to follow along, here’s a link to a draft of this article in Overleaf.

What is an Rtex file?

While R Markdown files are primarily markdown files that can include LaTeX equations and R code chunks (R, Markdown, LaTeX), Rtex files are primarily LaTeX files that can include R chunks (only LaTeX and R). If you’re used to typesetting using LaTeX, Rtex is the most painless way to start incorporating R into your documents.

Even if you’re already familiar with markdown syntax, however, having a LaTeX skeleton makes it easier to customize the layout and formatting of your document. Though it’s possible to include special LaTeX packages in R Markdown by creating YAML files and special headers, Rtex allows you to use your existing LaTeX templates directly while integrating your R code to make beautiful documents.

How do I make an Rtex file?

There are a few steps to making an Rtex file.

  1. Make a normal LaTeX document: Create a LaTeX file as you normally would. Include all of the LaTeX packages and macros that you would normally use and create whatever document you’d like. Feel free to answer all of the questions on your problem set that don’t involve R and ‘TeX them up. If you don’t already have a LaTeX template, feel free to copy mine above.
  2. Write an R script: Make an R script (any .R file) with things you’d like to include in your document. If there are any dependencies for that R file (ex. referenced .csv files, image files, etc.), be sure to upload those to your Overleaf workspace using the upload button. Make sure that your code works! When it’s ready to be included in your pdf, proceed to step 3.
  3. Change the file extension from tex to Rtex: Simply right-click on the appropriate LaTeX file in the Overleaf GUI (ex. main.tex) and rename it, changing the file extension from tex to Rtex (ex. main.Rtex).
    P.S. Once you switch from tex to Rtex, you lose the feature that allows you to double-click on the Overleaf pdf viewer to scroll your source code to the appropriate location. That’s why I recommend waiting to switch from tex to Rtex until all of your typesetting is complete.
  4. Insert R chunks: Use the following special commands to insert R right into your source code (these are knitr commands):
    # insert R code here

Compile your code, and there you have it! You’ve made your first pdf file using Rtex. If you’ve done everything right, your code and its results should be nicely colored and formatted with your typeset solutions. Isn’t it beautiful?

When should I use Rtex vs. Rmd?

Before deciding whether to choose Rtex or Rmd, I like to ask myself the following question: Am I making a math document with some R code, or am I making a document to present R code with some math?

Rtex files are great for creating theoretical documents that include a few chunks of simple R code as an enhancement. The complicated mathematical equations that you’re accustomed to including in your problem sets can be formatted using all the LaTeX fine-tuning your heart desires.

If you’re creating a document to talk about a bunch of R code that you’ve written, however, then R Markdown might be the better choice. With these documents, it’s often nice to execute your code in small chunks or print things to the console for debugging purposes — two features that aren’t directly available to you when using Rtex with Overleaf.

And there you have it! If you’d like to learn more about Rtex, feel free to check out this link to follow along with this guide on Overleaf or read some additional material on how to include dependency files or special R packages like ggplot2.

Aspiring Data Scientist and Statistician, Undergraduate in Statistics at Harvard University, Former Vice President of the Harvard Open Data Project.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store