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

mschilli

我正在以书本形式撰写科学报告,并且我希望使用不间断的空格作为成千上万的分隔符,以符合SI / ISO 31-0标准

实际上,我更希望使用不间断的精简空间U+202F/  ),但为简单起见,我们在这里考虑U+00A0/  

我设置了一个knitr钩子来快速执行此操作:

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

只要我不使用任何内联R表达式在数学表达式中返回数值输出> 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=' '),
                                      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);没有任何空格,用逗号,简单空格和不间断的空格作为千​​位分隔符。后者无法在数学上下文中呈现。

为了keep_md: true找出问题所在,我检查了生成的HTML和Markdown()输出,并在第三和第四列中添加了相应的代码,以更好地了解正在发生的情况。

为了清楚起见,这是上述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复制。
    • 我只是提到书本,因为我对书本特定的解决方法很满意。
  2. 这不是rmarkdown问题,因为生成的Markdown看上去与我期望的完全一样。
    • 我只是提到rmarkdown,因为我会对特定于rmarkdown的变通办法感到满意。
  3. 这不是MathJax的问题​​,因为HTML代码已被&替换为纯文本,&amp;并且我不希望它能正确呈现。
    • 无论如何,我将对MathJax相关的解决方法感到满意。
  4. 我怀疑&&amp;在代码和数学上下文中替换pandoc,而在文本上下文中却被替换pandoc
    • 我确定如果有一种方法可以说服pandoc要这样做,那么可以通过rmarkdown YAML标头轻松配置它。

关于如何&nbsp;在数学上下文中将字面上的内容从Markdown转移到HTML的任何想法都可能会帮助我找出其余的内容。


附录:

正如@tarleb指出那样$100&nbsp;0$无效的Latex。但是,手动修改HTML以使其包含内容\(100&nbsp;0\)就可以了,因为MathJax将不间断的空格视为space由于我不关心通过LaTex输出PDF,这意味着将Markdown转换为HTML时,只需不转换$100&nbsp;0$\(100&amp;nbsp;0\)而是转换\(100&nbsp;0\)(就像100&nbsp;0没有转换为那样100&amp;nbsp;0)。

塔莱布

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

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

或者,可以使用pandoc lua过滤器(或可能使用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

到您的文档。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章