How to create a function that knows how many times it has been called, without returning content value?

Sounds very close to what was asked just a few hours ago: How can I save a coordinate in Cetz?.

Something like this?

#import "@preview/cetz:0.3.4"
#cetz.canvas({
  import cetz.draw: *
  let func(pos) = {
    set-ctx(ctx => ctx + (my-func: ctx.at("my-func", default: 0) + 1))
    circle(pos)
    get-ctx(ctx => content(())[#ctx.my-func])
  }
  func((0, 0))
  func((3, 0))
})

image

3 Likes