Expand multirow cells to their text

This commit is contained in:
gloamxun committed 2026-10-26 01:33:51 +08:00
1 parent 20a2b1fa1f
commit f98964f6b6
4 files changed
+65 -7

No files matched your search

+60 -2
View File
@@ -1101,6 +1101,43 @@ fn hypersetup_values(line: &str) -> Vec<(String, String)> {
.collect()
}
/// Expands `\multirow{n}{width}{text}` cells to their text. The span is not
/// vertical-centre, so the text stays on its first row; the command itself
/// must not leak into the table.
fn expand_multirow(source: &str) -> String {
let mut rest = source;
let mut out = String::new();
while let Some(index) = rest.find(r"\multirow") {
out.push_str(&rest[..index]);
let after = rest[index + r"\multirow".len()..].trim_start();
let after = if let Some(inner) = after.strip_prefix('[') {
match inner.find(']') {
Some(end) => inner[end + 1..].trim_start(),
None => {
out.push_str(&rest[index..]);
return out;
}
}
} else {
after
};
let mut cursor = after;
let mut pieces = Vec::new();
for _ in 0..3 {
let Some((body, consumed)) = braced_group(cursor) else {
out.push_str(&rest[index..]);
return out;
};
pieces.push(body.trim().to_string());
cursor = &cursor[consumed..];
}
out.push_str(&pieces[2]);
rest = cursor;
}
out.push_str(rest);
out
}
fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
let mut geometry = PageGeometry::default();
let mut body = String::new();
@@ -1110,7 +1147,9 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
if let Some((_, text, environment, _)) = tabular.as_mut() {
if trimmed == format!("\\end{{{environment}}}") {
let (template, text, _, target) = tabular.take().unwrap();
geometry.tabular.push((template, text, target));
geometry
.tabular
.push((template, expand_multirow(&text), target));
} else {
if !text.is_empty() {
text.push('\n');
@@ -1533,7 +1572,9 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
}
}
if let Some((template, text, _, target)) = tabular {
geometry.tabular.push((template, text, target));
geometry
.tabular
.push((template, expand_multirow(&text), target));
}
(body, geometry)
}
@@ -7738,6 +7779,7 @@ Body $\operatorname{rank}(A)$ and $\Tr(B)$.";
assert!(!json.contains("substack"));
}
#[test]
fn matrix_environments_paint_their_delimiters() {
let json = preview_json("\\[\\begin{bmatrix} a & b \\\\ c & d \\end{bmatrix}\\]")
.expect("matrix preview");
@@ -7813,6 +7855,7 @@ a & b
assert!(text.contains("/Count 3"));
}
#[test]
fn hypersetup_writes_the_pdf_info_dictionary() {
let pdf =
pdf_bytes("\\hypersetup{pdftitle={My Title},pdfauthor={Ada}}\n\nBody.").expect("pdf");
@@ -7821,6 +7864,7 @@ a & b
assert!(text.contains("/Author (Ada)"));
}
#[test]
fn pageref_reports_the_page_of_its_heading() {
let source = "\\pagestyle{empty}\n\n\\section{One}\n\nFirst page.\n\n\\newpage\n\n\\section{Two}\\label{sec:two}\n\nSee page \\pageref{sec:two}.";
let json = preview_json(source).expect("preview");
@@ -8217,6 +8261,20 @@ See \ref{fig:a} and \ref{tab:b}.";
assert!(!body.contains("tabular"), "environment leaked: {body}");
}
#[test]
fn multirow_cells_keep_their_text() {
let source =
"\\begin{tabular}{ll}\n\\multirow{2}{*}{Both} & a \\\\\n & b\n\\end{tabular}\n\nTail.";
let (_, geometry) = geometry_and_body(source);
assert_eq!(geometry.tabular.len(), 1);
let (_, text, _) = &geometry.tabular[0];
assert!(text.contains("Both & a"), "{text}");
assert!(!text.contains("multirow"));
let json = preview_json(source).expect("preview");
assert!(json.contains(">Both</text>"));
assert!(!json.contains("multirow"));
}
#[test]
fn tabularx_fills_its_target_width() {
let source =