Ответ 1
Вам нужна предварительная обработка, дополнительный аргумент, переданный в 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)
Результат: