2

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?

  • 1
    Does this answer your question? [How to disable scientific notation?](https://stackoverflow.com/questions/5352099/how-to-disable-scientific-notation) – Moritz Schwarz Mar 09 '22 at 10:26
  • No, that simply displays it without scientific notation. I'm looking to format the notation a particular way. It seems the default behaviour of papaja is different to the standard .Rmd template. – Tom Beesley Mar 09 '22 at 11:51

2 Answers2

1

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

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

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&infin;", 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, ' &times; ', lx),
  md   = sci_notation('%s%s10^%s^', b, '&times; ', 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.

Kat
  • 15,669
  • 3
  • 18
  • 51