2

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

dariaa
  • 6,285
  • 4
  • 42
  • 58
  • 1
    Are you dead set on using “tables”? I recommend [pander](http://rapporter.github.io/pander/) in combination with pandoc. It offers fewer options for customisation but it mostly does “the correct thing”. – Konrad Rudolph Aug 16 '15 at 17:42

2 Answers2

5

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.

Matt Upson
  • 601
  • 5
  • 9
  • You wrote "This is achievable in RMarkdown", but then present a LaTeX solution? I'm confused … – CL. Aug 16 '15 at 16:57
  • Yes I take your point. I meant it is achievable from a .Rmd, although the underlying basis would be using LaTeX via xtable. I'll adjust my answer to reflect this. – Matt Upson Aug 16 '15 at 17:04
1

I don't think that this is possible with RMarkdown if you want the table to be in LaTeX style. However, you can easily do this with the xtable package when you write your code in an .Rnw file:

\documentclass{article}
\begin{document}
<<>>=
library("xtable")
df <- data.frame(matrix(1:9, nrow = 3))
colnames(df) <- c("first column", "second $\\frac{1}{2}$",
                  "third column")
@
<<xtable, results = "asis">>=
print(xtable(df), floating = TRUE,
      sanitize.colnames.function = identity, type = "latex")
@
\end{document}
dariaa
  • 6,285
  • 4
  • 42
  • 58
matthias
  • 164
  • 1
  • 10
  • Thank you for your answer, one more question, is it possible to have a multiline header? I updated the question? – dariaa Aug 14 '15 at 09:02
  • I haven't done that, yet. I hope that this link can help you a bit: [How to put a newline into a column header in an xtable in R](http://stackoverflow.com/a/2687142/2323832) – matthias Aug 14 '15 at 09:13
  • Yeah, thanks, I've been here. It does not compile for me with knitr for some reason – dariaa Aug 14 '15 at 09:17
  • That post is very helpful but as they noted, the alignment techniques used will not work on the last column of a table, making it tough to get all of the columns multi line while having alignment within each. – Trevor Nederlof Aug 14 '15 at 11:14