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,
  gt_table = 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.

gt_table

Deprecated. Use gt_object.

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)

gt(head(mtcars)) %>% gt_set_font("Oswald")
mpg cyl disp hp drat wt qsec vs am gear carb
21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# use a font already installed locally gt(head(iris)) %>% gt_set_font("Helvetica", from_google_font = FALSE)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa