Discussion:
[R] Sweave, xtable plus/minus sign
Ottorino-Luca Pantani
2010-06-29 15:08:19 UTC
Permalink
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?
--
Ottorino-Luca Pantani, Universit? di Firenze
Dip.to di Scienze delle Produzioni Vegetali,
del Suolo e dell'Ambiente Forestale (DiPSA)
P.zle Cascine 28 50144 Firenze Italia
Ubuntu 10.04 -- GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version
2.18.0)
ESS version 5.8 -- R 2.10.1
Marc Schwartz
2010-06-29 15:36:08 UTC
Permalink
Post by Ottorino-Luca Pantani
Dear R-users,
\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 ?
.......
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$
Post by Ottorino-Luca Pantani
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
Ottorino-Luca Pantani
2010-06-29 16:01:15 UTC
Permalink
................
Post by Ottorino-Luca Pantani
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
Thanks, Marc.
I modified the code as follows:

foo.df$Std.Dev <-
paste("$\\pm$", round(mySD,2), sep="")
tmpTable <-
xtable(foo.df, caption ="Simulated data",
label="tab:five", digits=2)
print(tmpTable, caption.placement="top",
sanitize.text.function= function(x){x})

which result in a .tex file like

......
\begin{table}[ht]
\begin{center}
\caption{Simulated data}
\label{tab:five}
\begin{tabular}{rrl}
\hline
& Mean & Std.Dev \\
\hline
1 & 0.46 & $\pm$0.42 \\
2 & 0.81 & $\pm$0.69 \\
3 & 0.17 & $\pm$0.56 \\
4 & 0.15 & $\pm$1.02 \\
5 & 0.60 & $\pm$1.37 \\
6 & 0.48 & $\pm$1.39 \\
\hline
\end{tabular}
\end{center}
\end{table}\end{document}

so it seems that there's no need to tweak the sanitize.text.function

Thanks again.
--
Ottorino
Loading...