自定义xtable

时间:2012-08-02 12:37:11

标签: r latex xtable

我想自定义xtable以导出到LaTeX。我知道这里有一些问题xtable,但我找不到我正在寻找的具体内容。

以下是我的表格的示例:

my.table <- data.frame(Specifiers=c("","Spec1", "Spec2", "Spec3"),
    Values1 = c("N=10", 1.03, 1.71, 2.25),
    Values2 = c("N=20", 1.32, 1.79, 2.43))
colnames(my.table)[1] <- ""

创造了:

         Values1 Values2
1          N=10    N=20
2 Spec1    1.03    1.32
3 Spec2    1.71    1.79
4 Spec3    2.25    2.43

实际上,此表是从。data.frame {。}}导入的.csv文件中导入的my.table <- read.delim("filename.csv", sep=",", header=TRUE)

现在我用xtable创建一个LaTeX表:

latex.tab <- xtable(my.table, caption=c("Stats"))
print(latex.tab, file="Summarystats.tex",
  floating.environment='sidewaystable',
  include.rownames=FALSE,
  booktabs=TRUE,
  latex.environment=NULL)

以下是生成的LaTeX代码:

\begin{sidewaystable}[ht]
\begin{tabular}{lllllll}
  \toprule
 & Values1 & Values2 \\ 
  \midrule
               N=10  &  N=20 \\
     Spec1  &  1.03  &  1.32 \\
     Spec2  &  1.71  &  1.79 \\
     Spec3  &  2.25  &  2.43 \\

   \bottomrule
\end{tabular}
\end{sidewaystable}

好的,现在这就是我要改变的地方:

1)在第二行之后而不是在第一行之后插入\midrule。 2)通过在\rowcolors{2}{gray!25}{white}(或普通sidewaystable)环境中插入table来替换此表行的颜色。 3)将柱名旋转45° 4)在我想要使表格居中的情况下,插入\centering而不是center - 环境。

关于如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:12)

你需要一些预处理,额外的参数传递给print.xtable和一些后期处理:

my.table <- data.frame(Specifiers=c("","Spec1", "Spec2", "Spec3"),
                       Values1 = c("N=10", 1.03, 1.71, 2.25),
                       Values2 = c("N=20", 1.32, 1.79, 2.43))
colnames(my.table)[1] <- ""

# Pre-processing: rotates column names by 45 degrees
head = apply(as.array(names(my.table)), 1, function(x) paste("\\rotatebox{45}{", x, "}"))
head = paste(head, c(rep("&", length(head)-1), "\\\\\n"), collapse="")

latex.tab <- xtable(my.table, caption=c("Stats"))
ltable = print(latex.tab, file="", # File is empty, post-processing needed
      floating.environment='sidewaystable',
      include.rownames=FALSE,
      include.colnames=FALSE, # No colnames
      booktabs=TRUE,
      latex.environment="center", # Or NULL
      # Adds some extra-text after the rows specified in pos.
      # Adds new \midrule and comments old one.
      # Adds pre-processed names of columns
      add.to.row=list(pos=as.list(c(0, 0, 1)), command=as.vector(c(head, "%", "\\midrule\n"))))

# Post-processing: replaces \begin{center} with \centering
ltable = sub("\\begin{center}\n", "\\centering\n", ltable, fixed=TRUE)
ltable = sub("\\end{center}\n", "\n", ltable, fixed=TRUE)

# Post-processing: adds alternating colours
ltable = sub("\\begin{tabular}",
             "\\rowcolors{2}{gray!25}{white}\n\\begin{tabular}",
            ltable, fixed=TRUE)

# Writes output to the file
cat(ltable, file="Summarystats.tex")

如果您需要其他标签环境而不是tabular 1)添加新变量:

TABULAR = "tabular"

2)将它的值传递给print.xtable,如下所示:

...
tabular.environment=TABULAR,
...

3)更改交替颜色的后处理:

ltable = sub(sprintf("\\begin{%s}", TABULAR),
             sprintf("\\rowcolors{2}{gray!25}{white}\n\\begin{%s}", TABULAR),
             ltable, fixed=TRUE)

结果:

enter image description here