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
733 stars 34 forks source link

`canvas()` does not pass the correct figure dimensions to `grid()` #591

Closed tapyu closed 1 month ago

tapyu commented 1 month ago

Consider this MWE:

#import "@preview/cetz:0.2.2": canvas, draw//, plot, decorations
#set page(width: auto, height: auto, margin: .5cm)

#let draw_layers(divisor) = {
  canvas({
    draw.merge-path(fill: red, {
      divisor
      draw.line((1, 1), (1, 2))
      draw.line((1, 2), (0, 2))
    },
    stroke: 0pt)

    divisor

    draw.merge-path(fill: blue, {
      divisor
      draw.line((1, 1), (1, 0))
      draw.line((1, 0), (0, 0))
    },
    stroke: 0pt)
  })
}

#grid(
    columns: (1fr, 1fr),
    rows: (1fr, 1fr),
    gutter: 90pt,
    draw_layers(draw.line((0, 1), (1, 1))),
    draw_layers(draw.line((0, 1), (1, 1))),
    draw_layers(draw.line((0, 1), (1, 1))),
    draw_layers(draw.line((0, 1), (1, 1))),
  )

It results in

image

Cetz doesn't seem to be able to correctly pass the image ratio (height x width) to grid() so that the correct page height and width can be automatically defined.

johannes-wolf commented 1 month ago

Does not work with native Typst drawing commands either (replace canvas(...) with rect(width: 1cm, height: 1cm)), so it seems to be a Typst bug.

tapyu commented 1 month ago

This issue was addressed in https://github.com/typst/typst/issues/4176 and is related to a conflict between auto and frac, thus having nothing to do it cetz