Latex In R Markdown



I often need to write short reports which are not full blown manuscripts, e.g. annual grant progress reports. Though such documents don’t need to adhere to a strict template, I still want them to look nice. I’ve accomplished this for years by writing directly in LaTeX, but I want to align my process with my recent transition to composing most docs in RStudio/Rmd. Ultimately though, I don’t want to abandon the LaTeX look in the compiled document. Thankfully, RStudio will render a LaTeX pdf, but formatting beyond the defaults (which are still nice!) can be a bit mysterious. This repository holds my working template for such purposes.

  1. Latex Table In R Markdown
  2. Latex In R Markdown Example
  3. Rmarkdown Tinytex
  4. R Markdown Download

R Markdown is one of the document formats that knitr supports, and it is probably the most popular one. I have been asked many times about the choice between Markdown and LaTeX, so I think I’d better wrap up my opinions in a blog post. As per this page on the R Markdown website, you can add whatever you want to the preamble via the in-header option in the YAML header; e.g.,- title: 'Titre' date: Fecha output: pdfdocument: includes: inheader: mystyles.sty - In mystyles.sty, located in the same directory as the.Rmd, you could have a whole list of additional things to add to the preamble of document, e.g. Converting knitr/LaTeX to PDF RStudio. You can use RStudio to convert a.Rnw file to PDF and preview the result, in the same way you worked with R Markdown. But the default in RStudio is still to use Sweave, so you first need to change that default.Go to the RStudio (on menu bar) → Preferences and select Sweave on the left. Determine whether you’re running 32-bit or 64-bit Windows. Before installing anything, you should.

9.1 LaTeX or HTML output. LaTeX and HTML are two commonly used output formats. The function knitr::islatexoutput tells you if the output format is LaTeX (including Pandoc output formats latex and beamer).Similarly, the function knitr::ishtmloutput tells you if the output format is HTML. By default, these Pandoc output formats are considered HTML formats: markdown, epub, html, html4.

Here’s a minimal example of what the defaults within a .Rmd will give you:

Latex Table In R Markdown

Now, two specific things I’d like to change are:

  • Left-justify the title/author/date section
  • Modify the font specs used in section titles

A solution to these two problems easily generalizes to the broader question of “How do I format the title and H1-H6 specs in the context of LaTeX rendering from .Rmd files?”

To start, we will borrow the LaTeX template R Markdown is currently using (h/t SO). The relevant remote repo is here, and you can copy the local version you’re using into your working directory with this line:

If you look past the pandoc nastiness in this template file (I at least find it nasty, being that I was mostly unfamiliar with pandoc scripting!), you’ll see familiar LaTeX commands that are often surrounded by $if(X)$ statements that are triggered if X appears in your .Rmd YAML. Here’s a straightforward example where, if you have title: in your .Rmd YAML header, the maketitle command will be executed in your LaTeX render:

Now the problem feels more tractable: all we have to do is modify the maketitle defaults in the usual LaTeX manner within the template.tex document. Let’s start with the following:

And don’t forget to include template.tex in your .Rmd YAML header like so:

Rendering gives the below, nice!

Next, we clearly need to fix the fact that section titles are now larger than the document title! Let’s do this with the LaTeX sectsty package – you can basically stuff this code anywhere in the preamble, like so:

Latex In R Markdown Example

Markdown math symbols

Let’s also reduce the overall margins a touch via the geometry argument in the YAML while we’re at it. Here’s the full .Rmd:

Rmarkdown Tinytex

Mission accomplished!

Here’s a bonus I learned along the way. You can send custom arguments to your .tex doc by simply defining new variables in the YAML header (relevant info in the pandoc docs here). Suppose I want an optional subtitle parameter. This is accomplished like so:

With this trick, you can start to do even fancier things (literally), such as include fancyhdr options. This option is ultimately included in the template.tex provided in the repository, and here’s the relevant YAML and output:

R Markdown Download

Happy R Markdowning!