108

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}.

2
  • 1
    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, 2014 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, 2014 at 18:52

3 Answers 3

128

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
  • 2
    This worked more readily than the other answer, which could not find the path to tex.
    – deepwinter
    Feb 9, 2015 at 15:39
42

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.
1
  • I believe that this code $\mathbb{1}$ should in fact be $\mathbbm{1}$, in order to utilise the bbm package, as described on page 1 here. Using $\mathbbm{1}$ gives me the correct symbol, whereas $\mathbb{1}$ gives me a "not \Vdash" amssymb symbol.
    – RTM
    Feb 12, 2021 at 19:14
16

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
2
  • 2
    Do you know why this code works when the output is pdf_document but not when it is html_document? (I asked about this problem at stackoverflow.com/questions/60722994/…)
    – user697473
    Mar 17, 2020 at 13:07
  • 2
    this option is not present for beamer_presentation, which is odd since beamer should be a subset of pdf_document family. Error in (function (toc = FALSE, slide_level = NULL, number_sections = FALSE, : unused argument (extra_dependencies = "booktabs") Calls: <Anonymous> -> <Anonymous> -> create_output_format -> do.call Execution halted
    – yonicd
    May 7, 2020 at 11:33

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.