cetz icon indicating copy to clipboard operation
cetz copied to clipboard

Scope inside Group does not properly leak its anchors

Open lordkekz opened this issue 5 months ago • 0 comments

Problem

When a scope is nested inside a group, anchors defined inside the scope are only visible in (1) the scope itself and (2) the surrounding group. Unlinke anchors defined directly inside the group, the anchors from the scope are not visible outside the group. Nesting Groups does not appear to have the same problem.

Expected Behavior

The anchors which are defined in the scope inside the group should be visible outside the group through the groups namespace, just like anchors which are directly defined in the group.

Example Code

#import "@preview/cetz:0.4.1"
#import cetz.draw: *

#cetz.canvas({
    // Anchors defined in scopes "leak" into root level
    anchor("a-outside", (0,4*1))
    scope({
      anchor("a-in-scope", (0,4*2))
      circle("a-in-scope", fill: blue, radius: 1cm)
    })
    circle("a-outside", fill: red, radius: 1cm)
    circle("a-in-scope", fill: black, radius: .5cm)

    // Anchors defined in groups are prefixed
    group(
      name: "g1",
      {
        anchor("a-in-group", (0,4*3))
        circle("a-in-group", fill: green, radius: 1cm)
      },
    )
    circle("g1.a-in-group", fill: black, radius: .5cm)

    // Anchors defined in scopes inside groups are not properly resolved.
    // I would have expected it to "leak" into the group's namespace.
    group(
      name: "g2",
      {
        scope({
          anchor("a-in-scope-in-group", (0,4*3))
          circle("a-in-scope-in-group", fill: green, radius: 1cm)
        })
        // This works
        circle("a-in-scope-in-group", fill: black, radius: .75cm)

        group(
          name: "g-in-g2",
          {
            anchor("a-in-g-in-g2", (0, 4*4))
            circle("a-in-g-in-g2", fill: green, radius: 1cm)
          },
        )
        // This works
        circle("g-in-g2.a-in-g-in-g2", fill: black, radius: .75cm)
      },
    )
    // error: panicked with: "Anchor 'a-in-scope-in-group' not in anchors (\"center\",) for element 'g2'"
    circle("g2.a-in-scope-in-group", fill: red, radius: .5cm)
    // error: panicked with: "Anchor 'a-in-scope-in-group' not in anchors (\"center\",) for element 'g2'"
    circle((name: "g2", anchor: "a-in-scope-in-group"), fill: red, radius: .5cm)

    // This works:
    circle("g2.g-in-g2.a-in-g-in-g2", fill: red, radius: .5cm)
    // This works:
    circle((name: "g2", anchor: ("g-in-g2", "a-in-g-in-g2")), fill: red, radius: .5cm)
})

I'm using Typst 1.13.1 and CeTZ 0.4.1.

lordkekz avatar Aug 18 '25 17:08 lordkekz