cetz-package/cetz

Fit canvas to container

Closed this issue · 1 comments

tv42 commented

If I've understood correctly, cetz canvas "interior coordinate" bounds are always the bounds of whatever is drawn there, and setting a unit value for the canvas length argument sets the physical on-page size of whatever 1 means inside the canvas; actual size comes indirectly from multiplying this unit size by length.

To get a fixed size out of cetz, it seems I need to say length: 100%/canvas_interior_width. And I can't seem to be able to figure out the canvas interior width programmatically -- my leftmost and rightmost content are text labels, and I don't directly control their size!

Trying to pull of some measure-then-rescale trick with #layout(size => let m = measure(my-canvas)) leads to cannot expand into infinite width error from cetz canvas.

This makes it incredibly hard to consistently set diagrams to be e.g. 80% of the column width.

Is there any way to make this better?

Is this answered in #689?

Using Typst' measure and scale:

#let c = cetz.canvas({
  import cetz.draw: *

  circle((1,1), fill: red)
  content((5,0), [Some test text is here. \ What do you think?])
  rotate(280deg)
  rect((1,3), (rel: (2, 2)), fill: blue)
})

#place(context {
  let (width, height) = measure(c)
  scale(10cm / width * 100%, place(c))
})