Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify width of LaTeX table #119

Closed
krlmlr opened this issue Jan 1, 2019 · 6 comments
Closed

Specify width of LaTeX table #119

krlmlr opened this issue Jan 1, 2019 · 6 comments

Comments

@krlmlr
Copy link
Contributor

krlmlr commented Jan 1, 2019

The setting tab_options(table.width = pct(100)) doesn't seem to be respected when rendering to LaTeX, see reprex below. Is this something you'd consider supporting?

It seems that the longtable LaTeX environment is used to render all tables. The fairly new xltabular package could be an option to render long tables with support for the X column modifier, which could perhaps be applied to all columns that are left-aligned.

Related: setting column width (#111).

library(gt)

default_latex <-
  data.frame(a = 1, b = 2) %>%
  gt() %>%
  as_latex()

full_width_latex <-
  data.frame(a = 1, b = 2) %>%
  gt() %>%
  tab_options(table.width = pct(100)) %>%
  as_latex()

identical(default_latex, full_width_latex)
#> [1] TRUE

Created on 2019-01-01 by the reprex package (v0.2.1.9000)

@rich-iannone
Copy link
Member

Thanks for the helpful comments here. Most of the options in tab_options() relate to HTML output but the goal is to make them largely independent of the output format (with style translation where necessary and perhaps warnings where there is no compatibility). Because LaTeX support came a bit later, it needs to catch up to HTML on these options (and RTF will need to do the same).

The plan then is to create a compatibility table that states where the options are currently working. My thought is to add that a vignette that's focussed on table options or perhaps to a more development-focussed article.

We should use some alternative to the longtable LaTeX package. I tried (unsuccessfully) to use other LaTeX packages that have better options for tables and repeating headers across pages. I think it's worth experimenting with the other packages again because longtable is lean on options (and xltabular does appear to be much better). Using longtable was the safe choice at the start because it's an included package in a tinytex LaTeX installation. If I were to include a LaTeX dependency that's not available by default, I think I'd then need to include functions that support package management via tlmgr—which is not a bad/difficult thing at all, it just needs to be worked on.

Let's keep the discussion going on this.

@jankowtf
Copy link

jankowtf commented Nov 7, 2019

I really love the {gt} package so far, you've built a really nice interface!

The only thing that keeps me from shipping a professional PDF doc out the door is the scaling issue, though.

Has there been any progress on this?

@zross
Copy link

zross commented Nov 1, 2021

Just adding a comment in case it helps others. I also love {gt} but the table scaling for latex/PDF is requiring workarounds. (I see this referenced here also #329)

For now we're doing something like:

gt::gt() %>% gt::gtsave("~/junk/table.png")

# Then in Rmd file something like this

```{r, echo = FALSE, warning = FALSE, out.width="75%"}
  knitr::include_graphics("~/junk/table.png")

@hyortiz
Copy link

hyortiz commented Jun 5, 2022

Has anyone found a solution already to the scaling issue with the gt package knitting to a pdf ? I try to use table.width without success. (table.background.color) doesn't work either. The code works wonders in R , however the pdf output just completely ignores the formatting.

Also I try to use the above suggestion : gt::gt(gt_tbl) %>% gt::gtsave("~/table.png") without success.

My code:
gt_tbl <- gt(head(edx,20)) %>%
tab_header(
title = md("MovieLens dataset"),
subtitle = md("First 20 records from the dataset")
) %>%
tab_stubhead(label = "userId")

#show the gt table
gt_tbl %>% tab_options(table.width = pct(75)) %>%
tab_options(table.background.color = "lightcyan")

@rich-iannone rich-iannone added this to the FUTURE milestone Aug 22, 2022
kbrevoort added a commit to kbrevoort/gt that referenced this issue Nov 18, 2023
Anchors LaTeX longtable to match a width set by the user.  Table width is achieved by changing the space between columns to produce a table that matches the specified width.
@kbrevoort kbrevoort mentioned this issue Nov 20, 2023
3 tasks
@elipousson
Copy link
Contributor

Inspired by an example shared by Andrew Heiss I wrote a wrapper function that I'm using to support specification of percent widths in LaTeX output tables. It currently requires passing column names as a character vector and you need to manually set your overall text width but otherwise it is working really well for me.

I may update it so the columns are optional and you can get the column names from the boxhead element of the gt table but, obviously, if someone tackles this issue within {gt} I won't need the function at all.

perc <- function(x,
                 textwidth = 7.5,
                 dpi = 96,
                 unit = "in",
                 scale = TRUE,
                 use_pct = knitr::is_html_output()) {
  if (scale && (x < 1)) {
    x <- x * 100
  }

  if (use_pct) {
    return(gt::pct(x))
  }

  paste0(textwidth * (x / dpi), unit)
}
cols_width_ext <- function(data,
                           columns = NULL,
                           widths = NULL,
                           .list = NULL,
                           textwidth = 7.5,
                           dpi = 96,
                           unit = "in",
                           scale = TRUE) {
  if (is.null(.list)) {
    if (is.null(widths)) {
      return(data)
    }

    if (rlang::is_named(widths)) {
      columns <- columns %||% names(widths)
    }

    stopifnot(all(purrr::map_lgl(columns, is.character)))

    .list <- lapply(
      seq_along(widths),
      \(i) {
        rlang::new_formula(
          columns[[i]],
          perc(
            widths[[i]],
            textwidth = textwidth,
            dpi = dpi,
            unit = unit,
            scale = scale
            )
        )
      }
    )
  }

  gt::cols_width(
    data,
    .list = .list
  )
}

Created on 2023-12-13 with reprex v2.0.2

@olivroy olivroy modified the milestones: FUTURE, v0.11.0 Jul 15, 2024
@olivroy
Copy link
Collaborator

olivroy commented Jul 15, 2024

Fixed in gt 0.11 #1465

@olivroy olivroy closed this as completed Jul 15, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in R Markdown Team Projects Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment