When working on a data science project in R, one often needs to write up a good-looking report, including some tables, formulas, and funny formatting. In this article, I will show you how to use Latex in RMarkdown to produce beautiful documents.
This article assumes that you have used LaTeX in the past, and just looking for info how to use it in RMarkdown. If you have not used LaTeX before, I suggest you read this first.
Setup for PDF output
When you are using RStudio, there is barely any setup that you need to do. It is recommended to use TinyTeX, a lightweight LaTeX distribution that can be installed right from R console. You need to run this command:
tinytex::install_tinytex()
And that is it! This command will download the latest TinyTeX distribution and unpack it to be used by RMarkdown. Note that this setup is required for PDF output, if you just want to stick to HTML, that you do not have to do anything: it works out of the box.
How to use regular LaTeX commands in RMarkdown
To make use of some basic LaTeX commands in RMarkdown (such as \newpage
), you can simply write them in the document:
If you prefer to use LaTeX commands for text formatting instead of Markdown (such as \textit
), you can do that as well:
Lastly, \begin
and \end
environments are also supported:
How to import LaTeX packages in RMarkdown
If you have worked with LaTeX before, you probably used lots of packages, like amsmath
or enumitem
. It is very easy to import those in RMarkdown. In the header of your .rmd
file, find this section:
This is a YML config for RMarkdown. To add more LaTeX packages, replace pdf_document: default
with this:
pdf_document:
extra_dependencies: ['amsmath', 'someotherpackage']
You can add any number of packages to this list, and TinyTeX will automatically install them.
How to write LaTeX equations in RMarkdown
To write basic inline LaTeX equations in RMarkdown, you need to enclose them in $
:
To write block (multiline) equations, you can use $$
:
If you want to write multiple equations and align them, you usually would use the align
environment. However, it does not work correctly with RMarkdown. To use align
with RMarkdown, use aligned
instead:
For those interested why is this the case: in a normal LaTeX environment, align
also enables the math environment inside, so you do not have to enclose it in $$
. However, Pandoc (which is responsible for Markdown -> Latex translation in RMarkdown) does not do that. If we surround the align
environment with $$
, you will get errors like these: Package amsmath Error: Erroneous nesting of equation structures;
. To get around that, you have to use aligned
, which does not create another math environment inside.
Closing notes
This pretty much covers basic LaTeX usage that you may need when working in RMarkdown. Please let me know what you think and if you had any issues using LaTeX in RMarkdown in the comments!
All of a sudden my Latex equations don’t appear in my Rmarkdown doc. They’ve been problem-free since I started using them years ago, but literally this morning I get nothing but a blank space where normally the eqn would appear. I shut down R and started again and it worked for maybe a second or two, then the equations disappeared again and I can’t retrieve them. Nothing else has been changed except that I tried to install both \usepackage{amsmath} (which seemed to do nothing but screw up e everything) and tinytex, also to no avail (so I removed it). Any ideas? Many thanks.