[R] Sweave, xtable plus/minus sign

Marc Schwartz marc_schwartz at me.com
Tue Jun 29 17:36:08 CEST 2010


On Jun 29, 2010, at 10:08 AM, Ottorino-Luca Pantani wrote:

> Dear R-users,
> please consider the following minimal example:
> 
> \documentclass[a4paper,titlepage,onecolumn,12pt]{article}
> \usepackage[italian]{babel}
> \usepackage{amssymb}
> \usepackage[utf8x]{inputenc}
> \usepackage[pdftex]{graphicx}
> \begin{document}
> 
> <<label=test, echo=FALSE, results=tex>>=
> df.data1 <-
> cbind.data.frame(A = rnorm(18),
>                  B =factor(rep(LETTERS[1:6], each=3)))
> myMean <- tapply(df.data1$A, df.data1$B, FUN = mean)
> mySD <- tapply(df.data1$A, df.data1$B, FUN = sd)
> foo <- matrix(c(myMean, mySD), ncol=2, nrow=6)
> colnames(foo) <- c("Mean", "Std.Dev")
> tmpTable <- xtable(foo, caption ="Simulated data",
>    label="tab:four", digits=2)
> print(tmpTable, caption.placement="top")
> @
> 
> \end{document}
> 
> Is it possible to insert the plus/minus sign (±)  between the two columns ?
> I mean within R/Sweave and not in the resulting .tex file ?
> 
> A possible workaround could be :
> .......
> foo.df <- as.data.frame(foo)
> foo.df$Std.Dev <- paste("±", round(mySD,2), sep="")
> tmpTable <- xtable(foo.df, caption ="Simulated data",
>    label="tab:five", digits=2)
> print(tmpTable, caption.placement="top")
> @
> 
> Any other solution?


Don't use "±" as the character, as that will be impacted upon by various issues, such as locale and fonts.

Use the available LaTeX symbols, which in this case is \pm. See:

  http://www.ctan.org/tex-archive/info/symbols/comprehensive/symbols-letter.pdf

In the case of this symbol, you need to put LaTeX into math mode by using '$' to surround the symbol:

  $\pm$

However, with R, you need to double the backslashes, otherwise the backslash will be interpreted as an escape sequence. Thus, you need:

 $\\pm$


So, for example:

> paste("$\\pm$", 1.34, sep="")
[1] "$\\pm$1.34"


I believe you then need to tweak the sanitize.text.function argument in print.xtable() to properly handle the backslashes.

HTH,

Marc Schwartz



More information about the R-help mailing list