可以使用xtable 包为 LaTeX 中的表格创建多行标题。这些可以从 .Rmd 或 .Rnw 文件编译。以 mattw 的示例为基础,并在 print 方法中为 xtable 使用 add.to.row:
df <- data.frame(matrix(1:50, nrow = 10))
print(
xtable(df),
include.rownames = FALSE,
include.colnames = FALSE,
add.to.row = list(
pos = list(0),
command = c(
"& \\multicolumn{4}{c}{4 column header} \\\\
\\cline{2-5}
col1 & col2 & col3 & col4 & col5 \\\\ "
)
)
)
请注意,add.to.row 需要一个包含两个元素的列表:pos 和 command。第一个必须是列表,第二个必须是字符串或向量,请参阅?print.xtable。 pos 给出了 LaTeX 插入的行号,command 是插入。格式化时要小心,因为如果您不输入空格或\n,它将直接运行到第一列的下一个单元格。
有很多自定义选项,允许您通过一些调整来创建相当复杂的表格。
print(
xtable(df),
include.rownames = FALSE,
include.colnames = FALSE,
hline.after = c(-1,0),
add.to.row = list(
pos = list(0,5,10),
command = c(
"& \\multicolumn{4}{c}{4 column header} \\\\
\\cline{2-5}
col1 & col2 & col3 & col4 & col5 \\\\ ",
"\\hline \\multicolumn{5}{l}{Separator in table.} \\\\ \\hline",
"\\hline \\multicolumn{5}{l}{Notes at end of table.} \\\\ "
)
)
)
在此示例中,我更改了 xtable 放置 \hline 的默认设置,允许我在注释上方添加最后一个 \hline - 有助于解释表格中的上标。
还要注意 \cline{2-5} 的使用,在第 2 - 5 列上给我一行。
请参阅gist 了解完全可重现的示例。