Learn to use R in LaTeX to create dynamic documents with Overleaf and Knitr.

CiteDrive
3 min readJun 27, 2022

--

Knitr lets you insert and execute R code in LaTeX documents and read data into a LaTeX template for dynamic output. You may know Knitr from RMarkdown or Quarto to do the same in Markdown-based documents, and you will find that the handling is very similar.

Getting started with Overleaf

After creating a new project, the first step in Overleaf is to save an “.Rtex” file, which would be the pendant to “Rmd” in R Markdown.

We can define a minimum set-up for your LaTeX, or Rtex enviroment:

\documentclass{article}
\begin{document}
The Euclidean algorithm in R code calculates the greatest common divisor of two numbers.\end{document}

Using R Code in offline-LaTeX documents

For other latex editors, the following guide is very similar, except that you save the file as *.Rnw. Please note that Knitr is pre-installed on Overleaf, and this guide assumes this is the case on your computer. To convert the document to a PDF:

R -e 'library(knitr);knit("main.Rnw")'

R as code chunk the LaTeX document

To include R code in the document, we can specify it with the following delimiters:
We start with <<>>= and end with @ at the end of the code chunk in R.

In this example, we define a function to calculate the greatest common divisor of two integers using the Euclidean algorithm:

\documentclass{article}
\begin{document}
The Euclidean algorithm in R code calculates the greatest common divisor of two numbers.<<>>= gcd <- function(a, b) {
while(b != 0) {
h = a %% b
a = b
b = h
}
return(a)
}
gcd(3780,3528)
@
\end{document}

Then when compiling in Overleaf, the following output comes:

Example Codechunk

Notice that after calling the function, the result is output in the code chunk, namely [1] 252.

Options in Codechunk

Knitr comes along with a few options which you declare within<<>>= . For Example<<background= '#000000'>>= will display everythin in black.

R as or inline code in the LaTeX document

With the help of inline code, it is possible to output R-expressions within the body text of an *.Rtex file by simply placing the code in $\Sexpr{RCODE}$. RCODE here is any R expression you can define between the brackets. You can also call a function from a previous section of R code, such as gcd from our example.

Example inline code

The sum of the natural numbers from 1 to 100 is equal to $\Sexpr{1:100}$ and the greatest common divisor of 143 and 65 is$\Sexpr{gcd(143,65}$

External Scripts and Data

You can easily upload or attach external data or R Scripts in the Overleaf project and declare for data:

data = read.csv(file="data.txt", head=TRUE,sep=" ")

An for external R Scripts:

read_chunk("gcd.R")

That’s it — and that’s how easy it is to use R in Overleaf or LaTeX.

CiteDrive

This guide is brought to you by CiteDrive, a web-based, collaborative, and BibTeX-based app for managing your references, bibliography, and citations that links directly to many publishing systems, including Overleaf. Overleaf’s blog post: CiteDrive — Easy Reference Management for Overleaf | Overleaf.com, Online LaTeX Editor.

--

--

CiteDrive
CiteDrive

Written by CiteDrive

CiteDrive: Cloud-based BibTeX manager. Enables easy collaboration, auto-syncing, and multi-format imports/exports. Research-focused, distraction-free.

Responses (1)