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:
tab_style()
has no effectFrom ?tab_style
we can read:
At present this function is focused on the application of styles for HTML output only (as such, other output formats will ignore all tab_style() calls)
Because you are targeting LaTeX as opposed to HTML, calls to tab_style()
are going to be ignored.
Instead here are two potential solutions. One using cols_width()
and another that performs a find and replace using your postulated LaTeX code.
cols_width()
solutionPerhaps cols_width()
will suffice?
---
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) |>
cols_width(name ~ px(125)) |>
as_latex()
```
If you really prefer your solution, you could do a find and replace on the LaTeX generated from as_latex()
like so:
---
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))
latex_table <- gt(islands_tbl) |>
as_latex()
latex_table[[1]] <- sub(
"\\begin{longtable}{lr}",
"\\begin{longtable}{p{4.5cm}r}",
latex_table[[1]],
fixed = TRUE
)
latex_table
```
Reprex files hosted with on GitHub