Using the booktabs package with Sweave and xtable

The xtable package in R can output R data as latex tables. Used in conjunction with Sweave it is possible to automatically generate tables in a report. Needless to say this provides a really appealing possibility of never typing data into a table again. The only problem is that I think the default latex tables don’t look that great. Enter booktabs…

The booktabs package in latex makes really nice tables, especially if there is math in your table that might run up against the regular \hline in the tabular environment.

Buried away in the xtable documentation are some options that make this possible. See the example below:



        \caption{Check out my table.}
            mat <-,nrow=5))
            colnames(mat) <- c("$\\alpha$","$\\beta$",
            rownames(mat) <- c(‘A’,’B’,’C’,’D’,’E’)
            mat <- xtable(mat,digits=rep(5,ncol(mat)+1))
                  sanitize.text.function = function(x){x},
        ,0, nrow(mat)),
                  command=c(‘\\toprule ‘,
                            ‘\\midrule ‘,
                            ‘\\bottomrule ‘)))


To compile, Sweave needs to be run on it. Running Sweave is an pre-processing step in the latex compilation process

$ R CMD Sweave table.Rnw
$ pdflatex table.tex

and then check out your awesome new table that you didn’t type!


11 thoughts on “Using the booktabs package with Sweave and xtable

    1. That would be nice indeed. I may submit a patch for xtable to do this.

      You can use a function like this:

      print.xtable.booktabs <- function(x){

    ,0, nrow(x)),
                   ‘\\toprule ‘,
                  ‘\\midrule ‘,
                   ‘\\bottomrule ‘)))


  1. Cameron

    This is a nice example. But why doesn’t the caption argument for xtable work? I though the xtable function allowed captions and labels to be passed as arguments whiche were then used in building the table envirnment.

    Cheers Ross

  2. Rotated column names are possible as well. Something like

    colnames(mat) <- paste("\\begin{sideways}",

    and include the argument

    sanitize.rownames.function = function(x){x}

    Don’t forget to include \usepackage{rotating} in the premable.

  3. Thanks for the tip!

    The caption argument probably doesn’t work because of the floating=FALSE argument. I like it this way because it gives me more control, but to works just fine to take out the table environment and the floating argument and use the caption option.

  4. The latest versions of xtable has support for booktabs built in. You can use

    print.xtable([yourtable], booktabs=TRUE)

    to produce them.

  5. This is really helpfull. Thank you very much!

    But I don’t understand why Sweave() her is used. It make all this to complex.

    Just put out the table-tex-code with print(file=”tab.tex”) and make an \input(“tab.tex”) in your global tex-file.

    Do you see any reason against it?

  6. The column names of my xtable are too long due to which the table doesn’t fit into the pdf(using Sweave). How can I format the names such that the names break automatically fitting within the specified column width( the width is same for each column) without using LaTex? Much thanks

  7. I would like to get the number in format 3.1415ยท10^4 (with a 10 instead of e or E), and align all the numbers in the column by its decimal point. How can I do it with xtable? Maybe with siunitx but I don’t know how.


Leave a Reply

Your email address will not be published. Required fields are marked *