Skip to contents

Sets column widths. Numeric values are converted to Typst units via .unit. String values are passed through directly to Typst (e.g. "auto", "1fr", "100pt", "50%"). Validity of strings is not checked in R — Typst will error on invalid values.

Usage

tt_widths(table, ..., .unit = function(x) paste0(x, "fr"), .default = NULL)

Arguments

table

A typst_table object.

...

Width values as numbers or strings. Can be unnamed (positional, applied in column order) or named by column name. Positional mode requires exactly one value per column.

.unit

Function applied to numeric values to produce a Typst length string. Default appends "fr" (fractional units), so 1 becomes "1fr".

.default

When using named columns, the width to apply to columns not explicitly mentioned. NULL (default) leaves unmentioned columns unchanged. Any other value (e.g. "1fr", "auto") is applied to all unmentioned columns.

Value

The modified typst_table object.

Details

Column widths follow last-value-in-pipe-wins logic: calling tt_widths() a second time with named columns only updates those columns.

Fractional units (fr) distribute available space proportionally. For example, tt_widths(tbl, 1, 2, 1) creates columns at 25%, 50%, 25% of the container width.

Examples

# Proportional widths (25%, 50%, 25%)
tt(mtcars[1:5, 1:3]) |> tt_widths(1, 2, 1)
#> #table(
#>   columns: (1fr, 2fr, 1fr),
#>   stroke: none,
#>   table.header(
#>     table.hline(stroke: 1pt),
#>     [mpg], [cyl], [disp],
#>     table.hline(stroke: 0.5pt)
#>   ),
#>   [21], [6], [160],
#>   [21], [6], [160],
#>   [22.8], [4], [108],
#>   [21.4], [6], [258],
#>   [18.7], [8], [360],
#>   table.hline(stroke: 1pt)
#> ) 

# String widths passed through directly
tt(mtcars[1:5, 1:3]) |> tt_widths("100pt", "auto", "50%")
#> #table(
#>   columns: (100pt, auto, 50%),
#>   stroke: none,
#>   table.header(
#>     table.hline(stroke: 1pt),
#>     [mpg], [cyl], [disp],
#>     table.hline(stroke: 0.5pt)
#>   ),
#>   [21], [6], [160],
#>   [21], [6], [160],
#>   [22.8], [4], [108],
#>   [21.4], [6], [258],
#>   [18.7], [8], [360],
#>   table.hline(stroke: 1pt)
#> ) 

# Update only one column, leave others unchanged
tt(mtcars[1:5, 1:3]) |> tt_widths(cyl = 2)
#> #table(
#>   columns: (auto, 2fr, auto),
#>   stroke: none,
#>   table.header(
#>     table.hline(stroke: 1pt),
#>     [mpg], [cyl], [disp],
#>     table.hline(stroke: 0.5pt)
#>   ),
#>   [21], [6], [160],
#>   [21], [6], [160],
#>   [22.8], [4], [108],
#>   [21.4], [6], [258],
#>   [18.7], [8], [360],
#>   table.hline(stroke: 1pt)
#> ) 

# Set all unmentioned columns to auto
tt(mtcars[1:5, 1:3]) |> tt_widths(cyl = 2, .default = "auto")
#> #table(
#>   columns: (auto, 2fr, auto),
#>   stroke: none,
#>   table.header(
#>     table.hline(stroke: 1pt),
#>     [mpg], [cyl], [disp],
#>     table.hline(stroke: 0.5pt)
#>   ),
#>   [21], [6], [160],
#>   [21], [6], [160],
#>   [22.8], [4], [108],
#>   [21.4], [6], [258],
#>   [18.7], [8], [360],
#>   table.hline(stroke: 1pt)
#> ) 

# Custom numeric unit
tt(mtcars[1:5, 1:3]) |> tt_widths(1, 2, 1, .unit = \(x) paste0(x, "pt"))
#> #table(
#>   columns: (1pt, 2pt, 1pt),
#>   stroke: none,
#>   table.header(
#>     table.hline(stroke: 1pt),
#>     [mpg], [cyl], [disp],
#>     table.hline(stroke: 0.5pt)
#>   ),
#>   [21], [6], [160],
#>   [21], [6], [160],
#>   [22.8], [4], [108],
#>   [21.4], [6], [258],
#>   [18.7], [8], [360],
#>   table.hline(stroke: 1pt)
#> )