73

I am writing a report with Rmd in RStudio via knitr package.

I want to write a indicator variable symbol in the report, like How do you get \mathbb{1} to work (characteristic function of a set)?.

The line \usepackage{bbm} should be included according to the answer.

I tried first

$$
\usepackage{bbm}
y_{ij} = b_{ij} + \beta_{0} + \beta_{1}
$$

But the program cannot interpret \usepackage{bbm}.

  • It would be great to see a full code in your post. My general advice is that \usepackage{bbm} belongs to the preamble of the document, not to the document body. Meaning, move this part above \begin{document}. – Malipivo Apr 16 '14 at 2:53
  • 6
    @Malipivo This is a question about R Markdown, and I believe it belongs to StackOverflow instead of TeX.SE. – Yihui Xie Apr 17 '14 at 18:52
89

I think this is the easiest option!

---
title: "Title"
author: "Me"
header-includes:
   - \usepackage{bbm}
output:
    pdf_document
---

(Edited to have three, instead of four, hyphens to open and close the YAML front-matter)

  • 1
    This worked more readily than the other answer, which could not find the path to tex. – deepwinter Feb 9 '15 at 15:39
34

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:
    pdf_document:
        includes:
            in_header: 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.:

\usepackage{bbm}
\usepackage{threeparttable}
\usepackage{booktabs}
\usepackage{expex}

Etc. The contents of mystyles.sty are then pasted into the LaTeX preamble; check out the default LaTeX template used here to see where precisely in the preamble they are included.

As a minimal example I tested an Rmd document with the header from above including the .sty from above and having the following in the body of the document:

$$ y_{ij} = b_{ij} + \beta_{0} + \beta_{1} $$

Test test $\mathbb{1}$ test.
7

As of rmarkdown version 1.4 it has been possible to use the extra_dependencies parameter to list a character vector of LaTeX packages. This is useful if you need to load multiple packages:

---
title: "Untitled"
output: 
  pdf_document:
    extra_dependencies: ["bbm", "threeparttable"]
---

If you need to specify options when loading the package, you can add a second-level to the list and provide the options as a list:

output: 
  pdf_document:
    extra_dependencies:
      hyperref: ["unicode=true", "breaklinks=true"]
      lmodern: null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.