vincentarelbundock / tinytable

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

typst fails if there command characters #252

Closed aghaynes closed 4 months ago

aghaynes commented 4 months ago

In the case where pvalues are less than a given value, or where ranges are indicated using [, typst fails unless the < or [ are followed by a space. E.g., the following fails

```{r}
dat <- data.frame(x = 1:3, y = c("foo", "[6-7)", "<0.001"))
dat |> tinytable::tt()

[typst]: Compiling tmp.typ to tmp.pdf...error: unclosed delimiter ┌─ \?\E:\sandbox\tinytable\tmp.typ:573:1 │ 573 │ #[ │ ^

error: unclosed delimiter ┌─ \?\E:\sandbox\tinytable\tmp.typ:601:8 │ 601 │ #table( // tinytable table start │ ^

error: unclosed label ┌─ \?\E:\sandbox\tinytable\tmp.typ:624:6 │ 624 │ [3], [<0.001], │ ^^^^^^


One solution is to follow the characters with a space, which is ugly in many cases (who wants `[ 6-7)`?). Another is to escape the characters
dat <- data.frame(x = 1:3, y = c("foo", "\\[6-7)", "\\<0.001"))
dat |> tinytable::tt()

I was wondering whether it's worth automatically "sanitizing" the table content? I think it would have to come in tt_typst.R before 

body

body <- apply(x@table_dataframe, 2, function(k) paste0("[", k, "]"))



Something similar could also be done for tex, where there are a whole load of characters that cause problems (see e.g. https://github.com/CTU-Bern/btabler/blob/main/R/sf.R)

This is where typst is great though... you see immediately what the problem is... tex just gives some stupid error message...
aghaynes commented 4 months ago

I just fount the format_tt(escape = TRUE) argument. It doesnt help in this instance...

vincentarelbundock commented 4 months ago

Thanks for the report. And good catch. Yes, this escape is the right option to use, but you're right that square brackets should also be escaped.

I just merged a change to do this automatically.