Skip to content

Commit

Permalink
typst: group_tt
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 29, 2024
1 parent eb89d52 commit dfac54e
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 11 deletions.
6 changes: 6 additions & 0 deletions R/build_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,18 @@ build_tt <- function(x, output = NULL) {
l[[1]] <- quote(group_tabularray)
} else if (output == "markdown") {
l[[1]] <- quote(group_grid)
} else if (output == "typst") {
l[[1]] <- quote(group_typst)
}
out <- eval(l)
}
out <- meta(out, "output", output)

# style the table
if (output == "typst") {
# rules of precedence appear to differ
m$lazy_style <- rev(m$lazy_style)
}
if (output != "markdown") {
for (l in m$lazy_style) {
l[["x"]] <- out
Expand Down
32 changes: 32 additions & 0 deletions R/group_typst.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
group_typst <- function(x, i = NULL, j = NULL, ...) {
out <- x

if (!is.null(i)) {
out <- group_typst_row(out, i)
}

if (!is.null(j)) {
out <- group_typst_col(out, j)
}

return(out)
}


group_typst_col <- function(x, j, ...) {
m <- meta(x)
out <- x
miss <- as.list(setdiff(seq_len(m$ncols), unlist(j)))
miss <- stats::setNames(miss, rep(" ", length(miss)))
j <- c(j, miss)
max_col <- sapply(j, max)
idx <- order(max_col)
j <- j[idx]
lab <- names(j)
len <- sapply(j, length)
col <- sprintf("colspanx(%s, align: center)[%s],", len, lab)
col <- paste(col, collapse = "")
out <- typst_insert(out, col, type = "body")
return(out)
}

17 changes: 8 additions & 9 deletions R/style_typst.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ style_typst <- function(x,
colspan = NULL,
indent = 0,
...) {
if (meta(x, "output") != "typst") {
return(x)
}

if (meta(x, "output") != "typst") return(x)

out <- x

ival <- if (is.null(i)) seq_len(meta(x, "nrows")) else i
jval <- if (is.null(j)) seq_len(meta(x, "ncols")) else j
jval <- jval - 1

# only columns means we also want to style headers
if (is.null(i) && !is.null(j)) {
ival <- c(-1 * rev(seq_len(meta(x)$nhead) - 1), ival)
}

# 0- & header-indexing
jval <- jval - 1
ival <- ival - 1 + meta(x, "nhead")

if (isTRUE(grepl("^#", color))) color <- sprintf('rgb("%s")', color)
if (isTRUE(grepl("^#", background))) background <- sprintf('rgb("%s")', background)
if (isTRUE(grepl("^#", line_color))) line_color <- sprintf('rgb("%s")', line_color)
Expand Down Expand Up @@ -113,7 +115,7 @@ let j = (%s,);",
# Lines are not part of cellspec/rowspec/columnspec. Do this separately.
if (!is.null(line)) {
iline <- NULL
if (grepl("b", line)) iline <- c(iline, ival + 1)
if (grepl("b", line)) iline <- c(iline, ival + 1) # -1 for 0-indexing
if (grepl("t", line)) iline <- c(iline, ival)
iline <- unique(iline)
for (i in iline) {
Expand All @@ -136,15 +138,12 @@ let j = (%s,);",
"vlinex(x: %s, start: %s, end: %s, stroke: %sem + %s),",
j,
min(ival),
max(ival) + 1,
max(ival),
line_width,
line_color)
out <- typst_insert(out, tmp, type = "lines")
}

# if (grepl("t", line)) iline <- c(iline, ival + meta(x, "nhead"))
# if (grepl("l", line)) jline <- c(jline, jval)
# if (grepl("r", line)) jline <- c(jline, jval)
}


Expand Down
2 changes: 1 addition & 1 deletion R/tt_typst.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tt_typst <- function(x, caption, theme, width, notes, ...) {

# themes ("void" needs nothing)
if (isTRUE(theme %in% c("default", "striped"))) {
out <- style_typst(out, i = 0, line = "t", line_width = .1)
out <- style_typst(out, i = 1 - meta(out, "nhead"), line = "t", line_width = .1)
out <- style_typst(out, i = 0, line = "b", line_width = .05)
out <- style_typst(out, i = meta(out)$nrows, line = "b", line_width = .1)
} else if (isTRUE(theme == "grid")) {
Expand Down
2 changes: 1 addition & 1 deletion inst/templates/typst.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "@preview/tablex:0.0.8": tablex, hlinex, vlinex
#import "@preview/tablex:0.0.8": tablex, hlinex, vlinex, colspanx
#let nhead = $TINYTABLE_TYPST_NHEAD;
#let nrow = $TINYTABLE_TYPST_NROW;
#let ncol = $TINYTABLE_TYPST_NCOL;
Expand Down
57 changes: 57 additions & 0 deletions inst/tinytest/_tinysnapshot/typst-complicated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

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

#figure(
caption: [Hello World],
tablex(
columns: ncol,
header-rows: nhead,
align: left + horizon,
auto-lines: false,

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

map-cells: cell => {




let i = (0,1,2,3,4,5,);
let j = (0,);

if (i.contains(cell.y) and j.contains(cell.x)) { cell.content = emph(cell.content) };
if (i.contains(cell.y) and j.contains(cell.x)) { cell.content = { set text(red); cell.content } };
let i = (3,);
let j = (0,1,2,);

if (i.contains(cell.y) and j.contains(cell.x)) { cell.content = strong(cell.content) };
if (i.contains(cell.y) and j.contains(cell.x)) { cell.content = strike(cell.content) };
if (i.contains(cell.y) and j.contains(cell.x)) { cell.fill = black };
if (i.contains(cell.y) and j.contains(cell.x)) { cell.content = { set text(white); cell.content } };

if (cell.x == 0) { cell.align = left };
if (cell.x == 1) { cell.align = center };
if (cell.x == 2) { cell.align = center };
if (cell.x == 3) { cell.align = center };
if (cell.x == 4) { cell.align = right };
// tinytable cell style before
return cell;
},

// tinytable cell content after
colspanx(1, align: center)[ ],colspanx(2, align: center)[Group 2],colspanx(2, align: center)[Group 1],
[mpg], [cyl], [disp], [hp], [drat],
[21], [6], [160], [110], [3.9],
[21], [6], [160], [110], [3.9],
[22.8], [4], [108], [93], [3.85],
[21.4], [6], [258], [110], [3.08],


) // end tablex
) // end figure
18 changes: 18 additions & 0 deletions inst/tinytest/test-typst.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
source("helpers.R")
using("tinysnapshot")
options(tinytable_print_output = "typst")



# semi complicated
tab <- tt(mtcars[1:4, 1:5], caption = "Hello World") |>
group_tt(j = list("Group 1" = 4:5, "Group 2" = 2:3)) |>
style_tt(j = 1:5, align = "lcccr") |>
style_tt(i = 2, j = 1:3, strikeout = TRUE, bold = TRUE, background = "black", color = "white") |>
style_tt(j = 1, color = "red", italic = TRUE)
expect_snapshot_print(tab, label = "typst-complicated")




options(tinytable_print_output = NULL)

0 comments on commit dfac54e

Please sign in to comment.