r - knitr : How to improve typography 中的科学记数法

标签 r knitr rstudio r-markdown

智能舍入一直是 knitr 的一大特色,可以避免很多 sprintf/round/paste0 走弯路。

在一位客户提示我给出了错误的小数点后,我注意到忘记 $$ 对于可能以科学计数法打印的数字是非常危险的。但是同一位客户提示我使用科学计数法的格式看起来很丑,因为类似于 latex 的数学与主要字体不匹配。

scientific notation

在@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/

相关文章:

r - 使用 marmap 包和 getNOAA.bathy 将测深线添加到 ggplot

R成对积

r - Bookdown(证明)自定义环境 : how to nest code chunks or inline R inside custom (e. g。 proof) 环境并得到正确的解析?

r - 为什么我在 Julia 中使用 dropmissing 或 skipmissing 会得到不同的结果?

r - 在 MacBook Pro 上安装 Rcmdr 包时出现 Tcl Tk 库问题

从内联 Rhtml knitr 代码中删除 <code> 标签

R 从批处理文件(windows)运行 knitr

读取 UTF-8 文本文件(希伯来语)在 RStudio 控制台中显示乱码,在 RGUI 中显示正常

r - fig.width 和 fig.height 不适用于 R Markdown 的 knitr Word 输出

RStudio 服务器 - 切换项目时挂起