How to wrap text in a `gt::as_latex()` table

  Kiến thức lập trình

When using the R package gt to generate a LaTeX table, wrapping of very long column entries does not happen. I tried several options in tab_style(), but they did not have any effect on indentation:

---
title: "Untitled"
author: "test"
date: "`r Sys.Date()`"
output:
  pdf_document:
    keep_tex: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(gt)
```


```{r, echo=FALSE}
islands_tbl <- 
  tibble(
    name = names(islands),
    size = islands
  ) |>
  arrange(desc(size)) |>
  slice(1:10) |>
  mutate(name = if_else(name == "Asia", "Asia shares the landmass of Eurasia with Europe, and of Afro-Eurasia with both Europe and Africa. In general terms, it is bounded on the east by the Pacific Ocean, on the south by the Indian Ocean, and on the north by the Arctic Ocean.", name))
gt(islands_tbl)  |>
  tab_style(
    style = cell_text(align = "justify",
                      stretch = "extra-condensed",
                      whitespace = "break-spaces"),
    locations = cells_body(
      columns = name
    )
  ) |>
  as_latex()
```

Looking at the .tex file shows that begin{longtable}{lr} is used for the table. Is there any possibility to switch this to begin{longtable}{p{4.5cm}r} or something similar in order to achieve text wrapping?

Undesired output:

Desired result:

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT