#show figure: set block(breakable: false)
#figure( // start preamble figure
  caption: smallcaps([Motor Trend Car Road Tests]),
  kind: table, // end preamble figure

block[ // start block

  #let style-dict = (
    // tinytable style-dict after
    "0_0": 0, "0_1": 0, "0_2": 0, "0_3": 0, "0_4": 0, "1_4": 0, "2_4": 0, "3_4": 0
  )

  #let style-array = ( 
    // tinytable cell style after
    (smallcaps: true,),
  )

  // Helper function to get cell style
  #let get-style(x, y) = {
    let key = str(y) + "_" + str(x)
    if key in style-dict { style-array.at(style-dict.at(key)) } else { none }
  }

  #show table.cell: it => {
    if style-array.len() == 0 { return it }
    
    let style = get-style(it.x, it.y)
    if style == none { return it }
    
    let tmp = it
    if ("fontsize" in style) { tmp = text(size: style.fontsize, tmp) }
    if ("color" in style) { tmp = text(fill: style.color, tmp) }
    if ("indent" in style) { tmp = pad(left: style.indent, tmp) }
    if ("underline" in style) { tmp = underline(tmp) }
    if ("italic" in style) { tmp = emph(tmp) }
    if ("bold" in style) { tmp = strong(tmp) }
    if ("mono" in style) { tmp = math.mono(tmp) }
    if ("strikeout" in style) { tmp = strike(tmp) }
    if ("smallcaps" in style) { tmp = smallcaps(tmp) }
    tmp
  }

  // tinytable align-figure before
  #let tinytable-coldata = (
    ([Sepal.Length], [5.1], [4.9], [4.7], [4.6], [5.0], [5.4]),
    ([Sepal.Width], [3.5], [3.0], [3.2], [3.1], [3.6], [3.9]),
    ([Petal.Length], [1.4], [1.4], [1.3], [1.5], [1.4], [1.7]),
    ([Petal.Width], [0.2], [0.2], [0.2], [0.2], [0.2], [0.4]),
    ([Species], [setosa], [setosa], [setosa], [setosa], [setosa], [setosa]),
  )

  #context layout(size => {
    let tinytable-naturals = tinytable-coldata.map(col => measure(grid(columns: 1, inset: 5pt, ..col)).width)
    let tinytable-total = calc.min(tinytable-naturals.sum(), size.width)
    block(width: tinytable-total)[

  #table( // tinytable table start
    columns: tinytable-naturals.map(w => w.pt() * 1fr),
    stroke: none,
    rows: auto,
    align: (x, y) => {
      let style = get-style(x, y)
      if style != none and "align" in style { style.align } else { left }
    },
    fill: (x, y) => {
      let style = get-style(x, y)
      if style != none and "background" in style { style.background }
    },
 table.hline(y: 1, start: 0, end: 5, stroke: 0.05em),
 table.hline(y: 7, start: 0, end: 5, stroke: 0.08em),
 table.hline(y: 0, start: 0, end: 5, stroke: 0.08em),
    // tinytable lines before

    // tinytable header start
    table.header(
      repeat: true,
[Sepal.Length], [Sepal.Width], [Petal.Length], [Petal.Width], [Species],
    ),
    // tinytable header end

    // tinytable cell content after
[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],

    // tinytable footer after

    table.footer(
      repeat: false,
      // tinytable notes after
    table.cell(align: left, colspan: 5, smallcaps([Source: Henderson and Velleman (1981)])),
    ),
    

  ) // end table

  ]
  })

  // tinytable align-figure after

] // end block
) // end figure 
