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

`figure` Environment for Quarto + Typst #337

Closed kazuyanagimoto closed 2 months ago

kazuyanagimoto commented 2 months ago

Hi, I am using this package for Quarto + Typst environment. I found that if the label is not specified in the Quarto chunk, the generated typst table is not in the #figure([]) environment. While this is not a problem in a regular Typst article, it gets an error in the Touying environment.

I am developing a slide template Quarto + Typst with Touying. In the demo slides, I manually add #figure([ and ]) before and after tinytable object in the code.

Is this non-figure output in Quarto + Typst an expected behavior? If so, is it possible to add an option to put the table in figure environment?

vincentarelbundock commented 2 months ago

Yes, it is an expected behavior. When users in Quarto use the tbl-cap or tbl-label chunk options, Quarto inserts the table in an environment, and there can be conflict with an existing environment. Therefore, to avoid duplication of environments and conflicts, tinytable does not create figures in Quarto, and recommends that users call the appropriate chunk options.

kazuyanagimoto commented 2 months ago

Thank you for your quick response! I understand it. But for the presentation, I would like to use tables without label or tbl-cap. I think captions are redundant in a presentation. Also, I would like to use style_tt() to highlight a table line by line, and it is time-consuming to re-label all tables just to highlight one line. If I put different labels on these tables, the table number will be counted up by highlighting.

So I expect that tinytable puts a table in the figure environment if label is not provided in the current Quarto chunk (possibly in R/finalize_typst.R?)

vincentarelbundock commented 2 months ago

Sounds reasonable.

Check out the vignette on how to define custom themes. You can apply those these automatically via a global option, and use that the structure to automatically add the figure environment "manually"

kazuyanagimoto commented 2 months ago

Thank you. I tried this

theme_slides <- function(x, ...) {

  fn <- function(table) {
    table@table_string <- paste0("#figure([\n", table@table_string, "\n])")
    return(table)
  }

  style_tt(x, finalize = fn)
}

options(tinytable_tt_theme = theme_slides)

However, it does not correctly work because while it adds the figure environment, it removes some table.hline and produces a table without top and bottom solid lines. It seems related to R/style_typst.R L159-L180, but I couldn't figure it out. Can I ask how to write such a theme?

vincentarelbundock commented 2 months ago

Does this work for you?

theme_slides <- function(x, ...) {
    fn <- function(table) {
        if (isTRUE(table@output == "typst")) {
          table@table_string <- paste0("#figure([\n", table@table_string, "\n])")
        }
        return(table)
    }
    x <- style_tt(x, finalize = fn)
    return(x)
}

tt(head(iris), theme = theme_slides)
kazuyanagimoto commented 2 months ago

No. This code removes some table.hline in the original table. You can see the difference by this example.

---
format: typst
---

```{r}
library(tinytable)
theme_slides <- function(x, ...) {
    fn <- function(table) {
        if (isTRUE(table@output == "typst")) {
          table@table_string <- paste0("#figure([\n", table@table_string, "\n])")
        }
        return(table)
    }
    x <- style_tt(x, finalize = fn)
    return(x)
}
tt(head(iris))
tt(head(iris), theme = theme_slides)
vincentarelbundock commented 2 months ago

Right, sorry, you also need to apply the default theme:

theme_slides <- function(x, ...) {
    fn <- function(table) {
        if (isTRUE(table@output == "typst")) {
          table@table_string <- paste0("#figure([\n", table@table_string, "\n])")
        }
        return(table)
    }
    x <- style_tt(x, finalize = fn)
    x <- theme_tt(x, theme = "default")
    return(x)
}
kazuyanagimoto commented 2 months ago

Now, it works! Thank you for your quick response!

vincentarelbundock commented 1 month ago

@kazuyanagimoto

FYI, I added a global option to do this more easily. It should work in the main branch on Github and will be part of the next release.

options(tinytable_quarto_figure = TRUE)
kazuyanagimoto commented 1 month ago

Thanks again for your quick and frequent development of the package!