Skip to contents

Applies a single font family to every styleable part of a gt table in one call: the title and subtitle, the stubhead, column spanners and labels, row groups, the stub, the body, footnotes, and source notes. Setting the same font through gt::tab_options() means naming each *_font option in turn.

Usage

gt_set_font(
  gt_object,
  font_family,
  from_google_font = TRUE,
  weight = NULL,
  style = NULL
)

Arguments

gt_object

A gt table object to modify.

font_family

Character. The font family to apply to the whole table.

from_google_font

Logical. Should the font be pulled from Google Fonts through gt::google_font()? FALSE treats font_family as a font already installed on the local machine. Defaults to TRUE.

weight

The font weight applied to every part, passed to gt::cell_text(). Either a keyword such as "bold" or a numeric weight. Defaults to NULL, which leaves the weight alone.

style

Character. The font style applied to every part, one of "normal", "italic", or "oblique". Defaults to NULL, which leaves the style alone.

Value

Returns a modified gt table with the font family applied to every covered part.

Details

The font is applied with a single gt::tab_style() over a list of cell locations covering each part of the table. Summary and grand-summary cells are not included, so a font set here does not reach them. When from_google_font is TRUE the family is wrapped in gt::google_font(), which adds the import so the font renders in an exported table without relying on it being installed locally.

Examples

library(gt)

nfc_south <- subset(
  sdv_example_standings, division == "NFC South",
  c(team_name, wins:points_against)
)
gt(nfc_south) |> gt_set_font("Oswald")
team_name wins losses ties win_pct points_for points_against
Carolina Panthers 8 9 0 0.471 311 380
Tampa Bay Buccaneers 8 9 0 0.471 380 411
Atlanta Falcons 8 9 0 0.471 353 401
New Orleans Saints 6 11 0 0.353 306 383
# use a font already installed locally gt(nfc_south) |> gt_set_font("Helvetica", from_google_font = FALSE)
team_name wins losses ties win_pct points_for points_against
Carolina Panthers 8 9 0 0.471 311 380
Tampa Bay Buccaneers 8 9 0 0.471 380 411
Atlanta Falcons 8 9 0 0.471 353 401
New Orleans Saints 6 11 0 0.353 306 383