我正在书记中撰写科学报告,我想使用non-breaking space作为SI/ISO 31-0 standard之后的千位分隔符。
实际上,我希望不会破坏thin space(U+202F
/  
)但是为了简单起见,我们在这里考虑U+00A0
/
。< / p>
我设置了一个knitr
挂钩来动态执行此操作:
knitr::knit_hooks$set(inline=function(output)
ifelse(is.numeric(output),
prettyNum(round(output, 1),
big.mark=' '),
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=' '),
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);没有任何空格,带有逗号,具有简单的空间,并且具有不间断的空间作为千位分隔符。
后者无法在数学上下文中呈现。
为了追踪问题,我检查了生成的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 0`| 100 0 |`100-0` | `100 0` |
|`$100 0$`|$100 0$|`<span_class="math_inline">\(100&nbsp;0\)</span>`|`$100 0$`|
所以我可以告诉你
&
替换为&
,我不希望这样做正确呈现。
&
替换&
但在文本上下文中没有。
关于如何在数学上下文中将
从Markdown转换为HTML的任何想法可能有助于我弄清楚其余部分。
附录:
pointed out @tarleb,$100 0$
无效Latex。
但是,手动修改HTML以包含\(100 0\)
可以正常工作MathJax treats non-breaking spaces as spaces。
由于我不关心通过LaTex输出PDF,这意味着根本不会将$100 0$
转换为\(100&nbsp;0\)
而是转换为\(100 0\)
(就像100 0
未转换为100&nbsp;0
一样当将Markdown转换为HTML时,我将需要它。
答案 0 :(得分:2)
Pandoc希望数学环境包含LaTeX数学标记,而不是HTML。当pandoc尝试输出$100 000$
作为LaTeX时,转换失败,但这会导致\(100&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
到你的文件。