How can I access which position in a grid a cell is?

I create a three column grid like this:

#grid(
  columns: (28%, 28%, 28%),
  rows: 17%,
  gutter: 1.3em,
  person(
    numarg: "1",
    arg: "A",
  ),
    person(
    numarg: "2",
    arg: "B",
  ),
    person(
    numarg: "3",
    arg: "C",
  ),
)

where in my case I call the function person more often than 3 times and with more different arguments that arg. The important thing is that there is one argument that is always just the number at which position this person gets called.

Now, if I exchange two persons, then I always have to also change the numbers to reflect their new position. The numarg argument feels redundant to me, because it is already completely fixed by the position, where in the grid it is.

Is there a way to circumvent the redundancy and program it, such that either I have to just change the number and then the position in the grid gets matched accordingly or just write the position and the numarg gets calculated from the position?

In this exact case I’d write it like this:

#grid(
  columns: (28%, 28%, 28%),
  rows: 17%,
  gutter: 1.3em,
  ..("A", "B", "C").enumerate(start: 1).map(((i, arg)) => person(numarg: str(i), arg: arg))
)

You could zip in more arrays for multiple arguments.

2 Likes

Thank you! I could adapt your code to do exactly what I want. :)

1 Like

You can’t directly access cell position within cell unless you use show grid.cell.where(x: 0, y: 0), but to set cell position you can use grid.cell.x and grid.cell.y.

Then why are you using it? Do you need to display cell order number?

Yes, I need it. It is for an election poster, where the number shows the place on the party list. (I will probably even post it in showcase, once it is public, if I remember to do so.)