Jollywatt / typst-fletcher

Typst package for drawing diagrams with arrows, built on top of CeTZ.
MIT License
270 stars 6 forks source link

Add support for determining node position through labels and relative coordinates #37

Closed 3akev closed 3 weeks ago

3akev commented 1 month ago

It would be really nice if something like this was possible:

#diagram({
  node((0, 0), [root], name: <r>)
  node((<r>, "r"), [child1], name: <c1>)
  edge(<c1>, <r>)
  node((<c1>, "d"), [child2], <c2>)
})

Where, for example, node can take a label which determines a base position, and a string to move the node relative to the given position, similar to how edge works.

As shown in this example, this allows easily chaining and reusing nodes, in code mode, without having to define variables.

Jollywatt commented 1 month ago

Not an answer, just adding to the issue: it would be great if you could simply use cetz coordinates and anchors directly, as well as fletcher's "elastic coordinates". Cetz already has great support for relative, polar, tangent/perpendicular coordinates, etc.

I'm not sure how that could be implemented technically, though. @johannes-wolf may have ideas about how other packages could add custom coordinate systems to cetz.

johannes-wolf commented 1 month ago

@Jollywatt, you want to be able to register some kind of custom coordinate handler that translates a given coordinate to a vector? I guess we could add something like this. I think we should create a cetz ticket for this.

Jollywatt commented 3 weeks ago

As of v0.5.0, this will be possible with CeTZ-style relative coordinates:

#diagram({
  node((0, 0), [root], name: <r>)
  node((rel: (1, 0), to: <r>), [child1], name: <c1>)
  edge(<c1>, <r>)
  node((rel: (0, 1), to: <c1>), [child2], <c2>)
})