rlatexr-markdownkableextra

how to align two tables side by side


I feel that I am close but can't quite able to align two tables side by side on Rmarkdown pdf. Could someone help please. Code below:

    ---
    title: "Caption"
    output: pdf_document
    params:
      runs: 
      - Late
      - Early
    date: "`r Sys.Date()`"
    ---
    
    ```{r}
    library(kableExtra)
    library(knitr)
    library(rmarkdown)
    
    k1 <- structure(list(year = c("2011", "2011", "2011", "2011", "2012", 
    "2012", "2012", "2012"), race2 = c("Fall", "LateFall", "Spring", 
    "Winter", "Fall", "LateFall", "Spring", "Winter"), total = c(67174, 
    1395, 2720, 18126, 70213, 2448, 3590, 29537)), class = "data.frame", row.names = c(NA, 
    -8L))
    
    ```
    
    ```{r, echo=FALSE, results= 'asis'}
    caption_text1 <- paste("This table represents t1", paste(params$runs, collapse = ", "))
    caption_text2 <- paste("This table represents t2", paste(params$runs, collapse = ", "))
    
t1 <- kbl(k1, booktabs = TRUE, format = 'latex') 

t2 <- kbl(k1, booktabs = TRUE, format = 'latex') 

cat(c("\\begin{table}[!htb]
      \\begin{minipage}{.4\\linewidth}
      \\caption{caption_text1}     #I want caption from params
      \\vspace{3mm}
      \\centering",
        t1,
    "\\end{minipage}%
    \\begin{minipage}{.8\\linewidth}
      \\centering
      \\caption{caption_text2}
        \\vspace{3mm}",
        t2,
    "\\end{minipage}
\\end{table}"
))
    
    ```

UPDATE: The code above works but I want the caption to be dynamic and take the params from the yaml


Solution

  • Borrowing the LaTeX from this great answer making sure to include \usepackage{subcaption} in the LaTeX header via yaml you can slightly change your MRE to:

    ---
    title: "Two tables with dynamic captions"
    output: pdf_document
    params:
      runs: 
      - Late
      - Early
    date: "`r Sys.Date()`"
    header-includes:
      - \usepackage{subcaption}
    ---
    
    ```{r setup, echo=FALSE, include=FALSE}
    library(kableExtra)
    library(knitr)
    library(rmarkdown)
    
    k1 <- structure(list(year = c("2011", "2011", "2011", "2011", "2012", 
    "2012", "2012", "2012"), race2 = c("Fall", "LateFall", "Spring", 
    "Winter", "Fall", "LateFall", "Spring", "Winter"), total = c(67174, 
    1395, 2720, 18126, 70213, 2448, 3590, 29537)), class = "data.frame", row.names = c(NA, 
    -8L))
    
    ```
    
    ```{r, echo=FALSE, results= 'asis'}
    
    caption_text1 <- paste("This table represents t1", paste(params$runs, collapse = ", "))
    caption_text2 <- paste("This table represents t2", paste(params$runs, collapse = ", "))
    
    
    t1 <- kbl(k1, booktabs = TRUE, format = 'latex') 
    t2 <- kbl(k1, booktabs = TRUE, format = 'latex') 
    
    cat(sprintf("
    \\begin{table}[htb]
        \\caption{Global caption}
        \\centering
        \\begin{subtable}[t]{0.48\\textwidth}
            \\centering
            \\caption{%s}
            %s   
        \\end{subtable}%%
        \\hfill
        \\begin{subtable}[t]{0.48\\textwidth}
            \\centering
            \\caption{%s}
            %s
        \\end{subtable}
    \\end{table}
    ", caption_text1, t1, caption_text2, t2))
    
    ```
    

    giving a nice table

    out