Simple example of using tables + knitr for latex

后端 未结 2 1986
粉色の甜心
粉色の甜心 2021-01-15 04:11

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 h

相关标签:
2条回答
  • 2021-01-15 05:01

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

    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.

    0 讨论(0)
  • 2021-01-15 05:05

    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}
    
    0 讨论(0)
提交回复
热议问题