vincentarelbundock / tinytable

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

Typst: center tables by default #248

Closed vincentarelbundock closed 6 months ago

vincentarelbundock commented 6 months ago

maybe it would be nice to center align the table, as with kable?

Originally posted by @aghaynes in https://github.com/vincentarelbundock/tinytable/issues/247#issuecomment-2115098640

vincentarelbundock commented 6 months ago

LaTeX does centering by default, so it would be nice to be consistent and center Typst tables by default too.

What's the appropriate syntax? Should go in here: https://github.com/vincentarelbundock/tinytable/blob/main/inst/templates/typst.typ

aghaynes commented 6 months ago
#align(center,
[
#let nhead = 1;
....
]
)

This would conflict with #250 though... there i replace out[1] (#[) with [ and prepend the rotate. If you switch to center aligned, I suppose you'd need to wrap the rotate in align instead. Or maybe there's a more elegant way (something along the lines of gsub("#", "#rotate(-90deg,\\n[", out[1]))

Ultimately it would be something like this I suppose (for a rotated table)

#align(center,
rotate(-90deg, reflow: true,
[
#let nhead = 1;
...
]
)
)

or like this for a portrait one

#align(center,
[
#let nhead = 1;
...
]
)

Having written it like that, an easier solution for the rotated table presents itself, assuming you go with center aligned tables. If the template.typ file has a blank line in position 2, it could just be replaced with the rotate statement, and an extra closing bracket added to the end of the vector.

Shall I add it to #250 like that? Or do you want the alignment to be optional?

vincentarelbundock commented 6 months ago

Thanks for the suggestion @aghaynes This was a good idea. Should be merged in Github dev version now.

aghaynes commented 6 months ago

This unfortunately doesnt seem to have worked... tables are still left aligned... image image

aghaynes commented 6 months ago

i think its interacting with the quarto stuff again... removing the tbl-cap and label seems to center align it... this is frustrating...

vincentarelbundock commented 6 months ago

Weird. This works on my computer:

---
format: typst
---

```{r}
packageVersion("tinytable")

library(tinytable)
tt(iris[1:3, 1:3], caption = "blah blah blah")
#| tbl-cap: blah blah blah
#| label: tbl-blah

library(tinytable)
tt(iris[1:3, 1:3], caption = "blah blah blah")


![image](https://github.com/vincentarelbundock/tinytable/assets/987057/e0a9f79a-7323-4c05-a485-97e8e3f9cef3)
aghaynes commented 6 months ago

that does indeed work for me too... i'm sure i tested it like this though.

Maybe it's the template i'm using then... that same code within the template i'm using is left aligned... i'll investigate...

aghaynes commented 6 months ago

it's the echo option!

---
format: typst
execute:
  echo: false
---

```{r}
packageVersion("tinytable")

library(tinytable)
tt(iris[1:3, 1:3], caption = "blah blah blah")
#| tbl-cap: blah blah blah
#| label: tbl-blah

library(tinytable)
tt(iris[1:3, 1:3], caption = "blah blah blah")
#| tbl-cap: blah blah blah
#| label: tbl-blah2
#| echo: true

library(tinytable)
tt(iris[1:3, 1:3], caption = "blah blah blah")

![image](https://github.com/vincentarelbundock/tinytable/assets/6594028/75ce36f9-dbdc-4972-a4f7-8f7bcafdc26d)
vincentarelbundock commented 6 months ago

I have no idea why this would change things. Maybe you can inspect the raw .typ file.

aghaynes commented 6 months ago

no idea why, but adding an empty r block into the .typ does the trick (but it also add the grey box to the PDF)

#block[
```r

figure([


The only difference between the code for these two tables is that r block (it's copy/pasted .typ code, not recompiled through knitr, only typst)
![image](https://github.com/vincentarelbundock/tinytable/assets/6594028/0e68b040-0e49-4837-a293-1592ae0326c2)