13

我想自定义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

实际上,该表是从 .csv 文件中导入data.framemy.table <- read.delim("filename.csv", sep=",", header=TRUE)

现在我创建一个 LaTeX 表xtable

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在第二行而不是第一行之后插入。\rowcolors{2}{gray!25}{white}2)通过在sidewaystable(或正常table)环境中插入来改变该表行的颜色。3) 将列名旋转 45° 4) 在我想使表格居中的情况下插入\centering而不是center-environment。

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

4

1 回答 1

14

您需要一些预处理,传递给的额外参数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")

如果您需要其他选项卡环境tabular1) 添加新变量:

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)

结果:

在此处输入图像描述

于 2012-11-10T23:40:13.923 回答