显示科学计数法 [papaja]

发布于 2025-01-12 19:59:10 字数 425 浏览 3 评论 0原文

我的代码块中有很多数字:

a <-  1.234 * 10^36

然后我在文档中内联打印:

What does this look like when knitted: `r a`

在标准 .Rmd 中编织它会产生预期的 1.234 x 10^36

但在 papaja 模板中编织会产生“计算格式”与 1.234e+36

无论如何,是否可以自动进行格式化,而不必求助于像 这里给出的解决方案

I have a large number in my code chunk:

a <-  1.234 * 10^36

Which I then print inline in my document:

What does this look like when knitted: `r a`

Knitting this in a standard .Rmd produces the expected 1.234 x 10^36

But knitting in the papaja template produces the "computational format" with 1.234e+36

Is there anyway to automate the formatting, without having to resort to a custom function like the solution given here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2025-01-19 19:59:10

您可以使用函数papaja::apa_num()

> papaja::apa_num(1.234 * 10^36, format = "e")
[1] "$1.23 \\times 10^{36}$"

You could use the function papaja::apa_num():

> papaja::apa_num(1.234 * 10^36, format = "e")
[1] "$1.23 \\times 10^{36}
quot;
凡尘雨 2025-01-19 19:59:10

您可以使用 knitr 强制格式化。

knitr::knit_hooks$set(inline = function(x) {
  knitr:::format_sci(x, 'md')
})

这会将外观格式化为如下所示(已使用如上所述的代码)。

输入图片这里的描述

我不喜欢关于x的空间分布不均匀,所以我发现捕获了函数(和支持函数)并且能够得到:

在此处输入图像描述

对于前者,我按照您的指示使用。不过,这不是您将用于第二个选项的内容。

`r a`

您不需要调用 hooks$set,但需要调用修改后的函数并使用 $ 封装该调用。

您可以使用 knitr 强制格式化。

knitr::knit_hooks$set(inline = function(x) {
  knitr:::format_sci(x, 'md')
})

这会将外观格式化为如下所示(已使用如上所述的代码)。

输入图片这里的描述

我不喜欢关于x的空间分布不均匀,所以我发现捕获了函数(和支持函数)并且能够得到:

在此处输入图像描述

对于前者,我按照您的指示使用。不过,这不是您将用于第二个选项的内容。

`r a`

您不需要调用 hooks$set,但需要调用修改后的函数并使用 $ 封装该调用。

r format_sci(a)`$

以下是产生第二个选项的稍加修改的 knitr 函数:

```{r doAsISay}

# scientific notation in TeX, HTML and reST
format_sci_one = function(
  x, format = 'latex', times = getOption('knitr.inline.times', '\\times ')
) {

  if (!(class(x)[1] == 'numeric') || is.na(x) || x == 0) return(as.character(x))

  if (is.infinite(x)) {
    return(
      switch(format, latex = {
        sprintf("%s\\infty{}", ifelse(x < 0, "-", ""))
      }, html = {
        sprintf("%s∞", ifelse(x < 0, "-", ""))
      }, as.character(x)))
  }

  if (abs(lx <- floor(log10(abs(x)))) < getOption('scipen') + 4L)
    return(round_digits(x)) # no need sci notation

  b = round_digits(x / 10^lx)
  b[b %in% c(1, -1)] = ''

  switch(format, latex = {
    sci_notation('%s%s10^{%s}', b, times, lx)
  },
  html = sci_notation('%s%s10<sup>%s</sup>', b, ' × ', lx),
  md   = sci_notation('%s%s10^%s^', b, '× ', lx),
  rst  = {
    # if AsIs, use the :math: directive
    if (inherits(x, 'AsIs')) {
      s = sci_notation('%s%s10^{%s}', b, times, lx)
      sprintf(':math:`%s`', s)
    } else {
      # This needs the following line at the top of the file to define |times|
      # .. include <isonum.txt>
      sci_notation('%s%s10 :sup:`%s`', b, ' |times| ', lx)
    }
  }, as.character(x))
}

# vectorized version of format_sci_one()
format_sci = function(x, ...) {
  if (inherits(x, 'roman')) return(as.character(x))
  vapply(x, format_sci_one, character(1L), ..., USE.NAMES = FALSE)
}

sci_notation = function(format, base, times, power) {
  sprintf(format, base, ifelse(base == '', '', times), power)
}

round_digits = function(x) {
  if (getOption('knitr.digits.signif', FALSE)) format(x) else {
    as.character(round(x, getOption('digits')))
  }
}
```

或者,您可以在下载的 knitr 包中更改此设置。 (不过,我之前对下载的软件包进行了修改,而不是 knitr。)

仅供参考,这是经过测试的,图像来自 knitted RMD,使用输出设置为 output: papaja ::apa6_pdf

You can use knitr to force formatting.

knitr::knit_hooks$set(inline = function(x) {
  knitr:::format_sci(x, 'md')
})

This will format the appearance to look like this (having used your code as described above).

enter image description here

I wasn't fond of the uneven distribution of space about x, so I found captured the functions (and supporting functions) and was able to get:

enter image description here

For the former, I used as you indicated. This isn't what you would use for the second option, though.

`r a`

You wouldn't need to call the hooks$set, but you would need to call the modified function and encapsulate the call with $.

You can use knitr to force formatting.

knitr::knit_hooks$set(inline = function(x) {
  knitr:::format_sci(x, 'md')
})

This will format the appearance to look like this (having used your code as described above).

enter image description here

I wasn't fond of the uneven distribution of space about x, so I found captured the functions (and supporting functions) and was able to get:

enter image description here

For the former, I used as you indicated. This isn't what you would use for the second option, though.

`r a`

You wouldn't need to call the hooks$set, but you would need to call the modified function and encapsulate the call with $.

r format_sci(a)`$

Here are the slightly modified knitr functions that produced the second option:

```{r doAsISay}

# scientific notation in TeX, HTML and reST
format_sci_one = function(
  x, format = 'latex', times = getOption('knitr.inline.times', '\\times ')
) {

  if (!(class(x)[1] == 'numeric') || is.na(x) || x == 0) return(as.character(x))

  if (is.infinite(x)) {
    return(
      switch(format, latex = {
        sprintf("%s\\infty{}", ifelse(x < 0, "-", ""))
      }, html = {
        sprintf("%s∞", ifelse(x < 0, "-", ""))
      }, as.character(x)))
  }

  if (abs(lx <- floor(log10(abs(x)))) < getOption('scipen') + 4L)
    return(round_digits(x)) # no need sci notation

  b = round_digits(x / 10^lx)
  b[b %in% c(1, -1)] = ''

  switch(format, latex = {
    sci_notation('%s%s10^{%s}', b, times, lx)
  },
  html = sci_notation('%s%s10<sup>%s</sup>', b, ' × ', lx),
  md   = sci_notation('%s%s10^%s^', b, '× ', lx),
  rst  = {
    # if AsIs, use the :math: directive
    if (inherits(x, 'AsIs')) {
      s = sci_notation('%s%s10^{%s}', b, times, lx)
      sprintf(':math:`%s`', s)
    } else {
      # This needs the following line at the top of the file to define |times|
      # .. include <isonum.txt>
      sci_notation('%s%s10 :sup:`%s`', b, ' |times| ', lx)
    }
  }, as.character(x))
}

# vectorized version of format_sci_one()
format_sci = function(x, ...) {
  if (inherits(x, 'roman')) return(as.character(x))
  vapply(x, format_sci_one, character(1L), ..., USE.NAMES = FALSE)
}

sci_notation = function(format, base, times, power) {
  sprintf(format, base, ifelse(base == '', '', times), power)
}

round_digits = function(x) {
  if (getOption('knitr.digits.signif', FALSE)) format(x) else {
    as.character(round(x, getOption('digits')))
  }
}
```

Alternatively, you can change this in your download of the knitr package. (I've made modifications to my downloaded packages before, not knitr, though.)

FYI, this was tested with and the images were from the knitted RMD using the output set to output: papaja::apa6_pdf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文