How can I use fletcher alongside native cetz?

I took a look in the documentation, there’s a part called “Cetz integration” that shows the following code:

#diagram(
	node((0,1), $A$, stroke: 1pt, shape: fletcher.shapes.diamond),
	node((2,0), [Bézier], fill: purple.lighten(80%)),
	render: (grid, nodes, edges, options) => {
		// cetz is also exported as fletcher.cetz
		cetz.canvas({
			// this is the default code to render the diagram
			fletcher.draw-diagram(grid, nodes, edges, debug: options.debug)
			// retrieve node data by coordinates
			let n1 = fletcher.find-node-at(nodes, (0,1))
			let n2 = fletcher.find-node-at(nodes, (2,0))
			let out-angle = 45deg
			let in-angle = -110deg
			fletcher.get-node-anchor(n1, out-angle, p1 => {
				fletcher.get-node-anchor(n2, in-angle, p2 => {
					// make some control points
					let c1 = (to: p1, rel: (out-angle, 10mm))
					let c2 = (to: p2, rel: (in-angle, 20mm))
					cetz.draw.bezier(
						p1, p2, c1, c2,
						mark: (end: ">") // cetz-style mark
					)
				})
			})
		})
	}
)

But I still can’t figure out how to make this integration based on this, I want to use #line(…) and other cetz commands in the same environment as the fletcher diagrams.

This worked for me, for example:

#import "@preview/fletcher:0.5.8" as fletcher: * 

#diagram(
  node((0,1), $A$, stroke: 1pt, shape: fletcher.shapes.diamond),
  node((2,0), [Bézier], fill: purple.lighten(80%)),
  render: (grid, nodes, edges, options) => {
    // cetz is also exported as fletcher.cetz
    cetz.canvas({
      import cetz.draw: *
  
      // this is the default code to render the diagram
      fletcher.draw-diagram(grid, nodes, edges, debug: options.debug)
  
      line((0, 0), (1, 1))
    })
  }
)

If I had to guess (hint: if you tell us the error message(s) you get, we can often be more precise in our help), you were maybe missing the import line, or were using #line(...) instead of line(...). The # is needed to switch from markup to code mode, and inside the diagram you were already in code mode.

1 Like

Hello. Please use backticks for inline code snippets and update your post. Your code example also does not compile, which makes it harder on those who want to answer or use it. See https://sscce.org.

That’s it, thanks. This example works well.

Fletcher’s integration with CeTZ currently sucks, but I’m working on it.
It’s being rewritten to be more like CeTZ, so you will soon be able to use cetz.draw commands directly in a fletcher diagram(), and conversely use fletcher nodes and edges directly in a CeTZ canvas(). The render option will vanish completely.

You’ll also be able to style nodes and edges with cetz.draw.set-style(..) like other CeTZ objects.

Fletcher will basically be a layer on top of CeTZ which gives you more powerful arrow styles and optionally lets you use tabular layouts for convenience.

5 Likes