且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用表格 + knitr for latex 的简单示例

更新时间:2022-10-24 12:18:11

It is possible to create multi-line headers for tables in LaTeX using the xtable package. These can be compiled from either .Rmd or .Rnw files. Building on the example by mattw and using add.to.row in the print method for 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 \\ "
    )
  )
)

Note that add.to.row requires a list with two elements: pos and command. The first must be a list, the second a character string or vector, see ?print.xtable. pos gives the row number for the LaTeX insertion, and command is the insertion. Be a bit careful with formatting this, as it is will run directly into the next cell of the first column if you don't put in spaces or .

There are lots of options for customisation, allowing you to create quite complex tables with a bit of tweaking.

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.} \\ "
    )
  )
)

In this example I change the default settings for where xtable puts hline, allowing me to add the last hline above the notes - useful for explaining superscripts in the table.

Note also the use of cline{2-5} giving me a line over columns 2 - 5.

See gist for fully reproducible example.