diff --git a/R/html_style.R b/R/html_style.R
index 532ce0c07..3b52d93de 100644
--- a/R/html_style.R
+++ b/R/html_style.R
@@ -201,6 +201,7 @@ setMethod(
# rowspan/colspan spans first
if (!is.null(other) && nrow(other) > 0 && any(c("rowspan", "colspan") %in% names(other))) {
+ listeners <- character(0)
for (row in seq_len(nrow(other))) {
rowspan <- if ("rowspan" %in% names(other) && !is.na(other$rowspan[row])) other$rowspan[row] else 1
colspan <- if ("colspan" %in% names(other) && !is.na(other$colspan[row])) other$colspan[row] else 1
@@ -216,14 +217,17 @@ setMethod(
rowspan,
colspan
)
- x@table_string <- lines_insert(
- x@table_string,
- listener,
- "tinytable span after",
- "after"
- )
+ listeners <- c(listeners, listener)
}
}
+ if (length(listeners) > 0) {
+ x@table_string <- lines_insert(
+ x@table_string,
+ paste(rev(listeners), collapse = "\n"),
+ "tinytable span after",
+ "after"
+ )
+ }
}
@@ -535,6 +539,8 @@ setMethod(
group_keys_sorted <- group_keys_sorted[order(sort_keys)]
}
+ style_arrays <- character(0)
+ css_entries <- character(0)
for (group_key in group_keys_sorted) {
group_data <- css_groups[[group_key]]
css_rule <- group_data$css_rule
@@ -561,12 +567,7 @@ setMethod(
"}, "
)
arr <- paste(arr, collapse = "")
- x@table_string <- lines_insert(
- x@table_string,
- arr,
- "tinytable style arrays after",
- "after"
- )
+ style_arrays <- c(style_arrays, arr)
# Generate CSS entry - scoped to table ID to prevent CSS cascade conflicts
table_id <- paste0("tinytable_", x@id)
@@ -574,9 +575,19 @@ setMethod(
" #%s td.%s, #%s th.%s { %s }",
table_id, id_css, table_id, id_css, css_rule
)
+ css_entries <- c(css_entries, entry)
+ }
+
+ if (length(style_arrays) > 0) {
+ x@table_string <- lines_insert(
+ x@table_string,
+ paste(rev(style_arrays), collapse = "\n"),
+ "tinytable style arrays after",
+ "after"
+ )
x@table_string <- lines_insert(
x@table_string,
- entry,
+ paste(rev(css_entries), collapse = "\n"),
"tinytable css entries after",
"after"
)
diff --git a/R/typst_style.R b/R/typst_style.R
index 53bcf98f0..ddfed2eee 100644
--- a/R/typst_style.R
+++ b/R/typst_style.R
@@ -187,11 +187,13 @@ typst_apply_styles <- function(x, rec) {
)
}
- # Insert style-array entries
- for (entry in rev(style_array_entries)) {
+ # Insert all style-array entries in one pass over the table string. The old
+ # loop inserted rev(style_array_entries) after the same marker, which
+ # produced style_array_entries order in the final output.
+ if (length(style_array_entries) > 0) {
x@table_string <- lines_insert(
x@table_string,
- paste0(" ", entry),
+ paste0(" ", style_array_entries, collapse = "\n"),
"tinytable cell style after",
"after"
)
@@ -318,10 +320,10 @@ typst_hlines <- function(x, lin) {
}
return(out)
})
- for (l in tmp) {
+ if (length(tmp) > 0) {
x@table_string <- lines_insert(
x@table_string,
- l,
+ paste(unlist(tmp, use.names = FALSE), collapse = "\n"),
"tinytable lines before",
"before"
)
@@ -483,10 +485,10 @@ typst_vlines <- function(x, lin) {
}
return(out)
})
- for (l in lin) {
+ if (length(lin) > 0) {
x@table_string <- lines_insert(
x@table_string,
- l,
+ paste(unlist(lin, use.names = FALSE), collapse = "\n"),
"tinytable lines before",
"before"
)
diff --git a/R/typst_tt.R b/R/typst_tt.R
index 8a0940b31..5861bdfa5 100644
--- a/R/typst_tt.R
+++ b/R/typst_tt.R
@@ -196,9 +196,20 @@ typst_notes <- function(x, out) {
notes <- sapply(notes, function(n) if (is.list(n)) n$text else n)
- for (k in seq_along(notes)) {
- note_text <- typst_note(notes[k], lab[k], ncol(x))
- out <- lines_insert(out, note_text, "tinytable notes after", "after")
+ note_text <- vapply(
+ seq_along(notes),
+ function(k) typst_note(notes[k], lab[k], ncol(x)),
+ character(1)
+ )
+ if (length(note_text) > 0) {
+ # Repeated insertion after one marker reverses the input, so reverse the
+ # batch to preserve the existing byte-for-byte output order.
+ out <- lines_insert(
+ out,
+ paste(rev(note_text), collapse = "\n"),
+ "tinytable notes after",
+ "after"
+ )
}
out