diff --git a/NEWS.md b/NEWS.md index 712c248b..ecf67cec 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ * `format_tt(quarto = TRUE)` no longer appends spurious `NA` rows to tables with `group_tt(i = )` row groups, and no longer wraps the wrong cells. The Quarto wrapper indexed `@data_body` directly with row positions expressed in the rendered-table numbering, which includes group label rows; it now goes through the same `apply_format()` dispatcher as every other formatter. As a consequence, `quarto = TRUE` now also applies to column names, captions, notes, and group labels by default, matching the behavior of `markdown = TRUE`. Thanks to @andrewheiss for Issue #685. * Fix COPYRIGHTS file issues. +* Typst: in tables with `notes`, column widths now account for `style_tt(indent = )`, so indented cells no longer overflow into the next column. Spanning cells, such as `group_tt(i = )` labels, no longer set the width of every column they cover; instead, the covered columns widen proportionally only when a label does not fit. Thanks to @tomasrei for Issue #695. ## 0.18.0 diff --git a/R/typst_tt.R b/R/typst_tt.R index bf1c997e..1e7f4cdb 100644 --- a/R/typst_tt.R +++ b/R/typst_tt.R @@ -263,29 +263,45 @@ typst_widths <- function(x, out) { } # Proportional columns: measure natural column widths in Typst itself. - # Group headers and spanning cells are excluded from the measurement; they - # span several columns, so they do not pin down any single column's width. - coldata <- apply(x@data_body, 2, function(k) { - paste0("[", k, "]", collapse = ", ") - }) - if (!is.null(colnames(x)) && length(colnames(x)) > 0) { - coldata <- paste0("[", colnames(x), "], ", coldata) - } - coldata <- sprintf(" (%s),", coldata) + m <- typst_measure_cells(x) + coldata <- vapply(seq_len(ncol(m$cells)), function(j) { + k <- m$cells[!m$skip[, j], j] + if (length(k) == 0) k <- "[]" + sprintf(" (%s,),", paste(k, collapse = ", ")) + }, character(1)) coldata <- paste( c(" #let tinytable-coldata = (", coldata, " )", ""), collapse = "\n" ) + spans <- if (length(m$spans)) { + paste0(paste(m$spans, collapse = ", "), ",") + } else { + "" + } + spans <- sprintf(" #let tinytable-spans = (%s)\n", spans) # `grid` with the same inset as `table` measures the same natural width, # but is immune to the `show table.cell` styling rule, whose styles are # keyed by cell position and would misapply in a single-column layout. + # Spanning cells (e.g. `group_tt(i=)` labels) are measured separately: when + # one is wider than the columns it covers, those columns grow in proportion. total <- "calc.min(tinytable-naturals.sum(), size.width)" wrapper_open <- paste( c( coldata, + spans, " #context layout(size => {", + " let tinytable-measure = cell => measure(grid(columns: 1, inset: 5pt, cell)).width", " let tinytable-naturals = tinytable-coldata.map(col => measure(grid(columns: 1, inset: 5pt, ..col)).width)", + " for (a, b, cell) in tinytable-spans {", + " let w = tinytable-measure(cell)", + " let cur = tinytable-naturals.slice(a, b).sum()", + " if w > cur {", + " for k in range(a, b) {", + " tinytable-naturals.at(k) = if cur > 0pt { tinytable-naturals.at(k) * (w / cur) } else { w / (b - a) }", + " }", + " }", + " }", sprintf(" let tinytable-total = %s", total), " block(width: tinytable-total)[", "" @@ -304,6 +320,77 @@ typst_widths <- function(x, out) { out } +# Cells used to measure natural column widths (#695) +# +# Returns a list with +# - `cells`: character matrix (header row + body rows) of Typst content +# expressions, with each cell's `indent` applied as `pad()`, because the +# `show table.cell` rule that normally adds it is bypassed by the measurement. +# - `skip`: logical matrix flagging cells that must not set a single column's +# width: cells spanning several columns and cells hidden under a span. +# - `spans`: Typst `(start, end, content)` triples for multi-column cells. +typst_measure_cells <- function(x) { + cells <- as.matrix(x@data_body) + has_header <- !is.null(colnames(x)) && length(colnames(x)) > 0 + if (has_header) { + cells <- rbind(colnames(x), cells) + } + cells <- matrix(paste0("[", cells, "]"), ncol = ncol(x)) + nh <- as.integer(has_header) + nr <- nrow(cells) + nc <- ncol(cells) + skip <- matrix(FALSE, nr, nc) + + # @style_other is resolved before build_eval(): column- and row-wide + # settings are expanded to cells and later style_tt() calls win. + sty <- x@style_other + if (!all(c("i", "j") %in% names(sty))) { + return(list(cells = cells, skip = skip, spans = character(0))) + } + row_of <- function(i) i + nh + ok <- !is.na(sty$i) & !is.na(sty$j) & row_of(sty$i) >= 1 & + row_of(sty$i) <= nr & sty$j >= 1 & sty$j <= nc + sty <- sty[ok, , drop = FALSE] + + if ("indent" %in% names(sty)) { + ind <- sty[!is.na(sty$indent) & sty$indent > 0, , drop = FALSE] + ind <- ind[!duplicated(ind[, c("i", "j")], fromLast = TRUE), , drop = FALSE] + if (nrow(ind) > 0) { + idx <- cbind(row_of(ind$i), ind$j) + cells[idx] <- sprintf( + "pad(left: %s)%s", + vapply(ind$indent, format_markup_unit, character(1), "em"), + cells[idx] + ) + } + } + + spans <- character(0) + cs <- if ("colspan" %in% names(sty)) sty$colspan else rep(NA, nrow(sty)) + rs <- if ("rowspan" %in% names(sty)) sty$rowspan else rep(NA, nrow(sty)) + is_span <- (!is.na(cs) & cs > 1) | (!is.na(rs) & rs > 1) + sp <- sty[is_span, , drop = FALSE] + sp$cs <- ifelse(is.na(cs[is_span]), 1, cs[is_span]) + sp$rs <- ifelse(is.na(rs[is_span]), 1, rs[is_span]) + sp <- sp[!duplicated(sp[, c("i", "j", "cs", "rs")]), , drop = FALSE] + for (k in seq_len(nrow(sp))) { + r <- row_of(sp$i[k]) + j <- sp$j[k] + rows <- r:min(nr, r + sp$rs[k] - 1) + cols <- j:min(nc, j + sp$cs[k] - 1) + # hidden cells under the span + skip[rows, cols] <- TRUE + if (length(cols) > 1) { + spans <- c(spans, sprintf("(%d, %d, %s)", j - 1, max(cols), cells[r, j])) + } else { + # row-only span: still a single column's content + skip[r, j] <- FALSE + } + } + + list(cells = cells, skip = skip, spans = spans) +} + # Helper function to process notes typst_notes <- function(x, out) { if (length(x@notes) == 0) { diff --git a/inst/tinytest/_tinysnapshot/escape-issue150_caption_03.typ b/inst/tinytest/_tinysnapshot/escape-issue150_caption_03.typ index 35f35e2b..eaafcc56 100644 --- a/inst/tinytest/_tinysnapshot/escape-issue150_caption_03.typ +++ b/inst/tinytest/_tinysnapshot/escape-issue150_caption_03.typ @@ -42,14 +42,26 @@ block[ // start block // tinytable align-figure before #let tinytable-coldata = ( - ([blah\_blah\_underscore], [21.0#super[b]], [21.0], [22.8]), - ([dollar\$sign], [6#super[b]], [6], [4]), - ([percent%sign], [160], [160], [108]), - ([ampersand&sign], [110], [110], [93]), + ([blah\_blah\_underscore], [21.0#super[b]], [21.0], [22.8],), + ([dollar\$sign], [6#super[b]], [6], [4],), + ([percent%sign], [160], [160], [108],), + ([ampersand&sign], [110], [110], [93],), ) + #let tinytable-spans = () + #context layout(size => { + let tinytable-measure = cell => measure(grid(columns: 1, inset: 5pt, cell)).width let tinytable-naturals = tinytable-coldata.map(col => measure(grid(columns: 1, inset: 5pt, ..col)).width) + for (a, b, cell) in tinytable-spans { + let w = tinytable-measure(cell) + let cur = tinytable-naturals.slice(a, b).sum() + if w > cur { + for k in range(a, b) { + tinytable-naturals.at(k) = if cur > 0pt { tinytable-naturals.at(k) * (w / cur) } else { w / (b - a) } + } + } + } let tinytable-total = calc.min(tinytable-naturals.sum(), size.width) block(width: tinytable-total)[ diff --git a/inst/tinytest/_tinysnapshot/style-smallcap.typ b/inst/tinytest/_tinysnapshot/style-smallcap.typ index ffef2ab3..7a91ad63 100644 --- a/inst/tinytest/_tinysnapshot/style-smallcap.typ +++ b/inst/tinytest/_tinysnapshot/style-smallcap.typ @@ -42,15 +42,27 @@ block[ // start block // tinytable align-figure before #let tinytable-coldata = ( - ([Sepal.Length], [5.1], [4.9], [4.7], [4.6], [5.0], [5.4]), - ([Sepal.Width], [3.5], [3.0], [3.2], [3.1], [3.6], [3.9]), - ([Petal.Length], [1.4], [1.4], [1.3], [1.5], [1.4], [1.7]), - ([Petal.Width], [0.2], [0.2], [0.2], [0.2], [0.2], [0.4]), - ([Species], [setosa], [setosa], [setosa], [setosa], [setosa], [setosa]), + ([Sepal.Length], [5.1], [4.9], [4.7], [4.6], [5.0], [5.4],), + ([Sepal.Width], [3.5], [3.0], [3.2], [3.1], [3.6], [3.9],), + ([Petal.Length], [1.4], [1.4], [1.3], [1.5], [1.4], [1.7],), + ([Petal.Width], [0.2], [0.2], [0.2], [0.2], [0.2], [0.4],), + ([Species], [setosa], [setosa], [setosa], [setosa], [setosa], [setosa],), ) + #let tinytable-spans = () + #context layout(size => { + let tinytable-measure = cell => measure(grid(columns: 1, inset: 5pt, cell)).width let tinytable-naturals = tinytable-coldata.map(col => measure(grid(columns: 1, inset: 5pt, ..col)).width) + for (a, b, cell) in tinytable-spans { + let w = tinytable-measure(cell) + let cur = tinytable-naturals.slice(a, b).sum() + if w > cur { + for k in range(a, b) { + tinytable-naturals.at(k) = if cur > 0pt { tinytable-naturals.at(k) * (w / cur) } else { w / (b - a) } + } + } + } let tinytable-total = calc.min(tinytable-naturals.sum(), size.width) block(width: tinytable-total)[ diff --git a/inst/tinytest/_tinysnapshot/typst-issue456.typ b/inst/tinytest/_tinysnapshot/typst-issue456.typ index a0883131..f170e69f 100644 --- a/inst/tinytest/_tinysnapshot/typst-issue456.typ +++ b/inst/tinytest/_tinysnapshot/typst-issue456.typ @@ -40,15 +40,27 @@ block[ // start block // tinytable align-figure before #let tinytable-coldata = ( - ([mpg#super[a]], [21.0#super[a]], [21.0], [22.8], [21.4]), - ([cyl], [6], [6], [4], [6]), - ([disp], [160], [160], [108], [258]), - ([hp], [110], [110], [93], [110]), - ([drat], [3.90], [3.90], [3.85], [3.08]), + ([mpg#super[a]], [21.0#super[a]], [21.0], [22.8], [21.4],), + ([cyl], [6], [6], [4], [6],), + ([disp], [160], [160], [108], [258],), + ([hp], [110], [110], [93], [110],), + ([drat], [3.90], [3.90], [3.85], [3.08],), ) + #let tinytable-spans = () + #context layout(size => { + let tinytable-measure = cell => measure(grid(columns: 1, inset: 5pt, cell)).width let tinytable-naturals = tinytable-coldata.map(col => measure(grid(columns: 1, inset: 5pt, ..col)).width) + for (a, b, cell) in tinytable-spans { + let w = tinytable-measure(cell) + let cur = tinytable-naturals.slice(a, b).sum() + if w > cur { + for k in range(a, b) { + tinytable-naturals.at(k) = if cur > 0pt { tinytable-naturals.at(k) * (w / cur) } else { w / (b - a) } + } + } + } let tinytable-total = calc.min(tinytable-naturals.sum(), size.width) block(width: tinytable-total)[ diff --git a/inst/tinytest/test-typst.R b/inst/tinytest/test-typst.R index bd555c3c..728c059b 100644 --- a/inst/tinytest/test-typst.R +++ b/inst/tinytest/test-typst.R @@ -383,3 +383,38 @@ expect_false(grepl("tinytable-coldata", out, fixed = TRUE)) tab <- tt(dat669, width = c(0.1, 0.2, 0.2, 0.3)) out <- build_tt(tab, "typst")@table_string expect_true(grepl("columns: (10.00%, 20.00%, 20.00%, 30.00%),", out, fixed = TRUE)) + +# Issue #695: with notes, column widths ignored indent and counted spanning +# group labels in every column. +dat695 <- data.frame(Yr = c(2025L, 2026L, 2024L), N = c(1L, 2L, 3L)) +tab <- tt(dat695, notes = "n") |> + group_tt(i = list("New rules" = 1, "Old" = 3)) |> + style_tt(i = "~groupi", j = 1, indent = 2) +out <- build_tt(tab, "typst")@table_string +coldata <- sub("(?s).*#let tinytable-coldata = \\((.*?)\n \\)\n.*", "\\1", out, perl = TRUE) +# spanning group labels are not measured as single-column content... +expect_false(grepl("[New rules]", coldata, fixed = TRUE)) +expect_false(grepl("[Old]", coldata, fixed = TRUE)) +# ...but as spans covering all columns +expect_true(grepl("#let tinytable-spans = ((0, 2, [New rules]), (0, 2, [Old]),)", out, fixed = TRUE)) +# indented cells are measured with their indent +expect_true(grepl("([Yr], pad(left: 2em)[2025], pad(left: 2em)[2026], pad(left: 2em)[2024],),", out, fixed = TRUE)) +expect_true(grepl("([N], [1], [2], [3],),", out, fixed = TRUE)) +# matrix-inserted group rows hold per-column content and are still measured +tab <- tt(dat695, notes = "n") |> + group_tt(i = 2, j = matrix(c("wide-inserted-value", "q"), nrow = 1)) +out <- build_tt(tab, "typst")@table_string +expect_true(grepl("([Yr], [2025], [wide-inserted-value], [2026], [2024],),", out, fixed = TRUE)) +expect_true(grepl("#let tinytable-spans = ()", out, fixed = TRUE)) +# column-wide indent is measured; a later indent = 0 overrides an earlier one +tab <- tt(data.frame(a = c(1000, 2)), notes = "n") |> style_tt(j = 1, indent = 2) +out <- build_tt(tab, "typst")@table_string +expect_true(grepl("(pad(left: 2em)[a], pad(left: 2em)[1000], pad(left: 2em)[2],),", out, fixed = TRUE)) +tab <- tt(data.frame(a = c(1000, 2)), notes = "n") |> + style_tt(i = 1, j = 1, indent = 20) |> + style_tt(i = 1, j = 1, indent = 0) +out <- build_tt(tab, "typst")@table_string +expect_true(grepl("([a], [1000], [2],),", out, fixed = TRUE)) +# empty theme (no styles at all) with notes does not error +out <- build_tt(tt(data.frame(a = 1, b = 2), theme = "empty", notes = "n"), "typst")@table_string +expect_true(grepl("([a], [1],),", out, fixed = TRUE))