How can I set the alignment of table cells for certain rows and columns through a function?

To complement this answer, whenever you write an if without an else, there’s an implicit else { none }:

#repr(if 5 != 5 { "something" })  // none

// this is because the above is equivalent to
#repr(if 5 != 5 { "something" } else { none })  // none

So, the problem arises in this part of your code:

#set table(align: (x, y) => {
    if x in (0, 1, 2) and y > 0 {
      // Align vertically and horizontally
      center + horizon
    } // else { none } is implicit
  })

When the condition doesn’t match, you’re setting an alignment of none, which isn’t valid!

Instead, add an explicit else { auto } (auto is the default and means that alignment should be inherited from outside the table):

#set table(align: (x, y) => {
    if x in (0, 1, 2) and y > 0 {
      // Align vertically and horizontally
      center + horizon
    } else {
      // Use alignment from outside
      // (Alternatively: can use 'start + top' as default)
      auto
    }
  })
2 Likes