How can I iterate over all cells of a table in a show rule?

Thank you very much again!

What I would like to achieve is described here:

The answer given there is interesting, but does not solve the problem. That’s my fault; I surely should have attached more screenshots. So here we go:

I would like to wrap the text in the first header cell of a table dependent on how much horizontal space the other cells in the first column take, as shown in the following pictures.

In the following first picture, the first header cell contains the two words “Headerword1” and “Headerword2” on one line. This makes sense because the text in the first data cell takes more horizontal space than the two words in the first header cell together, and because the text in the first data cell cannot be wrapped because there is no word boundary.

Screenshot 1

In the following second picture, the situation is different. The text in the first data cell takes much less horizontal space than the two words in the first header cell. Actually, it even takes less horizontal space than each single word in the first header cell. Under such circumstances, I would like the text in the first header cell to be line-wrapped.

[ Side note: The line break between “Headerword1” and “Headerword2” in the second picture has been inserted manually to demonstrate the goal. ]

In that sense, I would like to make the first column as small as possible. In other words, the second column should be as wide as possible.

This is easy in HTML, but I couldn’t find an easy way to achieve it in typst. To solve the problem, we eventually could do the following in a table #show rule:

  • Iterate over all cells in the table
  • For each cell, check if it is in column 1
  • If yes, split the text in the cell at word boundaries (e.g., space characters)
  • For each single word, compute the horizontal space it takes
  • Remember the maximum horizontal extent that has been encountered so far
  • Finally, set the first element of the columns: array to that maximum

Leaving aside the gory details of the implementation, this would make the table behave as desired, wouldn’t it?

Then it would be convenient to access the column number of a cell in in an easy way, because we want to include only cells in column 1 into the calculations.

Alternatively (as you have proposed), we could note the number of columns, increase a counter whenever we find a cell, and use these two values in a modulo calculation to determine if the cell is in the first column. This should work under the condition that we encounter the cells in the expected, natural order when we walk down the tree of children.

I am also aware that a full implementation of the desired feature probably will take very high effort, because of situations like cells in cells, block / box content besides text in header cells (what rule for line breaks would then apply?), and so on.

However, for now, I am seeking a solution for simple cases (nothing else than text in cell bodies).

Best regards, and thanks again!