R sub() backslash gotcha

One of the things I do to my Sweave output is to make scientific notation more transparent.  I have a special R function for this which I use in my Sweave documents:

myFormat <- function(...) {
tmp <- format(...)
return(sub("e(.*)","\\\\\\\\times 10^{\\1}",tmp))
}

Note in particular the ridiculous number of backslashes required in the call to sub() to obtain a single backslash in the string output.  I have no idea why so many backslashes are needed—but apparently they are.  Hopefully this will save someone else the headache of figuring out the magic number by trial and error!

Post a Comment

You must be logged in to post a comment.