cetz-package/cetz

"error: cannot divide by zero" when using a plot inside of tablex

PhilHelix opened this issue · 4 comments

First off, thank you for this project, it has helped me tons!
However I'm getting the following error whenever I want to plot a function inside of a #tablex(), however this doesn't happen when working with a stock #table().

The versions used were:

  • "@preview/cetz:0.1.2"
  • "@preview/tablex:0.0.6"
error: cannot divide by zero
    ┌─ @preview/cetz:0.1.2/src/util.typ:117:12
    │
117 │       float(num / ctx.length)
    │             ^^^^^^^^^^^^^^^^

help: error occurred in this call of function `resolve-number`
    ┌─ @preview/cetz:0.1.2/src/draw.typ:831:20
    │
831 │       let padding = util.resolve-number(ctx, style.padding)
    │                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this function call
   ┌─ @preview/cetz:0.1.2/src/canvas.typ:97:20
   │
97 │       coordinates = (element.transform-coordinates)(ctx, ..coordinates)
   │                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `process-element`
   ┌─ @preview/cetz:0.1.2/src/canvas.typ:58:14
   │
58 │       let r = process-element(child, ctx)
   │               ^^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `process-element`
   ┌─ @preview/cetz:0.1.2/src/canvas.typ:58:14
   │                                                                                                                                                                                                                                           ^58 │       let r = process-element(child, ctx)
   │               ^^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `process-element`
   ┌─ @preview/cetz:0.1.2/src/canvas.typ:58:14
   │
58 │       let r = process-element(child, ctx)
   │               ^^^^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `process-element`
    ┌─ @preview/cetz:0.1.2/src/canvas.typ:276:12
    │
276 │     let r = process-element(element, ctx)
    │             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A minimal (not) working example would be:

#import "@preview/cetz:0.1.2"
#import "@preview/tablex:0.0.6": tablex

#tablex(cetz.canvas({
  import cetz.plot
  plot.plot({
    plot.add(domain: (1, 2), x=>{ x })
  })
}))

Is this an error fixable on your end or is this more a tablex related issue?

I guess this is a bug in tablex @PgBiel? Or in Typst.

#tablex(
  style(st => {
    measure(line(length: 1cm), st)
  })
)

Gives (0pt, 0pt).

Fix is here: #346.

Thanks for the ping @johannes-wolf . I think I have an idea of what causes this. Can you (or the OP) open an issue on the tablex repository, linking to this one? Thanks!

To go into more detail:

I believe this is due to show rules on lines being used in tablex, so tablex could definitely be of blame here. However, it's still good for cetz to avoid this problem internally, as show rules by users themselves could inadvertently interfere with cetz in this same manner as well. So, in practice, it's sort of a bug on both sides. The proposed fix in the linked PR won't fix it for 100% of cases, as show rules on box could cause similar problems, but will probably fix it for the vast majority of cases (using line would be a much worse offender).

I'd leave that up to @johannes-wolf as I believe them to have a deeper understanding of the problem, in case any other questions arise.
Many thanks for the fix and hasty reply!

This is already fixed on the tablex side (in the 0.0.x branch), and will be released in tablex 0.0.7 (whenever that is, haha). 👍

In the meantime, you can use tablex.typ from that branch.