我喜欢编写可以有 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/