gt
gt copied to clipboard
Set default font size to NA so tables inherit document font
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
- [x] I understand and agree to the Code of Conduct.
- [x] I have listed any major changes in the NEWS.
- [x] I have added
testthatunit tests totests/testthatfor any new functionality.
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
By manual testing, do you mean like the example with screenshots in your original post in https://github.com/rstudio/gt/pull/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.
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
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 | |
|---|---|---|
| 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?