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

别来无恙 提交于 2020-05-13 05:05:27

问题


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

回答1:


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:

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.



来源:https://stackoverflow.com/questions/52201541/use-additional-latex-packages-for-math-expressions-in-rmarkdown-output-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!