如何使用xtable和Sweave在latex中打印带括号的符号

时间:2015-02-09 13:09:49

标签: r sweave sanitize xtable

我正在尝试使用xtable生成tex表。 在R中,该表包含:

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

如果我使用身份功能在控制台中打印以进行消毒,那么很好: 说明:

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

然后,我获得:

 & \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" \\

然而,当我将代码放入Sweave文件(.Rnw)时,然后在.tex中获取:

& \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 \\

然后,所有括号都会消失,箭头也会消失。我也尝试使用$\\uparrow$,但仍然无法正常工作。 第三行第二列("\\textuparrow WH")打印时没有"H",但在下一行" \\textuparrow H"中仅打印"H"。 带括号的其余单元格在.tex文件中打印时没有箭头或括号。

我需要在控制台中打印的.tex中打印一些线索吗?

1 个答案:

答案 0 :(得分:1)

好的,以下适用于我。你的问题并没有说明你是否加载了textcomp包 - 你需要它。侧身包装也是如此。我还将sanitize.text.function更改为sanitize.text.function = function(x){x}

以下脚本保存为.Rnw文件。

\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}