On 26.12.2012 23:28, xiaodao wrote:
I have problems with very large numbers using knitr. In the following, my a and b are extremely small and ssrr and ssru are extremely large.
\documentclass{article}
\begin{document}
<<setup, echo=FALSE, cache=FALSE>>=
## numbers >= 10^5 will be denoted in scientific notation,
## and rounded to 2 digits
options(scipen = 1, digits = 2)
<<>>=
a<-1e-13
b<-2.5e-10
ssrr<-123456.12
ssru<-123400.00
@
$
c=\Sexpr{a}/\Sexpr{b} % either this formula or the following formula will
has error message "missing $" after click "complie" in Rstudio.
f=\Sexpr{ssrr-ssru}/\Sexpr{ssru}
$
\end{document}
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
GnuPG Key: 0x7340821E