R Language Including LaTeX Preample Commands


Example

There are two possible ways of including LaTeX preamble commands (e.g. \usepackage) in a RMarkdown document.

1. Using the YAML option header-includes:

---
title: "Including LaTeX Preample Commands in RMarkdown"
header-includes: 
   - \renewcommand{\familydefault}{cmss}
   - \usepackage[cm, slantedGreek]{sfmath}
   - \usepackage[T1]{fontenc}
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, external=T)
```

# Section 1

As you can see, this text uses the Computer Moden Font!

enter image description here

2. Including External Commands with includes, in_header

---
title: "Including LaTeX Preample Commands in RMarkdown"
output: 
  pdf_document:
    includes:
      in_header: includes.tex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, external=T)
```

# Section 1

As you can see, this text uses the Computer Modern Font!

Here, the content of includes.tex are the same three commands we included with header-includes.

Writing a whole new template

A possible third option is to write your own LaTex template and include it with template. But this covers a lot more of the structure than only the preamble.

---
title: "My Template"
author: "Martin Schmelzer"
output:
  pdf_document:
    template: myTemplate.tex
---