html 和 pdf 格式的 Rmarkdown 对齐公式
Aligned formulas in Rmarkdown for html and pdf format
我喜欢编写可以有 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 中工作(这很自然,因为这对乳胶来说太多了):
---
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
时,它会删除乳胶输出的数学环境。将脚本另存为 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 前端:
---
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
)。
有一种方法可以直接在您的 Rmd 文件上实现。这是一个例子(方差分析假设检验):
`r outputFormat <- knitr::opts_knit$get("rmarkdown.pandoc.to")
latexalign <- "
\begin{align}
H_0: & \;\; \mu_1=\dots=\mu_J \nonumber \\
H_A: & \;\; \text{some not equal} \nonumber
\end{align}
"
if(outputFormat == 'latex') {
latexalign
} else {
paste0( "$$", latexalign, "$$" )
}
`
您可以将 latexalign
更改为您需要的任何代码,请记住,多余的空格或多余的空行可能会造成问题。
我喜欢编写可以有 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 中工作(这很自然,因为这对乳胶来说太多了):
---
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
时,它会删除乳胶输出的数学环境。将脚本另存为 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 前端:
---
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
)。
有一种方法可以直接在您的 Rmd 文件上实现。这是一个例子(方差分析假设检验):
`r outputFormat <- knitr::opts_knit$get("rmarkdown.pandoc.to")
latexalign <- "
\begin{align}
H_0: & \;\; \mu_1=\dots=\mu_J \nonumber \\
H_A: & \;\; \text{some not equal} \nonumber
\end{align}
"
if(outputFormat == 'latex') {
latexalign
} else {
paste0( "$$", latexalign, "$$" )
}
`
您可以将 latexalign
更改为您需要的任何代码,请记住,多余的空格或多余的空行可能会造成问题。