vincentarelbundock / tinytable

Simple and Customizable Tables in `R`
https://vincentarelbundock.github.io/tinytable
GNU General Public License v3.0
209 stars 18 forks source link

Typst misalignment #323

Closed vincentarelbundock closed 1 month ago

vincentarelbundock commented 2 months ago

Lots of bugs in variations of this. Try groups with black background and orange text.

library(tinytable)
dat <- mtcars[1:9, 1:8]
tt(dat) |> 
  group_tt(
    i = list("I like (fake) hamburgers" = 3,
             "She prefers halloumi" = 4,
             "They love tofu" = 7),
    j = list("Hamburgers" = 1:3,
             "Halloumi" = 4:5,
             "Tofu" = 7)) |>
  style_tt(
    i = c(3, 5, 9),
    align = "c",
    background = "black",
    color = "orange") |>
  style_tt(i = -1, color = "orange")
danwwilson commented 2 months ago

I am not sure if this is along the same lines. I am making use of a quarto to render typst tables and was trying your example form the tutorial

dat <- mtcars[1:9, 1:8]
tt(dat) |> 
  group_tt(
    i = list("I like (fake) hamburgers" = 3,
             "She prefers halloumi" = 4,
             "They love tofu" = 7),
    j = list("Hamburgers" = 1:3,
             "Halloumi" = 4:5,
             "Tofu" = 7)) |>
  style_tt(
    i = c(3, 5, 9),
    align = "c",
    background = "teal",
    color = "white") |>
  style_tt(i = -1, color = "teal")

When it creates the PDF, the output looks very different to your web version. CleanShot 2024-09-06 at 13 20 44

There could be some template interactions here. If you have any tips that I can use to help debug/diagnose I'd be more than happy to help improve this.

vincentarelbundock commented 2 months ago

Thanks for the nudge @danwwilson ! Yep, that looks like exactly the same issue.

My guess is that the solution is to fix the row indices used in a few places in this file:

https://github.com/vincentarelbundock/tinytable/blob/main/R/style_typst.R

I actually don't expect this to be hard to fix---the code is pretty straightforward--- but in my brief attempt I couldn't quite get all the combinations working. The horizontal lines or group indentation would get shifted, for example.

Again, shouldn't be too hard, but I just have not found the time to look at this seriously yet. If you take a look and find something, that would be super useful!

arbelt commented 1 month ago

I think the issue is that the group rows are off when columns are also grouped — in the screenshot, the formatting is in the correct row number, but the group label rows are one lower than they ought to be. Without the j argument in the group, I was getting correct(ish) results.

It doesn't seem like nhead should be in the offset for the body of the typst table?

https://github.com/vincentarelbundock/tinytable/blob/2349573fa5614dc42e12a0b878c58c6c90002211/R/group_typst.R#L25

So I submitted a PR where I changed x@nhead to 1, but not sure if this might cause breakage in other scenarios.

Also I'm not sure this is limited to Typst — had similar issues with latex output.

vincentarelbundock commented 1 month ago

Thanks all. This should now be fixed in the main branch on Github. The fix will be part of the next CRAN release, coming in the coming weeks.