I use xtable to manage my tables in Sweave like this

<<label=tab1, echo = FALSE, results = tex>>=

<<label=tab2, echo = FALSE, results = tex>>=

I wonder how to get these tables side by side. Any help will be highly appreciated. Thanks


The basic idea of this is that you need to make sure that the tables generated by xtable don't float; instead you should put them in your own floating environment. In the example below, I've generated two side-by-side tables, each within a {minipage} environment and each with their own caption using the \captionof command of the caption package.

I've formatted the tables using booktabs. See How can I use a table generated by R in LaTeX? for more details on this.

\title{Side-by-side xtables}
First some R code to create some data.
myData <- matrix(c(19,89,23,23,74,44,16,39,67),ncol=3,byrow=TRUE)
colnames(myData) <- c("A","B","C")
rownames(myData) <- c("1","2","3")
myData2 <- myData * 2

Now we place the data in two side-by-side tables:

  add.to.row=list(pos=list(-1,0, nrow(myData)),
\captionof{table}{The first table}
  add.to.row=list(pos=list(-1,0, nrow(myData2)),
\captionof{table}{The second table}

output of code

| improve this answer | |
  • Thanks for nice answer. This works fine for small tables. I wonder how to use tabular.environment='longtable' for large tables. Thanks again for your time and effort. – MYaseen208 Nov 28 '11 at 3:10
  • I don't think it's possible to put two longtables side by side independently of Sweave. Because a longtable can break pages, you can put them beside each other. – Alan Munn Nov 28 '11 at 3:22
  • Thanks for your feedback. Would you mind to tell me how to put Table captions above the table. Thanks – MYaseen208 Nov 28 '11 at 3:28
  • 1
    @MYaseen208 Just move the \captionof commands to right after the \centering command (before the code that generates the table). – Alan Munn Nov 28 '11 at 3:39
  • BTW, in the comment about longtables I meant to say because they can break pages you can't put them beside each other. (It was probably clear from the context anyway.) – Alan Munn Nov 28 '11 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.