gt: how to modify line spacing in cells with mutli-line text

Hello,

I try to modify the distance between lines in a cell with a longer text which includes line breaks. I tried

tab_options( data_row.padding = px(.5) )

however this only reduces the vertical space between the first/last line and the cell's border.

What I am looking for is a way to decrease the space between the lines, i.e. to make the table more compact.

library(tidyverse)
library(gt)

mtcars <- mtcars %>% 
  mutate(text="long text with line breaks\nlong text with linebreaks\nlong text with linebreaks")

mtcars %>% 
  tibble::rownames_to_column(., var = "name") %>% 
  select(name, text) %>% 
  gt() %>% 
  gt::tab_header(., title="row padding .5") %>% 
  tab_options(
    data_row.padding = px(.5) 
  )

Note that I am producing this table as part of a blog/distill post.
Here's the code/result of what I tried.

Many thanks!

While I couldn't find a way to do exactly what you're asking about, have you considered altering the font size of the text column? This will definitely make the table more compact.

  tibble::rownames_to_column(., var = "name") %>% 
  select(name, text) %>% 
  gt() %>% 
  gt::tab_header(., title="row padding .5") %>% 
  tab_options(
    data_row.padding = px(.5) 
  ) %>%  
  tab_style(
    style = cell_text(size = gt::px(10)),
    locations = cells_body(
      columns = text,
      rows = gt::everything()
    )
  )
1 Like

Here is what I found works if you use <br> instead of \n and add css.

my_gt |>
gt::opt_css(
    css = "
    br {
      display: block;
      content: \"\";
      margin-top: -6px;
    }
    "
  )

example of <br> inclusion

merge_comp_gt <- function(gt, col_label) {
  gt::cols_merge(
    data = gt,
    columns = gt::starts_with(col_label),
    pattern = "<< {1} >> << <br>────<br> {2} >>"
  )
}