If you have to create a report, LaTeX is definitely the choice to make, everything looks better in LaTeX!

Chances are that you used it to write your thesis or some assays, back in your academia years. Indeed, environments like texstudio or the more recent Overleaf are great for single projects. But now in your day job, can they really scale it up and make LaTeX still useful?

What you need is a method that generates reports automatically (maybe, just there at the end of your pipeline), that changes its content dynamically in relation to the findings of your code, that is visually pleasing (for your boss), and that can be fast enough to mass-produce results.

If that is the case, my favourite solution is the R package “knitr” that allows us to create LaTeX documents directly from R code. Combining the flexibility of a programming language and the LaTeX aesthetics.

In this article, I want to give you a basic tutorial and some tips on how to do it successfully.

I understand that not many know both R and LaTeX, I will try to keep it simple, but both coding systems and their combination cannot be explained in one single article, I hope that, if you have clicked on this article and read it until now, you have some basic knowledge on the two.

Without further ado, let’s create a LaTeX report with R.


Two are the steps needed to create a R/tex report, a ‘starter’ R script to create the final pdf, and the .Rnw file where all our code will be stored. Let’s see them:

The starter script

The ‘starter’ script has to do two basic functions:

  1. Load and pass the Rnw file to the knit command of the **knitr **package. This process is called weaving. This will execute the R code and produce a series of files among which there’s a .tex file that we would normally use in texstudio.
  2. Execute twice the .tex file in order to create the final PDF report

#data-science #pdf #latex #knitr #r #data analysis

How to combine LaTeX and R for report generation
2.25 GEEK