我正在以书本形式撰写科学报告,并且我希望使用不间断的空格作为成千上万的分隔符,以符合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 0`| 100 0 |`100 0` | `100 0` |
|`$100 0$`|$100 0$|`<span class="math inline">\(100&nbsp;0\)</span>`|`$100 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 0`| 100 0 |`100-0` | `100 0` |
|`$100 0$`|$100 0$|`<span_class="math_inline">\(100&nbsp;0\)</span>`|`$100 0$`|
所以从我所能知道的
&
替换为纯文本,&
并且我不希望它能正确呈现。
&
是&
在代码和数学上下文中被替换为pandoc,而在文本上下文中却被替换为pandoc 。
关于如何
在数学上下文中将字面上的内容从Markdown转移到HTML的任何想法都可能会帮助我找出其余的内容。
附录:
正如@tarleb指出的那样,$100 0$
无效的Latex。但是,手动修改HTML以使其包含内容\(100 0\)
就可以了,因为MathJax将不间断的空格视为space。由于我不关心通过LaTex输出PDF,这意味着将Markdown转换为HTML时,只需不转换$100 0$
为\(100&nbsp;0\)
而是转换为\(100 0\)
(就像100 0
没有转换为那样100&nbsp;0
)。
Pandoc希望数学环境包含LaTeX数学标记,而不是HTML。当pandoc尝试输出$100 000$
为LaTeX时,转换失败,但这提供的\(100&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] 删除。
我来说两句