Skip to content

Commit

Permalink
typst midrule with group_tt()
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentarelbundock committed Jan 30, 2024
1 parent 75fcb40 commit 9d74b66
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
12 changes: 9 additions & 3 deletions R/group_typst.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ group_typst <- function(x, i = NULL, j = NULL, ...) {
# }

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

return(out)
}


group_typst_col <- function(x, j, ...) {
group_typst_col <- function(x, j, ihead, ...) {
m <- meta(x)
out <- x
miss <- as.list(setdiff(seq_len(m$ncols), unlist(j)))
Expand All @@ -27,6 +27,12 @@ group_typst_col <- function(x, j, ...) {
col <- sprintf("colspanx(%s, align: center)[%s],", len, lab)
col <- paste(col, collapse = "")
out <- typst_insert(out, col, type = "body")

# midrule
jrule <- lapply(names(j), function(n) if (trimws(n) != "") j[[n]])
jrule <- Filter(function(x) !is.null(x), jrule)
for (jr in jrule) {
out <- style_typst(out, i = ihead, j = jr, line = "b", line_width = .05, midrule = TRUE)
}
return(out)
}

2 changes: 1 addition & 1 deletion R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ print.tinytable <- function(x,
stop("here be dragons")
}

return(invisible(x))
return(invisible(out))
}

3 changes: 3 additions & 0 deletions R/save_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ save_tt <- function(x, output, overwrite = FALSE) {
} else if (identical(output, "latex")) {
out <- build_tt(x, output = "latex")
return(as.character(out))
} else if (identical(output, "typst")) {
out <- build_tt(x, output = "typst")
return(as.character(out))
}

x <- meta(x, "output_dir", dirname(output))
Expand Down
9 changes: 7 additions & 2 deletions R/style_typst.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ style_typst <- function(x,
line_width = .1,
colspan = NULL,
indent = 0,
midrule = FALSE, # undocumented, only used by `group_tt()`
...) {

if (meta(x, "output") != "typst") return(x)
Expand Down Expand Up @@ -119,8 +120,12 @@ let j = (%s,);",
if (grepl("t", line)) iline <- c(iline, ival)
iline <- unique(iline)
for (i in iline) {
tmp <- sprintf(
"hlinex(y: %s, start: %s, end: %s, stroke: %sem + %s),",
if (isTRUE(midrule)) {
tmp <- "hlinex(y: %s, start: %s, end: %s, stroke: %sem + %s, expand: -1.5pt),"
} else {
tmp <- "hlinex(y: %s, start: %s, end: %s, stroke: %sem + %s),"
}
tmp <- sprintf(tmp,
i,
min(jval),
max(jval) + 1,
Expand Down
1 change: 1 addition & 0 deletions inst/tinytest/_tinysnapshot/group_tt-3level_typ.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1] "#import \"@preview/tablex:0.0.8\": tablex, hlinex, vlinex, colspanx\n#let nhead = 4;\n#let nrow = 3;\n#let ncol = 5;\n\n#figure(\n \n tablex(\n columns: ncol,\n header-rows: nhead,\n align: left + horizon,\n auto-lines: false,\n\nhlinex(y: 0, start: 0, end: 5, stroke: 0.1em + black),\nhlinex(y: 4, start: 0, end: 5, stroke: 0.05em + black),\nhlinex(y: 7, start: 0, end: 5, stroke: 0.1em + black),\nhlinex(y: 3, start: 1, end: 3, stroke: 0.05em + black, expand: -1.5pt),\nhlinex(y: 3, start: 3, end: 5, stroke: 0.05em + black, expand: -1.5pt),\nhlinex(y: 2, start: 0, end: 2, stroke: 0.05em + black, expand: -1.5pt),\nhlinex(y: 2, start: 2, end: 5, stroke: 0.05em + black, expand: -1.5pt),\nhlinex(y: 1, start: 0, end: 3, stroke: 0.05em + black, expand: -1.5pt),\nhlinex(y: 1, start: 3, end: 4, stroke: 0.05em + black, expand: -1.5pt),\n // tinytable lines before\n\n map-cells: cell => {\n\n\n\n\n\n\n\n\n\n // tinytable cell style before\n return cell;\n },\n\n // tinytable cell content after\ncolspanx(3, align: center)[e],colspanx(1, align: center)[f],colspanx(1, align: center)[ ],\ncolspanx(2, align: center)[c],colspanx(3, align: center)[d],\ncolspanx(1, align: center)[ ],colspanx(2, align: center)[a],colspanx(2, align: center)[b],\n[mpg], [cyl], [disp], [hp], [drat],\n[21], [6], [160], [110], [3.9],\n[21], [6], [160], [110], [3.9],\n[22.8], [4], [108], [93], [3.85],\n\n\n ) // end tablex\n) // end figure"
19 changes: 10 additions & 9 deletions inst/tinytest/test-group_tt.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ expect_snapshot_print(tab, label = "group_tt-3level_tex")
options(tinytable_print_output = NULL)


# # 3 level: typst
# options(tinytable_print_output = "typst")
# x <- mtcars[1:3, 1:5]
# tab <- tt(x) |>
# group_tt(j = list("a" = 2:3, "b" = 4:5)) |>
# group_tt(j = list("c" = 1:2, "d" = 3:5)) |>
# group_tt(j = list("e" = 1:3, "f" = 4))
# expect_snapshot_print(tab, label = "group_tt-3level_typ")
# options(tinytable_print_output = NULL)
# 3 level: typst
x <- mtcars[1:3, 1:5]
tab <- tt(x) |>
group_tt(j = list("a" = 2:3, "b" = 4:5)) |>
group_tt(j = list("c" = 1:2, "d" = 3:5)) |>
group_tt(j = list("e" = 1:3, "f" = 4)) |>
save_tt(output = "typst")
expect_snapshot_print(tab, label = "group_tt-3level_typ")


0 comments on commit 9d74b66

Please sign in to comment.