Search code examples
rlatexknitrtables-package

Simple example of using tables + knitr for latex


I'm struggling with a tables package, all the examples in the packable docs are so complex and none of them works for me with knitr and latex. Could somebody help be out and display a simple table with some formulas and multiline labels in the header? 

Something like this:

df <- data.frame(matrix(1:9, nrow = 3))
colnames(df) <- c("first column", "second \\frac{1}{2}", "third column first line \\ second line")

Thank you in advance


Solution

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

    enter image description here

    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 \n.

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

    enter image description here

    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.