Is it possible to style table cells on the diagonal?

While this works:

#show table.cell.where(x: 0): strong

this appearently doesn’t:

#show table.cell.where(x: y): strong

but would be sufficient to select any cell where x and y coordinates equal. Is there another approach for styling any diagonal cell (or generally, cells matching a more complex selection pattern)?

Instead of show-set it should be possible to use a full show rule (even if they are less flexible and harder to override or make composable rule sets with - the reason that they are less often seen.)

#show table.cell: it => {
  if it.x == it.y { strong(it) } else { it }
}
2 Likes

It’s not possible with a show rule selector. Related: Select outline target with an array of allowed targets or a more flexible selector · Issue #6099 · typst/typst · GitHub.

I agree that the only solution would be to use Is it possible to style table cells on the diagonal? - #2 by bluss, although I would write it in one line:

#show table.cell: it => if it.x == it.y { strong(it) } else { it }

This should probably be scoped if not all tables need this, which will resolve the issue with not being able to undo this.

1 Like

For completeness: styling of the cell itself (rather than its content) can be done by passing a function as property value:

#table(
  fill: (x, y) => if x == y { red } else { white },
  columns: 4,
  ..(none,)*16,
)

Same thing globally with a set rule:

#set table(fill: (x, y) => if x == y { red } else { white })
#table(
  columns: 4,
  ..(none,)*16,
)

image

2 Likes

Thanks to you all, good to know what options are available!