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}$$
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.
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.
We indicate a square root using the \sqrt operator.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With