The location in a context block is associated with the first element inside it. This usually makes sense, because when doing something like
Hello #context rect[#here().position()]
you don’t want the position to be the one right after “Hello”, but instead the position of the rectangle (which is a block and therefore not on the same line).
In the case here, the first element of the context block is a new page, and the position of the new page is naturally on that page, thus the counter in the original post is off by one. You could probably solve this simply by subtracting one from num initially so that the off-by-one error is compensated, but the other solutions given should also do the trick.
Two notes on the solution given by @aarnent:
-
The metadata isn’t actually necessary, as it’s enough to just label an empty content block like
[#[] <end>] -
The for loop only works like that for documents with up to 4 pages. To fix that, either use a while loop as in the original post, or do some modulus calculation as in
for _ in range(calc.rem-euclid(4 - num, 4)) { .. }