[R] paste in xtable

Charilaos Skiadas cskiadas at gmail.com
Wed Sep 24 02:33:19 CEST 2008


Actually the '\\textbf' specification would work just fine. If you  
examine test[,1] you'll see it contains the correct thing. The  
problem is when print.xtable is called. This is because it  
automatically contains a function that "sanitizes" the character  
entries to "fix" characters that have a special meaning in latex.  
Hence \\textbf becomes $\backslash$textbf. The following should work:

test= matrix(1:4, nrow=2,ncol=2)
test = xtable(test)
test[,1] = ifelse(test[,1]>1,paste('\\textbf{',test[,1],'}'),test[,1])
print(test, sanitize.text.function=I)

The last command sets the sanitize function to do effectively do  
nothing and simply return the string. Hope this helps.

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College

On Sep 23, 2008, at 8:13 PM, Ryan D. Enos wrote:

> Dear R help:
> I am trying to use paste(), within an ifelse() statement to insert  
> latex commands into an object that has been created using xtable 
> ().  I cannot make the strings behave as I would like, the '\t' is  
> creating a tab, the usual method of '\\t' is not working either -  
> nor is any series of backslashes.  The xtable object, I think,  
> automatically alters the strings.  How can I insert the literal  
> '\textbf' into my xtable object?  Any help would be appreciated.   
> Code examples below.
> Ryan
>
> example 1:
>
> > test= matrix(1:4, nrow=2,ncol=2)
> > test = xtable(test)
> > test[,1] = ifelse(test[,1]>1,paste('\textbf{',test[,1],'}'),test[, 
> 1])
> > test
> % latex table generated in R 2.5.1 by xtable 1.5-1 package
> % Tue Sep 23 17:08:18 2008
> \begin{table}[ht]
> \begin{center}
> \begin{tabular}{rrr}
>  \hline
> & 1 & 2 \\
>  \hline
> 1 & 1 &   3 \\
>  2 &   extbf\{ 2 \} &   4 \\
>   \hline
> \end{tabular}
> \end{center}
> \end{table}
>
> example 2:
>
> > test= matrix(1:4, nrow=2,ncol=2)
> > test = xtable(test)
> > test[,1] = ifelse(test[,1]>1,paste('\\textbf{',test[,1],'}'),test 
> [,1])
> > test
> % latex table generated in R 2.5.1 by xtable 1.5-1 package
> % Tue Sep 23 17:11:56 2008
> \begin{table}[ht]
> \begin{center}
> \begin{tabular}{rrr}
>  \hline
> & 1 & 2 \\
>  \hline
> 1 & 1 &   3 \\
>  2 & $\backslash$textbf\{ 2 \} &   4 \\
>   \hline
> \end{tabular}
> \end{center}
> \end{table}
>
>
> -- 
> Ryan D. Enos
>
> Department of Political Science
> UCLA
>
> http://renos.bol.ucla.edu/
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list