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

1

7

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}$$
Dunseath answered 6/9, 2018 at 10:1 Comment(0)
A
6

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.

Adamski answered 6/9, 2018 at 20:52 Comment(6)
Nice answer! Just out of curiosity: Does this mean it is not possible to add arbitrary \usepackage commands with include-before? And (bonus question): Why not?Octodecimo
Thanks, @Octodecimo I've updated the answer to include a bit more info on the why, and how one might work around this.Adamski
Thanks for the update. However, I'm not quite sure if I can follow (or if I'm convinced). Of course, having a verbatim usepackage command in HTML is pointless. However, as your answer shows, include-before allows to provide LaTeX commands which pandoc (somehow) injects into the TEX file. This raises my question why the same option cannot be used to provide usepackage commands that pandoc injects into the LaTeX preamble.Octodecimo
MathJax has (limited) LaTeX capabilities and can interpret commands like \newcommand -- as long as those commands are given in a math environment. This is what I'm using in my answer, since I perceived the focus of this question to be on MathJax. You are right in that one could do the same using pandoc's features, as pandoc supports LaTeX macros in Markdown. However, interpreting complex LaTeX other than math, SI units, and macros is out of scope for pandoc, hence the current behavior.Adamski
Thank you for the clarification, I've learned a lot. All these weird complications are exactly the reason why I don't like all the Rmarkdown stuff and prefer plain RNW documents. But maybe I'm a dinosaur ...Octodecimo
Seems like the url link is obsolete?Lafond

© 2022 - 2024 — McMap. All rights reserved.