Skip to content
Prev 316306 / 398502 Next

Problem with large/small numbers in knitr

On 26.12.2012 23:28, xiaodao wrote:
I copied your file to "abc.Pnw", then opened an R session and run
setwd("<Path_to_file>")
library(knitr)
knit("abc.Rnw")

I got the following text in the resulting abc.tex file:


$
c=$10^{-13}$/$2.5\times 10^{-10}$ % either this formula or ...
f=56.12/$1.23\times 10^{5}$
$

knitr converts large numbers in Sexpr to the exponential notation, so
you've got a nested math environment here.

You can convert your numbers to strings with as.character()

Alternative:
if you run knit_hooks$get("inline") you get the function used for inline
output procession


https://github.com/yihui/knitr/issues/33


-> if you write
knit_hooks$set()$inline
function (x)
{
    if (is.numeric(x))
        x = round(x, getOption("digits"))
    paste(as.character(x), collapse = ", ")
}

in your setup chunk (did you forget the closing @ in your setup chunk?),
it also works. (which is strange, because this function is exactly what
is predefinded by knitr but that's another question

Regards, Moritz