Skip to content

Refine Latex Tables with Group Cols #1804

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

Merged
merged 9 commits into from
Jul 18, 2024
Merged

Conversation

kbrevoort
Copy link
Collaborator

Summary

I was making some tables for a paper I'm writing this afternoon and found a few minor issues when Latex tables are created with row_group_as_column = TRUE that this PR addresses. There are three changes:

  1. The multirow Latex command used for the group column use an "=" which instructs Latex to respect the column widths that have been selected. However, when no column widths have been selected, it defaults to using all the width available. This PR changes this behavior, replacing the "=" with "*", so the natural width of the column will be used.
  2. There was a bug in the code that caused an error when assigning widths to the group column that I've fixed. (There was a boolean vector where I wanted to count the TRUE values, but instead was taking the length of the vector.)
  3. Stubhead labels were not being properly centered when widths were specified. The stubhead label is now properly centered.

This Quarto code:

---
title: "Untitled"
execute: 
  echo: false
format: pdf
---

```{r}
#| include: false
library(tidyverse)
library(gt)
tbl <- select(exibble, group, row, num, currency) |>
  gt(rowname_col = 'row', 
     groupname_col = 'group',
     row_group_as_column = TRUE) |>
  tab_row_group(label = 'Group 1', 
                rows = 1:3) |>
  tab_stubhead('Stubhead') |>
  tab_options(latex.use_longtable = TRUE)

tbl

currently produces this output:

Screenshot 2024-07-18 at 1 35 04 AM

With this PR, the same code will now produce:

Screenshot 2024-07-18 at 1 36 14 AM

Related GitHub Issues and PRs

None.

Checklist

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Collaborator

@olivroy olivroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your diligence! Please add news for the change!

I think that this fixes #1799.

@olivroy
Copy link
Collaborator

olivroy commented Jul 18, 2024

@kbrevoort Could you run source("scripts/visual-test-latex.R") and commit the result?

kbrevoort and others added 5 commits July 18, 2024 10:13
@olivroy
Copy link
Collaborator

olivroy commented Jul 18, 2024

@kbrevoort Wow! It actually fixes #1799. See the second table in the visual examples. The second page of https://github.com/rstudio/gt/blob/8744851023d7bee722d74bfd9693b17cb3b232e2/pkgdown/assets/gt-latex.pdf no longer is baffled

Before the PR:
image

This PR:
image

@rich-iannone
Copy link
Member

Thank you @kbrevoort and @olivroy ! Will merge this in now.

@rich-iannone rich-iannone merged commit 56e6c2f into rstudio:master Jul 18, 2024
12 checks passed
@kbrevoort kbrevoort deleted the fixgroupcol branch July 18, 2024 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants