111

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

Stella Hu
  • 1,210
  • 2
  • 9
  • 5
  • 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 '14 at 02: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

3 Answers3

130

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)

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.
  • 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 '21 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
    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 https://stackoverflow.com/questions/60722994/inconsistent-processing-of-yaml-lists-in-r-markdown-documents-related-to-loadin.) – user697473 Mar 17 '20 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 07 '20 at 11:33