简体   繁体   中英

How to print symbols with parenthesis in latex using xtable and Sweave

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??

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM