Skip to content

Style API

Styles define how selected table cells are rendered. Use them with TypTable.tab_style and a locator from typ_tables.locators:

from narwhals import selectors as ncs

from typ_tables import TypTable, locators, style

table = (
    TypTable(df)
    .tab_style(
        locator=locators.LocBody(columns=ncs.numeric()),
        text=style.TextStyle(weight="bold"),
        cell=style.CellStyle(align="right"),
    )
)

TextStyle controls the content inside a cell, such as font size, weight, and text fill. CellStyle controls the table cell container, such as alignment, inset, background fill, and borders. Sides is a helper for properties that can vary by side, such as cell inset or stroke.

Style values are written as Typst-compatible strings and are passed through to the generated Typst. For body and stub locators, style fields can also be lists or Narwhals expressions so each row can resolve to a different value. For single-region locators, such as LocHeader and LocStubhead, style fields must be scalar values.

Styling Types

typ_tables.ttypes.Alignment module-attribute

Alignment = Literal[
    "start",
    "end",
    "left",
    "center",
    "right",
    "top",
    "horizon",
    "bottom",
]

Typst alignment keyword for table cell content.

typ_tables.ttypes.Auto module-attribute

Auto = Literal['auto']

Typst automatic value keyword.

typ_tables.ttypes.Relative module-attribute

Relative = str

Typst relative length value.

Use a Typst-compatible string such as "10pt", "1em", or "20%".

typ_tables.style.Fill module-attribute

Fill = str

Typst fill value.

Use any Typst-compatible paint string, such as "red" or "#f2f2f2".

typ_tables.style.Length module-attribute

Length = str

Typst length value.

Use a Typst-compatible string such as "10pt", "1em", or "0.2cm".

typ_tables.style.Stroke module-attribute

Stroke = str

Typst stroke value.

Use any Typst-compatible stroke string, such as "1pt + black".

typ_tables.style.FontStyle module-attribute

FontStyle = Literal['normal', 'italic', 'oblique']

Typst font style keyword.

typ_tables.style.FontWeight module-attribute

FontWeight = (
    Literal[
        "thin",
        "extralight",
        "light",
        "regular",
        "medium",
        "semibold",
        "bold",
        "extrabold",
        "black",
    ]
    | int
)

Typst font weight keyword or numeric font weight.

typ_tables.style.Sides dataclass

Bases: Generic[T]

Describe per-side values for Typst cell properties.

Use this helper for properties that can vary by side, such as inset and stroke. Only fields with non-None values are emitted to Typst.

rest class-attribute instance-attribute

rest: T | None = None

Fallback value for sides without a more specific value.

x class-attribute instance-attribute

x: T | None = None

Horizontal value applied to the left and right sides.

y class-attribute instance-attribute

y: T | None = None

Vertical value applied to the top and bottom sides.

top class-attribute instance-attribute

top: T | None = None

Top-side value.

right class-attribute instance-attribute

right: T | None = None

Right-side value.

left class-attribute instance-attribute

left: T | None = None

Left-side value.

bottom class-attribute instance-attribute

bottom: T | None = None

Bottom-side value.

typ_tables.style.Inset module-attribute

Inset = Relative | Sides[Relative] | dict[str, str]

Cell inset value.

Use a Relative value for uniform inset, Sides for per-side inset, or a dictionary accepted by Sides.

typ_tables.style.FullStroke module-attribute

FullStroke = Stroke | Sides[Stroke] | dict[str, str]

Stroke value accepted by style fields.

Use a Stroke value for a uniform stroke, Sides for per-side stroke, or a dictionary accepted by Sides.

Style Containers

typ_tables.style.TextStyle dataclass

Text-level style properties for selected table cells.

These fields map to Typst text(...) arguments. Values are written as Typst strings, except for fractions, which is rendered as a boolean. Scalar values apply the same style everywhere the locator selects. Lists and Narwhals expressions resolve one value per data row for row-based locators.

font class-attribute instance-attribute

font: str | list[str] | Expr | None = None

Font family name.

style class-attribute instance-attribute

style: FontStyle | list[FontStyle] | Expr | None = None

Font FontStyle.

weight class-attribute instance-attribute

weight: FontWeight | list[FontWeight] | Expr | None = None

Font FontWeight.

stretch class-attribute instance-attribute

stretch: str | list[str] | Expr | None = None

Font stretch value.

size class-attribute instance-attribute

size: Length | list[Length] | Expr | None = None

Text size as a Length value.

fill class-attribute instance-attribute

fill: Fill | list[Fill] | Expr | None = None

Text Fill color or paint.

stroke class-attribute instance-attribute

stroke: FullStroke | list[FullStroke] | Expr | None = None

Text FullStroke.

tracking class-attribute instance-attribute

tracking: Length | list[Length] | Expr | None = None

Additional spacing between glyphs as a Length value.

spacing class-attribute instance-attribute

spacing: Relative | list[Relative] | Expr | None = None

Additional spacing between words as a Relative value.

fractions class-attribute instance-attribute

fractions: bool | list[bool] | Expr | None = None

Whether Typst should render numeric fractions specially.

typ_tables.style.CellStyle dataclass

Cell-level style properties for selected table cells.

These fields map to Typst table.cell(...) arguments. Scalar values apply the same style everywhere the locator selects. Lists and Narwhals expressions resolve one value per data row for row-based locators.

inset class-attribute instance-attribute

inset: Inset | list[Inset] | Expr | None = None

Cell padding/inset as an Inset value.

align class-attribute instance-attribute

align: (
    Auto | Alignment | list[Auto | Alignment] | Expr | None
) = None

Cell content Alignment or Auto.

fill class-attribute instance-attribute

fill: Fill | list[Fill] | Expr | None = None

Cell background Fill color or paint.

stroke class-attribute instance-attribute

stroke: FullStroke | list[FullStroke] | Expr | None = None

Cell border FullStroke.