Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use additional Latex packages for math expressions in RMarkdown `output = "html_document"`

I`m aware how to use additional Latex-packages for pdf-format output from .Rmd files using

---
header-includes:
   - \usepackage{mathtools}
---

in the YAML header.

However, this does (of course) not work if one specifies output: html_document.

---
output: html_document
header-includes:
   - \usepackage{mathtools}
---

Using additional Latex-Packages could be of interest for output: html_document, too - especially in math expressions (MWE below)

---
title: "MWE"
output: html_document
header-includes:
   - \usepackage{mathtools}
---

## Use "Defined by" Symbol

$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
like image 387
sammerk Avatar asked Sep 06 '18 10:09

sammerk


People also ask

Can you use LaTeX packages in R markdown?

With the rmarkdown package, RStudio/Pandoc, and LaTeX, you should be able to compile most R Markdown documents. You will also need to install a bunch of additional R packages on which rmarkdown depends. TinyTeX is a lightweight, portable, cross-platform, and easy-to-maintain LaTeX distribution.

How do you write math in R markdown?

Math inside RMarkdownIn side a text chunk, you can use mathematical notation if you surround it by dollar signs $ for “inline mathematics” and $$ for “displayed equations”. Do not leave a space between the $ and your mathematical notation. Example: $\sum_{n=1}^{10} n^2$ is rendered as ∑10n=1n2.

How do you write square root in R markdown?

We indicate a square root using the \sqrt operator.


1 Answers

MathJax offer a number of extensions, and there are third-party extensions as well. If your desired package is not available in this way, then things get difficult.

Simple commands, such as \coloneqq, can be recreated using \newcommand. The simplest way is to add these via the include-before option. Using your MWE with a solution from Mathematics Meta SE, one gets:

---
title: "MWE"
output:
  html_document: default
include-before:
- '$\newcommand{\coloneqq}{\mathrel{=}}$'
---

## Use "Defined by" Symbol

$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$

Output:

Screenshot of MWE exported as HTML

Background

RMarkdown is build around pandoc, which performs most of the format conversions. Pandoc creates PDF via LaTeX (by default), and will simply include any raw LaTeX commands which are given in the source. When seeing \usepackage{mathtools}, the package is not parsed, but the command is simply added verbatim to the intermediate LaTeX. However, when exporting to HTML, it wouldn't make sense to pass through LaTeX commands, so any such command will simply be omitted from the output, so any \usepackage in your document won't effect the HTML output.

Alternative solution

If you are using very complex LaTeX-packages, then you could consider setting up a complex pipeline to still use it: E.g, one could use a pandoc filter to extract all equations, compile each equation as a separate document, and then convert the resulting PDF to SVG. Finally, that SVG can then be included in the HTML output. This is non-trivial and probably not worth the effort. A similar approach is recommended to include TikZ pictures.

like image 157
tarleb Avatar answered Sep 22 '22 04:09

tarleb