0

I am trying to generate a tex table using xtable. In R, the table contains:

>tvPre
        p\\_1              p\\_2                p\\_3
FV     "\\textuparrow M"     ""                 "\\textuparrow R"                
a      "\\textuparrow WH"  ""                 ""                               
b      "\\textuparrow H"   ""                 "\\textuparrow (H)"              
c      "\\textuparrow (WH)"  ""                 "\\textuparrow (H)"              
Oil    "\\textuparrow W"     "\\textuparrow R"  "\\textdownarrow R"  

If I print in the console using an identity function for sanitizing, then is fine: The instruction :

print(xtable(tvPre), rotate.colnames=T,  
      sanitize.text.function = getOption("xtable.sanitize.text.function", function(x)x))

Then, I obtain:

 & \begin{sideways} p\_1 \end{sideways} & \begin{sideways} p\_2\end{sideways} & \begin{sideways} p\_3 \end{sideways} \\ 
  \hline
FV & \textuparrow M   &                & \textuparrow R \\ 
a  & \textuparrow WH  &                &   \\ 
b  & \textuparrow H   &                & \textuparrow (H)   \\ 
c  & \textuparrow (WH)  &                & \textuparrow (H) \\ 
O  & \textuparrow W   & \textuparrow R & "\\textdownarrow R" \\

Nevertheless, when I put the code in a Sweave file (.Rnw), then in the .tex I obtain:

& \begin{sideways} p\_1 \end{sideways} & \begin{sideways} p\_2\end{sideways} & \begin{sideways} p\_3 \end{sideways} \\ 
  \hline
FV & \textuparrow M &  & \textuparrow R \\
a  & \textuparrow W &  &  \\
b  & H              &  & H \\
c  & \textuparrow W &  & H \\
O  & \textuparrow W & \textuparrow R & \textdownarrow R \\

Then, all parenthesis disappear and the arrow as well. I tried using $\\uparrow$ as well, but still not working. The third line, second column ("\\textuparrow WH") is printed without the "H", but in the next line " \\textuparrow H" is printed only the "H". The rest of cells with parenthesis are printed without arrows nor parenthesis in the .tex file.

I need to print in the .tex as is printed in the console, some clue??

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 1
    Could you add a reproducible example for your table (http://stackoverflow.com/a/5963610/2633645). It may be that you need to double back slash your special characters to escape them. – r.bot Feb 09 '15 at 13:46

1 Answers1

1

Ok, the following works for me. Your question didn't state whether you had the textcomp package loaded - you need that. Ditto the sideways package. I've also changed the sanitize.text.function to sanitize.text.function = function(x){x}.

The script below is saved as an .Rnw file.

\documentclass{article}
\usepackage{textcomp}
\usepackage{rotating}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<table, echo=FALSE, results=tex >>= 
require(xtable)

tvPre <- as.data.frame(structure(list(`p\\\\_1` = c("\\textuparrow M", "\\textuparrow WH", 
"\\textuparrow H", "\\textuparrow (WH)", "\\textuparrow W"), 
    `p\\\\_2` = c("", "", "", "", "\\textuparrow R"), `p\\\\_3` = c("\\textuparrow R", 
    "", "\\textuparrow (H)", "\\textuparrow (H)", "\\textdownarrow R"
    )), .Names = c("p\\_1", "p\\_2", "p\\_3"), row.names = c("FV", 
"a", "b", "c", "Oil"), class = "data.frame"))

print(xtable(tvPre), rotate.colnames = TRUE, sanitize.text.function = function(x){x})
@


\end{document}
r.bot
  • 5,309
  • 1
  • 34
  • 45
  • That works! Finally, I could not find the problem, but I pretty sure that it was a syncronization problem among the workspaces, that one mode was loading a old code..I couldn't find why but I reinstall all and works. – user1686315 Feb 16 '15 at 12:03
  • How do you restrict the sanitization to just a few columns? – skan Apr 30 '18 at 12:05