html - Rmarkdown 中 html 和 pdf 格式的对齐公式

标签 html latex r-markdown pandoc

我喜欢编写可以有 html 输出和 pdf 输出的 rmd 文件。在我的大多数用例中都是有效的。只是公式的对齐不起作用,因为我需要 html 中的额外美元符号:

以下内容适用于 HTML:

---
title: "Test"
author: "RW"
date: "Monday, February 15, 2016"
output: html_document
---

Testing formulae

$$
\begin{align} 
              y &= x \\
              z &= a
\end{align}
$$

我必须删除 $ 才能使其在 pdf 中工作(这很自然,因为这对于 latex 来说太多了):

---
title: "Test"
author: "RW"
date: "Monday, February 15, 2016"
output: pdf_document
---

Testing formulae

\begin{align} 
              y &= x \\
              z &= a
\end{align}

有没有办法让它在 html 和 pdf 中工作?

最佳答案

您可以使用自定义 pandoc filter 来做到这一点。当且仅当它使用 align 时,才会删除 latex 输出的数学环境。将脚本另存为 math.py 位于 Rmd 文件的路径或同一目录中

#!/usr/bin/env python

from pandocfilters import toJSONFilter, RawInline, stringify
import re

align = re.compile("\\\\begin{align}")


def math(k, v, f, meta):
    if k == 'Math' and f == 'latex' and re.search(align, v[1]):
        return RawInline('latex', v[1])


if __name__ == "__main__":
    toJSONFilter(math)

并将其添加到您的 yaml front Matter 中:

---
title: "Test"
author: "RW"
date: "Monday, February 15, 2016"
output: 
  pdf_document:
    pandoc_args:
    - --filter
    - definition_to_bold.py
---

Testing formulae

$$
\begin{align} 
              y &= x \\
              z &= a
\end{align}
$$

现在,使用周围的 $$align 环境编写的所有方程都可以在 Latex 中运行。

您将需要 python 和 pandocfilters 库 (pip install pandocfilters)。

关于html - Rmarkdown 中 html 和 pdf 格式的对齐公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35413688/

相关文章:

javascript - 将社交媒体网络共享按钮集成到 Highcharts。

Java - 当我尝试获取源代码时,服务器返回了 HTTP 响应代码 : 400

latex - TeX:字符串解析和空格剥离

matlab - 如何在 Matlab 图形中使用非 ASCII 字符(用于 LaTeX 文档)?

HTML 下拉菜单被图像隐藏

python - Pandas to_latex() 转义数学模式

r - rmarkdown 中的 YAML 当前日期

r - 从 RMarkdown 代码块生成 Latex 代码

R 3d 绘图的旋转在 knit 和 webgl 中无法正常工作

javascript - 将一个 div 置于另一个 div 中心的最佳方法是什么?