在RMarkdown`output ="html_document"`中为数学表达式使用其他Latex软件包. [英] Use additional Latex packages for math expressions in RMarkdown `output = "html_document"`

查看:191
本文介绍了在RMarkdown`output ="html_document"`中为数学表达式使用其他Latex软件包.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用

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

在YAML标头中.

但是,如果指定output: html_document,这当然不起作用.

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

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

对于output: html_document,也可能需要使用附加的Latex-Packages,尤其是在数学表达式中(见下面的MWE)

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

推荐答案

MathJax提供了许多扩展,并且还有第三方扩展.如果无法通过这种方式获得所需的软件包,则事情会变得很困难.

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.

可以使用\newcommand重新创建简单的命令,例如\coloneqq.最简单的方法是通过include-before选项添加它们.将您的MWE与Mathematics Meta SE中的解决方案一起使用,您将获得:

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

输出:

RMarkdown基于 pandoc 构建,该文件执行大多数格式转换. Pandoc通过LaTeX创建PDF(默认情况下),并将仅包含源中提供的所有原始LaTeX命令.当看到\usepackage{mathtools}时,不分析该软件包,而是将命令逐字添加到中间LaTeX中.但是,在导出为HTML时,通过LaTeX命令没有任何意义,因此任何此类命令都将从输出中省略,因此文档中的任何\usepackage都不会影响HTML输出.

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.

如果您使用的是非常复杂的LaTeX软件包,则可以考虑设置一个复杂的管道以继续使用它:例如,可以使用 pandoc过滤器以提取所有方程式,将每个方程式编译为单独的文档,然后将生成的PDF转换为SVG.最后,该SVG然后可以包含在HTML输出中.这是不平凡的,可能不值得付出努力.建议使用类似的方法包含Ti k Z图片.

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.

这篇关于在RMarkdown`output ="html_document"`中为数学表达式使用其他Latex软件包.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆