How to dynamically apply stroke to right-most cells in table?

Hi! I’m attempting to create a general style for tables in a report I’m working on.
This is the code that I’m using:

#let tableReg = table.with(
  fill: (x, y) =>
    if (calc.odd(y)) { rgb("EAF2F5") } 
    else if (y > 1) { rgb("fff7ff") },
  stroke: (x,y) => (
    top: if (y == 0) {black},
  ),
)

This is what it looks like:

I want to have black borders only on the edges of the table.
My idea is that I can achieve this by setting the stroke: left and stroke: right of cells on the left-most column and right-most column respectively. And setting stroke: bottom on the final rows cells.

I know the left cells stroke can be set with left: if (x == 0) {black}. However, the bottom and rightmost cells can have different indices depending on the underlying tables layout, so I’m not sure how to set the stroke for them.
How do I apply stroke to right-most cells in table?

1 Like

Hi. See Tables – Typst Documentation guide that shows how to solve this exact problem.

1 Like

Thank you Andrew! Missed that section of the table documentation.

Solution:

#let tableReg = table.with(
  fill: (x, y) => if (calc.odd(y)) { rgb("EAF2F5") } else if (y > 1) { rgb("fff7ff") },
  stroke: (x,y) => (
    top: if (y < 2) {black} else {0pt},
    left: if (x ==0) {black} else {0pt},
    right: {black},
    bottom: {black},
  ),
)

#tableReg(
  columns: (0.25fr, 0.5fr, 1fr),
  ...
)

Explanation of the stroke rule:

right of a cell at x = 0 and left of a cell at x = 1, refer to the exact same edge, so by setting left to be 0pt the right stroke value black gets overwritten to 0pt as the cells are calculated from left → right.
The bottom stroke is rendered in the same way.

1 Like