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

Set default font size to NA so tables inherit document font #1937

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

capnrefsmmat
Copy link

@capnrefsmmat capnrefsmmat commented Jan 1, 2025

Summary

In LaTeX and HTML output formats, gt tables previously always set the font size (see #1594). If the tables were being inserted into a document (e.g., in a Quarto document), they did not inherit the font size of the surrounding document, requiring the user to manually set the size option for each table.

Instead, the default table.font.size is NA. Leaving it NA causes the HTML and LaTeX outputs to emit no font size change, so the table is the same size as the surrounding text in the document (or the browser's default body font size). If the user manually sets the table.font.size, it will be respected in both formats.

Note I didn't see where the default font behavior is documented -- should this be added somewhere? Where?

Related GitHub Issues and PRs

Fixes #1852.

Checklist

In LaTeX and HTML output formats, gt tables previously always set the
font size (see rstudio#1594). If the tables were being inserted into a
document (e.g., in a Quarto document), they did not inherit the font
size of the surrounding document, requiring the user to manually set the
size option for each table.

Instead, the default table.font.size is NA. Leaving it NA causes the
HTML and LaTeX outputs to emit no font size change, so the table is the
same size as the surrounding text in the document (or the browser's
default body font size). If the user manually sets the table.font.size,
it will be respected in both formats.

Fixes rstudio#1852.
@CLAassistant
Copy link

CLAassistant commented Jan 1, 2025

CLA assistant check
All committers have signed the CLA.

@olivroy
Copy link
Collaborator

olivroy commented Feb 24, 2025

I agree with the idea, but this requires a lot of manual testing.

  • bookdown (with / without xref)
  • Quarto (with / without xref)
  • rmarkdown
  • Standalone latex.

We have to make sure we are still producing valid output!

As an example, see what I did here to make sure I wasn't introducing new regression #1958

@capnrefsmmat
Copy link
Author

By manual testing, do you mean like the example with screenshots in your original post in #1958, showing the output in different cases?

I did try to ensure there were unit tests and snapshots covering the font size options, but I can render some examples to different document formats if that's what you're looking for.

@olivroy
Copy link
Collaborator

olivroy commented Feb 24, 2025

I mean to ensure that output looks good in different formats, that LaTeX documents compile well!

I did try to ensure there were unit tests and snapshots covering the font size options, but I can render some examples to different document formats if that's what you're looking for.

Yes, that would be great. Also making sure that the correct table is shown if the option is set differently

@capnrefsmmat
Copy link
Author

Okay, I made some test documents each with two tables, one at default size and one at a very large font size (so it's easy to tell if it worked). For example, the Rmd version:

---
title: Test table font size
date: February 25, 2025
output: html_document
---

```{r setup}
library(gt)
```

Standard font size:

```{r}
# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"

# Create a gt table based on preprocessed
# `sp500` table data
test_tbl <- sp500 |>
  dplyr::filter(date >= start_date & date <= end_date) |>
  dplyr::select(-adj_close) |>
  gt() |>
  tab_header(
    title = "S&P 500",
    subtitle = glue::glue("{start_date} to {end_date}")
  ) |>
  fmt_currency() |>
  fmt_date(columns = date, date_style = "wd_m_day_year") |>
  fmt_number(columns = volume, suffixing = TRUE)

test_tbl
```

Non-default font size:

```{r}
test_tbl |>
  tab_options(table.font.size = "24px")
```

Then I tested the following combinations:

Format HTML PDF
Quarto .qmd
R Markdown .rmd
knitr .Rnw (LaTeX) NA
Bookdown pdf_book and gitbook

I also tried labeling the second table as tbl-foobar with a caption and cross-referencing it in Quarto (PDF and HTML).

The outputs all looked reasonable. For instance, in Rnw there was no font size command in the first table (so it used the default), but there was \fontsize{18.0pt}{21.6pt} in the second. In the HTML outputs, I could see in the web developer console that no CSS font rules were applied to the first table (apart from the body default), but they were for the second table.

Are there any other configurations to check, or would you like the test files?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Table font sizes should respect document default if gt font size option is not set
3 participants