Search code examples
rr-markdownkabletintkableextra

kable_styling with full_width = T in a tint document


I have the following tint PDF document:

---
title: "Title"
subtitle: "Subtitle"
author: "Author"
date: "`r Sys.Date()`"
output: tint::tintPdf
---

```{r echo = FALSE, message = FALSE}
# Load library
library(dplyr)

# Create data frame
df <- data.frame(A = runif(10), 
                 B = runif(10), 
                 C = runif(10), 
                 D = runif(10),
                 E = runif(10),
                 F = runif(10),
                 G = runif(10),
                 H = runif(10))

# Print as a table
knitr::kable(df, booktabs = TRUE, format = "latex", 
             caption = "This is a caption in the margin.") 
```

This produces a PDF with the following table:

enter image description here

The table is wide and spills out over the caption. To avoid this, I can specify it's a full width table using full_width = TRUE in the kable_styling function of kableExtra.

---
title: "Title"
subtitle: "Subtitle"
author: "Author"
date: "`r Sys.Date()`"
output: tint::tintPdf
---

```{r echo = FALSE, message = FALSE}
# Load library
library(dplyr)

# Create data frame
df <- data.frame(A = runif(10), 
                 B = runif(10), 
                 C = runif(10), 
                 D = runif(10),
                 E = runif(10),
                 F = runif(10),
                 G = runif(10),
                 H = runif(10))

# Print as a table
knitr::kable(df, booktabs = TRUE, format = "latex", 
             caption = "This is a caption in the margin.") %>%
  kableExtra::kable_styling(full_width = TRUE)
```

This gives the following error:

! LaTeX Error: Environment tabu undefined.

Error: Failed to compile Test.tex. See Test.log for more info. Execution halted

It seems to be upset about the tabu package (or lack thereof). So, I add in this package in my YAML, like so:

---
title: "Title"
subtitle: "Subtitle"
author: "Author"
date: "`r Sys.Date()`"
output: tint::tintPdf
header-includes:
  - \usepackage{tabu}
---

This runs, but produces the following:

enter image description here

Now, the contents of the table overlaps. Humpf. Even if I include fig.fullwidth = TRUE in the chunk options I have no luck.

How do I produce a full width table in this situation?


Solution

  • Here's a workaround. By specifying longtable = TRUE in kable, the caption is bumped to the top. Also, in the YAML: tables: yes. Counterintuitively, using kable_styling with full_width = TRUE will squish the table into the full width of the main body and not the main body + margin.

    ---
    title: "Title"
    subtitle: "Subtitle"
    author: "Author"
    date: "`r Sys.Date()`"
    output: tint::tintPdf
    tables: yes
    ---
    
    ```{r echo = FALSE, message = FALSE}
    # Load library
    library(dplyr)
    
    # Create data frame
    df <- data.frame(A = runif(10), 
                     B = runif(10), 
                     C = runif(10), 
                     D = runif(10),
                     E = runif(10),
                     F = runif(10),
                     G = runif(10),
                     H = runif(10))
    
    # Print as a table
    knitr::kable(df, booktabs = TRUE, format = "latex", longtable = TRUE,
                 caption = "This is a caption in the margin.") 
    ```
    

    enter image description here