3

I have a document in .Rnw which I use to produce a .texdocument with kntir. I have a problem with the formula \num{\Sexpr{max(degree)}} which in R console produce 12598 but after the running knitr in the .tex document becomes \num{\ensuremath{1.2598\times 10^{4}}}, which create the following error in LaTex:

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! siunitx error: "invalid-number"
! 
! Invalid numerical input '\protect $\relax 1.2598\times 10^{4}$'.
! 
! See the siunitx documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

l.466 ... & \num{\ensuremath{1.2598\times 10^{4}}}

I am bit puzzled since in the document I have bigger numbers which do not create any problem when compiling.

These are my /siunitx options

%Separate digits with comma (e.g. 1,000,000)
\usepackage[group-separator={,}]{siunitx}
\sisetup{
  detect-all,
  detect-inline-family=math,
  detect-inline-weight=math,
  detect-display-math=true}
Francesco
  • 4,624
  • 4
  • 35
  • 66

1 Answers1

2

It seems you want to format the numbers using siunitx, while knitr also tries to do that automatically. Only one of them should be used, so either you do not use \num{}, or tell knitr to output the numbers without scientific notations (this is perhaps what you prefer), e.g. \Sexpr{as.character(max(degree))}, or use other functions like format(), sprintf(), and so on, to turn the numbers to character strings, so that knitr will no longer do scientific notations.

Yihui Xie
  • 2,805
  • I don't mind let knitr manage the formatting of numbers. I just don't know how to set it to give me the comma (e.g. 10,000 instead of 10000) – Francesco Sep 30 '13 at 22:30
  • 1
    I'd recommend you to use siunitx instead; it seems to be more powerful. – Yihui Xie Oct 01 '13 at 00:25
  • 1
    @Francesco I strongly recommend you let siunitx do all the formatting and try to get the most raw output from knitr you can get. siunitx has excellent capabilities in typesetting numbers and units and is very easy to configure (e.g. to get 10,000 instead of 10 000, just ask for group-separator={,}). – moewe Oct 01 '13 at 19:49
  • 1
    I think the \num{\Sexpr{as.character(...)}} approach is the most practical one. That is, to keep using siunitx while getting raw output from knitr. – Francesco Oct 02 '13 at 01:17