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)
  • Any chance of a MWE? – moewe Sep 30 '13 at 12:03
  • 1
    \ensuremath is problematic, \times probably too. Can't you configure R/knitr to output \num{1.2598E4} (or just 1.598E4 for a S column)? – Qrrbrbirlbel Sep 30 '13 at 12:06
  • 1
    Nope, no idea. Maybe it is easier to work on R to make sure the input is what I expect. – Francesco Sep 30 '13 at 12:11

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.

  • 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 1 '13 at 0: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 1 '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 2 '13 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.