Search code examples
rpdfr-markdowntabular

Rmarkdown side-by-side tables spacing


I'm trying to print two table side-by-side through Rmarkdown, output as PDF.

I can print the tables out fine, but they end up stuck really close together and I cannot find a way to create more space between them. The solutions I find in other posts returns strange output or errors, e.g. the second one from here just gives an 'error 43': Align multiple tables side by side

My code is this:

```{r, echo=FALSE}
library(knitr)
kable(list(head(bymonth),head(bydecade)),align='c')
```

Tables

Anyone know how to add some spacing between those two tables?


Solution

  • Incorporating the answer given here you could do it manually like this:

    ```{r, echo = FALSE, results = 'asis', warning = F}
    library(knitr, quietly = T)
    t1 <- kable(head(mtcars[,1:2]), format = 'latex')
    t2 <- kable(head(mtcars[,3:4]), format = 'latex')
    cat(c("\\begin{table}[h] \\centering ", 
          t1,
        "\\hspace{1cm} \\centering ",
          t2,
        "\\caption{My tables} \\end{table}"))  
    ```
    

    The basic idea is to create the tables individually and align them with plain latex. The spacing is added by \\hspace{1cm}.

    enter image description here