Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align multiple tables side by side

The following code produces 2 tables on top of each other. How would I set it to have them aligned side by side, e.g. 3 to a row?

--- title: "sample" output: pdf_document ---  ```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)} ```     ```{r sample, echo=FALSE, results='asis'} library(knitr) t1 <- head(mtcars)[1:3] t2 <- head(mtcars)[4:6] print(kable(t1)) print(kable(t2)) ``` 

Output looks like this: enter image description here

like image 694
Doug Fir Avatar asked Jun 26 '16 08:06

Doug Fir


People also ask

How do I align two tables in HTML?

You only need to float the first table. Then you simply add margin-right: 50px; (for example) to it and the second one will be next to it, 50px away. If you want to center them both, put them in a div with some width and add margin: 0 auto; .


2 Answers

Just put two data frames in a list, e.g.

t1 <- head(mtcars)[1:3] t2 <- head(mtcars)[4:6] knitr::kable(list(t1, t2)) 

Note this requires knitr >= 1.13.

like image 159
Yihui Xie Avatar answered Oct 20 '22 14:10

Yihui Xie


I used this Align two data.frames next to each other with knitr? which shows how to do it in html and this https://tex.stackexchange.com/questions/2832/how-can-i-have-two-tables-side-by-side to align 2 Latex tables next to each other. It seems that you cannot freely adjust the lines of the table as you can do it with xtable (does anybody know more about this?). With format = Latex you get a horizontal line after each row. But the documentation shows two examples for other formats. One using the longtable package (additional argument: longtable = TRUE) and the other using the booktabs package (booktabs = TRUE).

--- title: "sample" output: pdf_document header-includes: - \usepackage{booktabs} ---  ```{r global_options, R.options=knitr::opts_chunk$set(warning=FALSE, message=FALSE)} ```   ```{r sample, echo=FALSE, results='asis'} library(knitr) library(xtable)  t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE) t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE)  cat(c("\\begin{table}[!htb]     \\begin{minipage}{.5\\linewidth}       \\caption{}       \\centering",         t1,     "\\end{minipage}%     \\begin{minipage}{.5\\linewidth}       \\centering         \\caption{}",         t2,     "\\end{minipage}  \\end{table}" ))   ``` 

enter image description here

like image 40
Alex Avatar answered Oct 20 '22 14:10

Alex