PgBiel / typst-tablex

More powerful and customizable tables in Typst
MIT License
370 stars 12 forks source link

Column sizes with mixed fractional and auto sizes don't fit the page #78

Closed manveru closed 7 months ago

manveru commented 9 months ago
#import "@preview/tablex:0.0.6": tablex, cellx

#tablex(
  columns: (1fr, 1fr, auto, auto, auto),
  [a], [b], [c], [d], [e],
  cellx(colspan: 5)[#lorem(5)],
  [a], [b], [c], [d], [e],
  cellx(colspan: 2)[#lorem(10)], none, none, none,
  [a], [b], [c], [d], [e],
  cellx(colspan: 3)[#lorem(15)], none, none,
)

Here's a rendered version of the difference the last row makes:

rendered

It seems like some combination of longer text with automatic sizing and fractional scaling may cause that, but I can't really figure out the logic behind this.

PgBiel commented 9 months ago

Related: #56

PgBiel commented 9 months ago

I believe this happens because auto columns are calculated before fr columns. As such, the last lorem paragraph expands the most it can, causing the auto column to take all available space, leaving 0 space for the 1fr columns. I'd like to improve the handling of 1fr columns in such cases, but this is complicated because, technical limitations aside, I'm not sure what the expected behavior should even be! :joy:

I guess one could consider splitting evenly between auto and 1fr in such cases, but that'll need further thought.

manveru commented 9 months ago

My intuition about this was that auto takes at least the width of the widest word within that column, while fractions take up the remaining space. But this is obviously very subjective. So maybe additional options would be a better way to express that?

I'm imagining something like:

I have no idea how complicated the calculations for each of these would be, I'm mostly trying to achieve the same effect as you'd get when auto-sizing a column in a spreadsheet application for now.

PgBiel commented 8 months ago

Thanks for the input. I believe your ideas are in the right direction.

I've found that tabularray[^1] has an option which would be the equivalent in tablex of having colspans not affect the sizes of auto columns at all (or rowspans not affect the sizes of auto rows). With such an option, you'd force colspans to fit into existing columns instead of causing them to expand, which would solve your problem, as the auto column it spans wouldn't expand unnecessarily. Of course, however, the colspan would have less space available, but that's a tradeoff one must consider. You'd choose what looks best in the end.

That could thus be added to tablex in an attempt to alleviate this kind of problem if needed.

Another solution, of course, is replacing the auto column by a fixed width column, but that's not always very convenient.

[^1]: Starting at p. 27 of tabularray docs (hspan etc. options): https://mirrors.ctan.org/macros/latex/contrib/tabularray/tabularray.pdf

PgBiel commented 7 months ago

You may now (once v0.0.8 releases) use fit-spans: (x: true) to solve your problem. It causes auto columns to ignore colspans:

#tablex(
    columns: (1fr, 1fr, auto, auto, auto),
    fit-spans: (x: true),
    [a], [b], [c], [d], [e],
    cellx(colspan: 5)[#lorem(5)],
    [a], [b], [c], [d], [e],
    cellx(colspan: 2)[#lorem(10)], none, none, none,
    [a], [b], [c], [d], [e],
    cellx(colspan: 3)[#lorem(15)], none, none,
)

image

Keeping this open as I might make tablex smarter in this regard in the future (specifically if the cell spans all fractional columns available).

PgBiel commented 7 months ago

This particular case (of a colspan over all fractional columns) is now automatically handled. You may opt into having auto columns ignore the sizes of colspans with fit-spans: (x: true).