The marriage between R and LaTeX- Part 4 (R code inside LaTeX)

In the previous article in this series (Part 3), I explained how to use the xtable package to generate tables in HTML and  to generate tables that can be included into a LaTeX document.

There is still a method that I would like to discuss in this article which is using the knitr package in R. The knitr package allows you to embed any R code within any LaTeX document. Sort of the opposite way of what I discussed in previous articles. It is installed automatically in case you have installed the rmarkdown package in R or if you are using RStudio. Actually It comes bundled with RStudio.

I will dedicate few articles in the future to talk about the beauty of R Markdown markup language , the rmarkdown package and the many usages that this language is designed for.

To install knitr package use: install.packages(“knitr”). The file extension of the file where we are going to put our LaTeX code mixed with R Code is not  .tex as you would have imagined. The file type is .Rnw. The reason why, this is needed is because this is the file type  read by the knit(..) function inside the knitr package.

First in your R console load the knitr package as follows:


Create a file named test.Rnw ( the extension should be .Rnw).  This file should reside in the working directory of your R session. Of course you can change at anytime the working directory by issuing the following command with the path that is convenient to you:  setwd(“C:\\myplots”) 

Now using the knit() function issue the following:


This will output a LaTeX document as test.tex in your working directory. I should mention something obvious here that you should have LaTeX completely installed and all your LaTeX compilers working fine (pdflatex etc…).  You can use a helper function to generated directly a PDF out of it as follows:


This will generate a PDF document named test.pdf.

Now to the most important part, how this actually work?

Content of the Rnw file

The content of the Rnw file is exactly like a normal LaTeX document except that when you want to include R code you do it by 2 methods:

  • Including inline R code between you LaTeX code is done  by using \Sexpr{ R CODE HERE }.
  • A block R Code starts with the delimiter <<options>>= and should end with an @ symbol.

Don’t worry,  I will give plenty of examples later. The “options” is a set of options separated by commas in the form of: option1=X,option2 = Y…… These options governs many things.The following is a list of what these options could be:

  • echo: whether R source code should be included in the output LaTeX document or not. It is either TRUE or FALSE
  • results: whether  to hide the results :”hide” ; or output them: “asis”
  • warning: whether to include warnings in the output (TRUE) ot not (FALSE). You know sometimes the R codes gives warning. The idea here: do you want these to be included in the final LaTeX or PDF document or not?
  • message: whether whether to include informational messages in the output (TRUE) ot not (FALSE). You know sometimes the R codes gives informational messages. The idea here: do you want these to be included in the final LaTeX or PDF document or not?
  • error: whether whether to include errors in the output (TRUE) ot not (FALSE). You know sometimes the R codes gives errors. The idea here: do you want these to be included in the final LaTeX or PDF document or not?
  • fig.width: Specifies the width of plots in inches
  • fig.height: Specifies the height of plots in inches

The following is the content of test.Rnw. In the first R block surrounded by <<…>>= and the @ the source of R and results are hidden won’t show in the final output. The second one, show the source code and the result. Last example shows how you can put a plot R code inside a LaTeX figure environment.

\section{Including R code into LaTeX} \label{sec:RandLaTeX}
If you do not want to show the source code of R and hide the results like in the following case :

<<echo=FALSE, results=’hide’>>=
Firstto20thRowTable <- tli[1:20, ]

Then probably you want to show what is going on here:

<<echo=TRUE, results=’asis’>>=

xtable(Firstto20thRowTable, caption=c(“This is a very long caption appearing below to the table”, “I appear in the list of tables only”))

So what about LaTeX figures, did you know you can include a block of R code inside a figure environment like what follows:


<<echo=FALSE, fig.width=4,fig.height=3>>=
counts <- table(mtcars$gear)
barplot(counts, main=”Car Distribution”, xlab=”Number of Gears”)

\caption{Car Distribution}

You can know the number of the rows of our data by giving an inline R code as \Sexpr{nrow(Firstto20thRowTable) }


Still one last example that is the use of \Sexpr{…}. As you can see I have used the nrow() function inside this LaTeX command to get the number of row in Firstto20thRowTable.

Hope you enjoyed reading this article. Please leave any comments or suggestions below.