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 escape characters #232

Closed marcboschmatas closed 7 months ago

marcboschmatas commented 7 months ago

I noticed that the parametre escape does not escape the star signs (*) if the output is set to typst, which causes an error when compiling quarto documents to typst. See example. R code

t <- lm(mpg ~ cyl, data = mtcars) |> modelsummary::modelsummary(stars = TRUE, output = "tinytable")
library(tinytable)
t <- format_tt(t, escape = TRUE)
> t@output <- "typst"

Typst output

#import "@preview/tablex:0.0.8": tablex, hlinex, vlinex, colspanx
#let nhead = 1;
#let nrow = 11;
#let ncol = 2;

#figure(

  kind: "tinytable",
  supplement: none,
  tablex(
    columns: ncol,
    header-rows: nhead,
    align: left + horizon,
    auto-lines: false,

hlinex(y: 5, start: 0, end: 2, stroke: 0.05em + black),
hlinex(y: 0, start: 0, end: 2, stroke: 0.1em + black),
hlinex(y: 1, start: 0, end: 2, stroke: 0.05em + black),
hlinex(y: 12, start: 0, end: 2, stroke: 0.1em + black),
    // tinytable lines before

    map-cells: cell => {

if (cell.x == 1) { cell.align = center };

if (cell.x == 0) { cell.align = left };

      // tinytable cell style before
      return cell;
    },

    // tinytable cell content after
[ ], [(1)],
[(Intercept)], [37.885***],
[], [(2.074)],
[cyl], [-2.876***],
[], [(0.322)],
[Num.Obs.], [32],
[R2], [0.726],
[R2 Adj.], [0.717],
[AIC], [169.3],
[BIC], [173.7],
[Log.Lik.], [-81.653],
[RMSE], [3.10],

  ) // end tablex
) // end figure 

The problem is that the stars at lines 34 and 36 are not escaped, and should work like this

[(Intercept)], [37.885\*\*\*],

[cyl], [-2.876\*\*\*],

I can do some work on it if you like, I guess the relevant code is here https://github.com/vincentarelbundock/tinytable/blob/main/R/escape.R. The only problem is something similar to what happened with the void theme, that I don't know how to make sure only the relevant * characters are escaped, (for instance, if you want to make the R-squared row in bold). I'll play around but any suggestions welcome.

vincentarelbundock commented 7 months ago

Oh yeah, that would be great!

Yes, that's almost the right spot. There are already two escapes for < and > in Typst, which you can see here:

https://github.com/vincentarelbundock/tinytable/blob/main/R/escape.R#L36

All of the work should consist in identifying characters that need escaping, and adding obvious lines at the link above. Then, adding a bullet in NEWS.