智能舍入一直是 knitr 的一大特色,可以避免很多 sprintf/round/paste0
走弯路。
在一位客户提示我给出了错误的小数点后,我注意到忘记 $$ 对于可能以科学计数法打印的数字是非常危险的。但是同一位客户提示我使用科学计数法的格式看起来很丑,因为类似于 latex 的数学与主要字体不匹配。
在@yihui 对已关闭问题( https://github.com/yihui/knitr/issues/864 )的评论之后,$$ 是必需的。
有人对此有智能解决方案吗?目前,我回到过去使用 sprintf 格式化所有内容。
---
output:
html_document:
theme: cosmo
---
I use the cosmo theme because it shows the typographic problem more clearly.
```{r}
options(digits=2)
mean = c(2.31310934, 1.23456e-7)
std = c(0.31310934, 6.54321e-7)
```
digits is `r getOption("digits")`
The mean is `r mean[1]` with a standard deviation of `r std[1]`.
This looks good
The mean is `r mean[2]` with a standard deviation of `r std[2]`.
Note that the aboves looks like "1.23510".
The mean is $`r mean[2]`$ with a standard deviation of $`r std[2]`$.
最佳答案
您可以随时重新定义 inline
输出 Hook 以避免许多显式 sprintf/round/paste0
...
knitr::knit_hooks$set(inline = function(x) {
knitr:::format_sci(x, 'md')
})
这将使用
1.23 × 10^-7^
形式的语法。对于科学记数法,如果您的输出格式仅为 HTML,它应该可以正常工作。对于特定类型的输出格式,这是一个简单的问题,但是当您希望数字格式可以跨所有格式(包括 HTML、Word、LaTeX 等)移植时,这很难。这就是为什么
$ $
在 knitr 1.7 中成为必需的。
关于r - knitr : How to improve typography 中的科学记数法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437835/