自定义 xtable [英] Customize xtable

查看:31
本文介绍了自定义 xtable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义 xtable 以导出到 LaTeX.我知道这里有一些关于 xtable 的问题,但我找不到我要查找的具体内容.

I'd like to customize xtable for export into LaTeX. I know there are some questions abot xtable here, but I couldn't find the specific things I'm looking for.

下面是我的表格的示例:

Here is an example of how my table might look like:

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.frame 导入的,其中 my.table <- read.delim("filename.csv", sep=",", header=TRUE)

In fact, this table is imported from a .csv-file as data.frame with my.table <- read.delim("filename.csv", sep=",", header=TRUE)

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

Now I create a LaTeX table with 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 代码:

Here is the resulting LaTeX code:

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

   ottomrule
end{tabular}
end{sidewaystable}

好的,现在这就是我要更改的内容:

Ok, and now this is what I'd like to change:

1) 在第二行而不是第一行之后插入 midrule.2) 通过在 sidewaystable (或普通 table) 环境.3) 将列名旋转 45°4) 当我想将表格居中时,插入 centering 而不是 center-环境.

1) Insert midrule after the second row instead of after the first. 2) Alternating colours of the rows of this table by inserting owcolors{2}{gray!25}{white} within the sidewaystable (or normal table) environment. 3) Rotating column names by 45° 4) Insert centering instead of the center-environment in cases when I want to center the table.

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

Any ideas on how to achieve this?

推荐答案

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

You need some pre-processing, extra argument passed to print.xtable and some post-processing:

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), "\\
"), 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
"))))

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

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

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

如果您需要 tabular 以外的其他选项卡环境,您可以1) 添加新变量:

If you need other tabs environment than tabular you can 1) add new variable:

TABULAR = "tabular"

2) 像这样将它的值传递给 print.xtable:

2) Pass it's value to print.xtable like this:

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

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

3) Change your post-processing for alternating colors:

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

结果:

这篇关于自定义 xtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆