cetz-package / cetz

CeTZ: ein Typst Zeichenpaket - A library for drawing stuff with Typst.
https://cetz-package.github.io
GNU Lesser General Public License v3.0
809 stars 35 forks source link

Possibly a bug in cetz.annotate #550

Closed kdog3682 closed 5 months ago

kdog3682 commented 5 months ago

https://typst.app/project/wqLpO0mXFqs5_qbpCwk4iA

The circle becomes an ellipse due to the size being larger in the x.

#import "@preview/cetz:0.2.2"

#cetz.canvas({
  import cetz.plot
  import cetz.draw: *
  plot.plot(
    size: (-6, 3), {
    plot.add(domain: (-1, 1), (x) => x)
    plot.annotate({
        circle((0, 0)) 
        // displays as ellipse
    })
  })
})
johannes-wolf commented 5 months ago

This is actually expected behavior. The plot.annotate content is scaled via the axes of the annotation (you can set them via axes: ("x", "y")). If you want to draw annotations that do not use the plot's transformation, you have to use anchors and draw those elements after the plot.

kdog3682 commented 5 months ago

Understood.

But here is another version of it.

https://typst.app/project/wqLpO0mXFqs5_qbpCwk4iA

#import "@preview/cetz:0.2.2"

#cetz.canvas({
  import cetz.plot
  import cetz.draw: *
  plot.plot(
    size: (3, 3), 
    x-min: -3,
    x-max: 5, {
    plot.add(domain: (-3, 1), (x) => x)
    plot.annotate({
        circle((0, 0)) 
        // displays as ellipse
    })
  })
})

In this case, I feel like the annotation should automatically scale with the original plot.

johannes-wolf commented 5 months ago

Looks correct to me.

To draw outsides the plot environment and hence without its transformation use anchors:

#import "@preview/cetz:0.2.2"

#cetz.canvas({
  import cetz.plot
  import cetz.draw: *
  plot.plot(
    name: "plot",
    size: (8,8), 
    x-min: -3,
    x-max: 5, {
    plot.add(domain: (-3, 1), (x) => x)
    plot.add-anchor("zero", (0,0))
  })
  // Using anchors and drawing outsides the plot:
  circle("plot.zero")
})

There is a ticket open to implement drawing fixed size objects insides a plot: cetz-package/cetz-plot#4, but this is not trivial.