【问题标题】:Use an escaped percent sign in TeX and MathJax with pandoc markdown在 TeX 和 MathJax 中使用带有 pandoc markdown 的转义百分号
【发布时间】:2017-10-23 11:19:45
【问题描述】:

很容易在 Markdown 文档中编写 TeX 方程并将它们转换为实际排版方程 using pandoc(1.18 版),无论是在 PDF 文档(通过 LaTeX)还是 HTML 文档(通过 MathJax)中:在 @987654330 中环绕数学@ 用于内联方程,$$...$$ 用于块方程。

但是,MathJax 和 TeX 语法之间似乎存在差异,带有 % 等特殊字符。例如,考虑这个示例文档:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

$$\text{\% change} = \frac{x_2 - x_1}{x_1} \times 100$$

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

使用命令通过 LaTeX 将其转换为 PDF

pandoc test.md -o test.pdf

正确生成未转义的百分号:

但是,使用 MathJax 将相同的文档转换为 HTML,使用命令

pandoc test.md -s --mathjax -o test.html

错误地产生了一个转义的百分号:

目前,在将文档转换为 PDF/HTML 之前,我一直在手动转义/取消转义百分号,这似乎违背了拥有可以转换为任何格式的主 Markdown 文档的目的。

在 TeX 和 MathJax 中处理转义百分号的正确方法是什么? MathJax 中是否有允许转义特殊字符的设置?我需要告诉 LaTeX 在数学模式下接受未转义的百分号吗?

【问题讨论】:

标签: latex pandoc mathjax


【解决方案1】:

(我知道这没有在 [R] 下标记,但我知道 Andrew 是一个狂热的 R 和 Rmarkdown 用户,所以我希望这就足够了。)

这可以使用 Rmarkdown 和 knitr。下面的脚本在使用rmarkdown::render 渲染时会产生所需的输出。我还创建了一个gist of the script

---
title: TEST
output:
    pdf_document:
        latex_engine: xelatex
    html_document:
        fig_caption: true
---

```{r, echo = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  echo = TRUE,
  cache = FALSE,
  comment = "#>",
  fig.path = "fig-",
  fig.pos = "h"
)

# this will check what the output format is going to be
getOutputFormat <- function() {
  knitr::opts_knit$get("rmarkdown.pandoc.to")
}
```


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

```{r, warning=FALSE, message=FALSE, echo=FALSE, eval=TRUE, results='asis'}
s <- "$$\\text{\\% change} = \\frac{x_2 - x_1}{x_1} \\times 100$$"
if(getOutputFormat() == 'html') s <- gsub('\\\\%', '%', s) # dont escape if html
knitr::asis_output(s)
```

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

【讨论】:

    猜你喜欢
    • 2013-04-07
    • 2013-12-29
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2013-01-14
    相关资源
    最近更新 更多