Skip to content

Problem with formatted xtable in R 2.5.0

3 messages · Pfaff, Bernhard Dr., Brian Ripley

#
Dear R-Devel subscriber,

I encountered the following problem for tex-formatted table with xtable(). Suppose I do want the following matrix as a table in LaTeX:

library(xtable)
a11 <- "\\color{green}\\textbf{big green}"
a21 <- "\\color{red}\\textbf{big red}"
a12 <- "\\color{green}green"
a22 <- "\\color{red}red"
A <- matrix(c(a11, a21, a12, a22), nrow = 2, ncol = 2)
colnames(A) <- c("big", "normal")
rownames(A) <- c("green", "red")
A
xtable(A, vsep=c('@{\\hspace{0.1cm}}', rep('@{\\hspace{0.4cm}}', 6), '@{\\hspace{0.1cm}}'))

I do get the following output:

% latex table generated in R 2.5.0 by xtable 1.4-3 package
% Fri Apr 27 13:19:43 2007
\begin{table}[ht]
\begin{center}
\begin{tabular}{rll}
  \hline
 & big & normal \\
  \hline
green & $\backslash$color\{green\}$\backslash$textbf\{big green\} & $\backslash$color\{green\}green \\
  red & $\backslash$color\{red\}$\backslash$textbf\{big red\} & $\backslash$color\{red\}red \\
   \hline
\end{tabular}
\end{center}
\end{table}


Please, note that the curly braces are prefixed with a backslash and the double backslashes are interpreted as math backslashes. The above code snippet worked fine in R 2.4.1 and I reckon that this behaviour might be due to:

 o	There is a warning if \ is used unnecessarily in a string when
	being parsed, e.g. "\." where probably "\\." was intended.
	("\." is valid, but the same as ".".)  Thanks to Bill Dunlap
	for the suggestion.

Anyway, has somebody else encountered this problem and/or is so kind and give me some hints/pointers to circumvent this problem?

Many thanks in advance,
Bernhard
_                           
platform       i386-pc-mingw32             
arch           i386                        
os             mingw32                     
system         i386, mingw32               
status                                     
major          2                           
minor          5.0                         
year           2007                        
month          04                          
day            23                          
svn rev        41293                       
language       R                           
version.string R version 2.5.0 (2007-04-23)

Dr. Bernhard Pfaff
Global Structured Products Group
(Europe)
Senior Analyst

Invesco Asset Management Deutschland GmbH
Bleichstrasse 60-62
D-60313 Frankfurt am Main

Tel: +49(0)69 29807 230
Fax: +49(0)69 29807 178
Email: bernhard_pfaff at fra.invesco.com

Gesch?ftsf?hrer: Karl Georg Bayer, Bernhard Langer, Alexander Lehmann, Christian Puschmann
Handelsregister: Frankfurt am Main, HRB 28469
Sitz der Gesellschaft: Frankfurt am Main
 
*****************************************************************
Confidentiality Note: The information contained in this mess...{{dropped}}
#
On Fri, 27 Apr 2007, Pfaff, Bernhard Dr. wrote:

            
How can a warning that you are not showing (and I don't see when running 
your code) have anything to do with this?  That change is just to add a 
warning: there is no change in the parsed code.

I get the same behaviour in 2.4.1 with xtable 1.4-3.  Have you 
perhaps updated 'xtable' as well as R and are blaming R for an 'xtable' 
change?  As ?print.xtable says

      From version 1.4-3, all non-numeric columns are sanitized, and all
      LaTeX special characters are sanitised for LaTeX output. See the
      vignette for an example of customising the sanitization.

[...]
#
Dear Prof. Ripley,

many thanks for your swift reply and - as usual - you are dead right
about your conclusion: 
I was not aware of sanitization introduced in 1.4-3 and prior to 2.5.0 I
used an older version of xtable with 2.4.1, although it was not my
intention to blame anybody, except myself now for not having done my
homework properly - flush.

Best,
Bernhard
*****************************************************************
Confidentiality Note: The information contained in this mess...{{dropped}}