knitr and Latex Documents

programming
Published

June 27, 2022

Rmarkdown seems to be the most standard way to embed R code and results into a document. Markdown has many benefits compared to Latex: it is much easier to get started with and the source code is closer to a plain readable text document. It is also possible to use Latex within Markdown when an occasional equation is needed. But sometimes you really want to work in Latex without going through Markdown.

I have been aware that Sweave allows embedded R for Latex documents, but it seemed archaic compared to more modern R tools for reproducible research, and I avoided using it. Recently, I was made aware that Overleaf supports authoring Sweave documents which require very little special markup and are almost like working in regular Latex. After some additional searching, I found that this is also possible without Overleaf. knitr has long been able to do this.

Examples of Sweave with knitr can be found online, but I thought it would be worthwhile to post several more. See the repo https://github.com/andrewraim/sweave-examples for an example in article format (pdf) and one in Beamer slide format (pdf).