GT.tab_stub_indent()

Control indentation of row labels in the stub.

Usage

Source

GT.tab_stub_indent(
    rows,
    indent="increase",
)

Indentation of row labels is an effective way to establish visual hierarchy in a table stub. tab_stub_indent() allows for fine-grained control over row label indentation in the stub. You can use an explicit integer indentation level (between 0 and 5), or use a keyword directive: "increase" (the default) or "decrease".

Parameters

rows: RowSelectExpr

The rows to target for the indentation change. We can supply a list of row indices, a single row index integer, or a callable that takes the table data and returns a boolean Series. When targeting rows by name, provide a list of row label strings.

indent: Union[int, Literal["increase", "decrease"]] = "increase"
An indentation directive or explicit integer level. The keyword "increase" (the default) increments the indentation level by 1; "decrease" decrements it by 1. The minimum indentation level is 0 (no indentation) and the maximum is 5. An integer value directly sets the indentation level (clamped to the 05 range).

Returns

GT
The GT object is returned. This is the same object that the method is called on so that we can facilitate method chaining.

Examples

Let’s use a subset of the exibble dataset to create a gt table with row groups and row labels. We’ll use tab_stub_indent() to add indentation to all the row labels in the stub.

from great_tables import GT
from great_tables.data import exibble

exibble_mini = exibble[["num", "char", "row", "group"]].head(8)

(
    GT(exibble_mini, rowname_col="row", groupname_col="group")
    .tab_stub_indent(rows=True, indent=2)
)
num char
grp_a
row_1 0.1111 apricot
row_2 2.222 banana
row_3 33.33 coconut
row_4 444.4 durian
grp_b
row_5 5550.0
row_6 fig
row_7 777000.0 grapefruit
row_8 8880000.0 honeydew

Here’s a more advanced example using the constants dataset. We filter for three groups of physical constants and rename the sub-entries to start with "..." so it’s clear they belong to a parent constant. Then tab_stub_indent() targets those sub-rows (via a Polars expression) and indents them by 4 levels.

from great_tables import GT, stub
from great_tables.data import constants
import polars as pl

constants_mini = (
    pl.from_pandas(constants)
    .select(["name", "value", "uncert", "units"])
    .filter(
        pl.col("name").str.starts_with("atomic mass constant")
        | pl.col("name").str.starts_with("Rydberg constant")
        | pl.col("name").str.starts_with("Bohr magneton")
    )
    .with_columns(
        name=pl.when(
            pl.col("name").str.contains("constant ")
            | pl.col("name").str.contains("magneton ")
        )
        .then(pl.col("name").str.replace(r".*?(?:constant |magneton )", "..."))
        .otherwise(pl.col("name"))
    )
)

(
    GT(constants_mini, rowname_col="name")
    .tab_stubhead(label="Physical Constant")
    .tab_stub_indent(
        rows=pl.col("name").str.starts_with("..."),
        indent=4,
    )
    .fmt_scientific(columns=["value", "uncert"])
    .fmt_units(columns="units")
    .cols_label(value="Value", uncert="Uncertainty", units="Units")
    .cols_width(cases={stub: "250px", "value": "150px", "uncert": "150px", "units": "80px"})
)
/home/runner/work/great-tables/great-tables/great_tables/_render_checks.py:37: RenderWarning: Rendering table with .cols_width() in Quarto may result in unexpected behavior. This is because Quarto performs custom table processing. Either use all percentage widths, or set .tab_options(quarto_disable_processing=True) to disable Quarto table processing.
  warnings.warn(
Physical Constant Value Uncertainty Units
atomic mass constant 1.66 × 10−27 5.00 × 10−37 kg
...energy equivalent 1.49 × 10−10 4.50 × 10−20 J
...energy equivalent in MeV 9.31 × 102 2.80 × 10−7 MeV
Bohr magneton 9.27 × 10−24 2.80 × 10−33 J T−1
...in eV/T 5.79 × 10−5 1.70 × 10−14 eV T−1
...in Hz/T 1.40 × 1010 4.20 Hz T−1
...in inverse meter per tesla 4.67 × 101 1.40 × 10−8 m−1 T−1
...in K/T 6.72 × 10−1 2.00 × 10−10 K T−1
Rydberg constant 1.10 × 107 2.10 × 10−5 m−1
...times c in Hz 3.29 × 1015 6.40 × 103 Hz
...times hc in eV 1.36 × 101 2.60 × 10−11 eV
...times hc in J 2.18 × 10−18 4.20 × 10−30 J