在HTML输出的[R] Markdown数学表达式中插入不间断空格

时间:2018-01-12 17:03:18

标签: markdown r-markdown pandoc mathjax bookdown

我正在书记中撰写科学报告,我想使用non-breaking space作为SI/ISO 31-0 standard之后的千位分隔符。

实际上,我希望不会破坏thin spaceU+202F / &#8239;)但是为了简单起见,我们在这里考虑U+00A0 / &nbsp;。< / p>

我设置了一个knitr挂钩来动态执行此操作:

knitr::knit_hooks$set(inline=function(output)
                               ifelse(is.numeric(output),
                                      prettyNum(round(output, 1),
                                                big.mark='&nbsp;'),
                                      output))

只要我不使用任何返回数字输出的内联R表达式&gt;这就可以正常工作。数学表达式中的999。

以下的预订MWE说明了问题:

---
output:
  bookdown::html_document2: default
---
```{r set-output-hook, include=FALSE}
knitr::knit_hooks$set(inline=function(output)
                               ifelse(is.numeric(output),
                                      prettyNum(round(output, 1),
                                                big.mark='&nbsp;'),
                                      output))
```

This works:
The product of $\pi$ and `r 1000` is `r pi*1000`.

This fails to render: 
$\pi\cdot`r 1000`=`r pi*1000`$

This renders but is cumbersome as it requires me to know *a priori* which
values might exceed 999:
$\pi\cdot1000=`r as.character(round(pi*1000, 1))`$

我试图追踪它并提出以下rmarkdown MWE:

---
output:
  rmarkdown::html_document:
    keep_md: true
---

| Rmarkdown    | Render     | HTML                                                | Markdown     |
|--------------|------------|-----------------------------------------------------|--------------|
| `1000`       | 1000       |`1000`                                               | `1000`       |
|`$1000$`      |$1000$      |`<span class="math inline">\(1000\)</span>`          |`$1000$`      |
|              |            |                                                     |              |
|  `100,0`     | 100,0      |`100,0`                                              | `100,0`      |
|`$100,0$`     |$100,0$     |`<span class="math inline">\(100,0\)</span>`         |`$100,0$`     |
|              |            |                                                     |              |
|  `100 0`     | 100 0      |`100 0`                                              | `100 0`      |
|`$100 0$`     |$100 0$     |`<span class="math inline">\(100 0\)</span>`         |`$100 0$`     |
|              |            |                                                     |              |
|  `100&nbsp;0`| 100&nbsp;0 |`100 0`                                              | `100&nbsp;0` |
|`$100&nbsp;0$`|$100&nbsp;0$|`<span class="math inline">\(100&amp;nbsp;0\)</span>`|`$100&nbsp;0$`|

表格的前两列足以看出问题所在: 每对行在文本和数学上下文中显示数字 1000 1 000);没有任何空格,带有逗号,具有简单的空间,并且具有不间断的空间作为千位分隔符。 后者无法在数学上下文中呈现。

为了追踪问题,我检查了生成的HTML和Markdown(keep_md: true)输出,并将相应的代码添加为第3列和第4列,以便更好地了解正在发生的事情。

为清楚起见,以下是上面的rmarkdown MWE的调整版本,用_替换简单空格,用HTML和Markdown输出列中的-替换非空格:

---
output:
  rmarkdown::html_document:
    keep_md: true
---

| Rmarkdown    | Render     | HTML                                                | Markdown     |
|--------------|------------|-----------------------------------------------------|--------------|
| `1000`       | 1000       |`1000`                                               | `1000`       |
|`$1000$`      |$1000$      |`<span_class="math_inline">\(1000\)</span>`          |`$1000$`      |
|              |            |                                                     |              |
|  `100,0`     | 100,0      |`100,0`                                              | `100,0`      |
|`$100,0$`     |$100,0$     |`<span_class="math_inline">\(100,0\)</span>`         |`$100,0$`     |
|              |            |                                                     |              |
|  `100 0`     | 100 0      |`100_0`                                              | `100_0`      |
|`$100 0$`     |$100 0$     |`<span_class="math_inline">\(100_0\)</span>`         |`$100_0$`     |
|              |            |                                                     |              |
|  `100&nbsp;0`| 100&nbsp;0 |`100-0`                                              | `100&nbsp;0` |
|`$100&nbsp;0$`|$100&nbsp;0$|`<span_class="math_inline">\(100&amp;nbsp;0\)</span>`|`$100&nbsp;0$`|

所以我可以告诉你

  1. 这不是一个减记的问题,因为它可以通过简单的rmarkdown复制。
    • 我只是提到了bookdown,因为我对特定于bookdown的解决方案感到满意。
  2. 这不是一个rmarkdown问题,因为生成的Markdown看起来与我期望的完全一样。
    • 我只是提到了rmarkdown,因为我会对特定于rmarkdown的解决方案感到满意。
  3. 这不是MathJax问题,因为HTML代码将&替换为&amp;,我不希望这样做正确呈现。
    • 无论如何,我会对与MathJax相关的解决方法感到满意。
  4. 我怀疑是pandoc在代码和数学上下文中用&替换&amp;但在文本上下文中没有。
    • 我确定如果有一种方法可以说服pandoc 而不是这样做,可以通过rmarkdown YAML标头轻松配置它。
  5. 关于如何在数学上下文中将&nbsp;从Markdown转换为HTML的任何想法可能有助于我弄清楚其余部分。

    附录:

    pointed out @tarleb$100&nbsp;0$无效Latex。 但是,手动修改HTML以包含\(100&nbsp;0\)可以正常工作MathJax treats non-breaking spaces as spaces。 由于我不关心通过LaTex输出PDF,这意味着根本不会将$100&nbsp;0$转换为\(100&amp;nbsp;0\)而是转换为\(100&nbsp;0\)(就像100&nbsp;0未转换为100&amp;nbsp;0一样当将Markdown转换为HTML时,我将需要它。

1 个答案:

答案 0 :(得分:2)

Pandoc希望数学环境包含LaTeX数学标记,而不是HTML。当pandoc尝试输出$100&nbsp;000$作为LaTeX时,转换失败,但这会导致\(100&amp;nbsp;000\)而非您的意图。

作为一种解决方案,您可以尝试在钩子中使用文字窄的不间断空间 unicode字符“”。

或者,可以使用pandoc lua filter(或可能是R pandoc-filter)来强制pandoc不加改变地传递数学内容:

-- filename: force plain math
function Math (el)
  if el.mathtype == 'DisplayMath' then
    return pandoc.RawInline('html', '\\[' .. el.text .. '\\]')
  else -- InlineMath
    return pandoc.RawInline('html', '\\(' .. el.text .. '\\)')
  end
end

保存到文件并添加

使用它
output:
  bookdown::html_document2:
    pandoc_args: --lua-filter=force-plain-math.lua

到你的文件。