dar*_*iaa 2 latex r knitr tables-package
我在使用表包时很挣扎,可打包文档中的所有示例都非常复杂,没有一个适合我knitr和Latex。有人可以帮忙在表头显示带有一些公式和多行标签的简单表格吗?
像这样:
df <- data.frame(matrix(1:9, nrow = 3))
colnames(df) <- c("first column", "second \\frac{1}{2}", "third column first line \\ second line")
Run Code Online (Sandbox Code Playgroud)
先感谢您
使用该xtable包可以为LaTeX中的表创建多行标题。可以从.Rmd或.Rnw文件中编译这些文件。通过mattw建立示例,并add.to.row在print方法中使用xtable:
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 \\\\ "
)
)
)
Run Code Online (Sandbox Code Playgroud)
请注意,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.} \\\\ "
)
)
)
Run Code Online (Sandbox Code Playgroud)
在此示例中,我更改了xtableputs 的默认设置\hline,使我可以将最后一个添加\hline到音符上方,这对于解释表中的上标很有用。
还要注意在第\cline{2-5}2-5列上给我一行的用法。
有关完整的示例,请参见要点。