rpdflatexgtquarto

Scale Quarto PDF table output to fit page


How can I scale a table to fit a PDF page in Quarto?

Example:

---
title: "Untitled"
format: pdf
---

```{r setup}
#| include: false

library(dplyr)
library(gt)
```


```{r tbl}
#| include: true
#| echo: false


tibble(a = 1000, b = 2000, c = 3000, d = 4000, e = 5000, f = 6000, g = 7000, 
       h = 8000, i = 9000, j = 10000, k = 11000, l = 12000, m = 13000, n = 14000, 
       o = 15000, p = 16000, q = 17000, r = 18000, s = 19000, t = 20000, u = 21000, 
       v = 22000, w = 23000, x = 24000, y = 25000, z = 26000) %>% 
  gt()
```

This produces a PDF where the table runs off the page:

Image of PDF with table running off of page

I have tried using the gt tab_options() function, but this appears to only work with HTML outputs.


Solution

  • you have actually too many columns to fit in a potrait mode, I would recommend to try landscape mode.

    Nevertheless, if you want to fit these too many columns, you can use small font size, reduce the gap between columns and take some space off the left and right margins.

    ---
    title: "Untitled"
    format: pdf
    ---
    
    ```{r setup}
    #| include: false
    
    library(dplyr)
    library(gt)
    ```
    
    \begingroup
    \setlength{\LTleft}{0pt minus 500pt}
    \setlength{\LTright}{0pt minus 500pt}
    \fontsize{5pt}{7pt}\selectfont
    \addtolength{\tabcolsep}{-3pt}
    
    ```{r tbl, results='asis'}
    #| include: true
    #| echo: false
    
    tibble(a = 1000, b = 2000, c = 3000, d = 4000, e = 5000, f = 6000, g = 7000,
           h = 8000, i = 9000, j = 10000, k = 11000, l = 12000, m = 13000, n = 14000,
           o = 15000, p = 16000, q = 17000, r = 18000, s = 19000, t = 20000, u = 21000,
           v = 22000, w = 23000, x = 24000, y = 25000, z = 26000) %>%
      gt() %>%
      as_latex()
    ```
    \endgroup
    
    

    a wide table fitted to a page (kind of forced)