Scale Quarto PDF table output to fit page
Asked Answered
V

2

8

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.

Vulpecula answered 16/10, 2022 at 19:21 Comment(0)
N
13

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)


Nadeen answered 17/10, 2022 at 3:53 Comment(1)
This is SO helpful and SO needed. Thank you.Godlike
G
0

I can't find the answer I found on this for stackoverflow, but it was the same question just with RMarkdown.

I would check out the kableExtra extra package. there is a function called kable_styling where you can change the latex option to "scaled_down", and it should get you closer to fitting the table (may have to tinker with it a bit more).

A great resource- https://rdrr.io/cran/kableExtra/f/inst/doc/awesome_table_in_pdf.pdf

#| include: true
#| echo: false

example1 <-
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)

 kbl(example1) |>
 kable_styling(
  latex_options = "scale_down"
)
Galligaskins answered 13/9, 2024 at 0:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.