2

I am trying to create a page with side-by-side tables. I used other SO answers to do this for a simple table

```{r start-block, include=F,echo=F}
library(dplyr)
library(knitr)
library(kableExtra)
```

```{r sample, echo=FALSE, results='asis'}
t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE) %>%  kable_styling(latex_options = c("striped"), font_size=5)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE) %>%  kable_styling(latex_options = c("striped"), font_size=5)
cat("\n")
cat("\\newpage")
cat("\\begin{table}[!htb]")
cat(c("\\begin{minipage}{.5\\linewidth}
       \\caption{}
       \\centering",
         t1,
     "\\end{minipage}%
     \\begin{minipage}{.5\\linewidth}
       \\centering
         \\caption{}",
         t2,
     "\\end{minipage}") )
cat("\\end{table}")
```

Table Output

I am trying to reproduce the same behavior with a custom table (.RDS format file: tbl). This works fine without side-by-side tables.

```{r table, echo=FALSE, results='asis'}
tbl <- readRDS("table.RDS") #load file using the link "tbl"
cat(tbl)
```

enter image description here

byt I am getting an error when I try this table side-by-side.

```{r table2, echo=FALSE, results='asis'}
cat("\n")
cat("\\newpage")
cat("\\begin{table}[!htb]")
cat(c("\\begin{minipage}{.5\\linewidth}
       \\caption{}
       \\centering",
         tbl,
     "\\end{minipage}%
     \\begin{minipage}{.5\\linewidth}
       \\centering
         \\caption{}",
         tbl,
     "\\end{minipage}") )
cat("\\end{table}")
```

! LaTeX Error: Not in outer par mode.

Eric
  • 1,381
  • 9
  • 24
  • 1
    I got the tables to display side-by-side by adding `kable_styling(latex_options = c("striped", "Hold_position"))`. I looked at the Latex for table t1 above and saw that it started with `\begin{table}[H]` and my table `tbl` didn't have [H]. the kableExtra documentation explains how to add the hold parameter as I noted in the beginning. I don't know Latex so I have no clue why this works but it does. – Eric Jun 16 '18 at 07:00
  • Adding `- \usepackage{float}` and `- \floatplacement{table}{H}` to the `header-includes:` section of the YAML header might work (based on this post https://community.rstudio.com/t/how-do-i-specify-h-for-hold-position/25963/4) – Damian Oct 09 '20 at 21:49
  • 1
    You mean `"HOLD_position"`. (That did work for me.) There's only that and `"hold_position"`. – DHW Mar 22 '21 at 20:08

1 Answers1

3

I would recommend using the LaTeX package subcaption for building the side-by-side tables/figures.

An example .Rmd file:

---
title: "Answer for SO Question 50879745"
header-includes:
  \usepackage{subcaption}
  \usepackage{booktabs}
  \usepackage[table]{xcolor}
---


```{r start-block}
library(dplyr)
library(knitr)
library(kableExtra)
opts_chunk$set(echo = FALSE)
``` 

Build two example tables based on the `mtcars` data set.


```{r sample, results='asis'}
t1 <- kable(head(mtcars)[1:3], format = "latex", booktabs = TRUE) %>%  kable_styling(latex_options = c("striped"), font_size=5)
t2 <- kable(head(mtcars)[4:6], format = "latex", booktabs = TRUE) %>%  kable_styling(latex_options = c("striped"), font_size=5) 
```

Modify the tables to use the `subtable` environment and added labels and
captions.

```{r}
t1 <- gsub("\\begin{table}[H]", "\\begin{subtable}[b]{0.48\\linewidth}\n\\caption{\\label{tab:1a}This is Table 1 in the example, but now labeled with a (a).}\n", t1, fixed = TRUE)
t1 <- gsub("\\end{table}", "\\end{subtable}", t1, fixed = TRUE) 

t2 <- gsub("\\begin{table}[H]", "\\begin{subtable}[b]{0.48\\linewidth}\n\\caption{\\label{tab:1b}Shorter caption.}", t2, fixed = TRUE)
t2 <- gsub("\\end{table}", "\\end{subtable}", t2, fixed = TRUE) 
```

Place the tables into the document.

```{r, results = "asis"}
cat("",
    "\\begin{table}[!htb]",
    "\\centering",
    "\\caption{\\label{tab:tab1}Two tables, side-by-side.}",
    t1,
    t2,
    "\\end{table}",
    "",
    sep = "\n") 
```

Another example, start by reading in a rds file.


```{r table, results = "asis"}
tbl <- readRDS("table.RDS") #load file using the link "tbl"
cat(tbl)
```

A few modifications to Table~\ref{tab:comments-block} will be made so that we
can show the table twice in a `subtable`.

```{r table2_mod}
tbl <- gsub("\\begin{table}", "\\begin{subtable}[t]{0.48\\linewidth}", tbl, fixed = TRUE)
tbl <- gsub("\\end{table}", "\\end{subtable}", tbl, fixed = TRUE) 
```

Printing the tables works, see \ref{tab:tab3}, but there is overlap as the
tables are too wide.  Consider the `tabularx` package as a solution or an ad hoc
approach of changing the font size.

```{r, results = "asis"}
cat("",
    "\\begin{table}[!htb]",
    "\\centering",
    "\\caption{\\label{tab:tab3}Two tables, side-by-side.}",
    # "\\scriptsize",
    tbl,
    tbl,
    "\\end{table}",
    # "\\normalsize",
    "",
    sep = "\n") 
```

Which turned into this pdf:

enter image description here

The files and graphics for this example can be found here: https://github.com/dewittpe/so/tree/master/50879745

Peter
  • 7,460
  • 2
  • 47
  • 68