Capture tabulars wrapped in resizebox

A \resizebox or \scalebox around a tabular left the wrapper in the body and the
tabular capture swallowed the caption and \end{table}, so scaled tables were
lost. A source pass now removes such wrappers when their content contains a
table, longtable or table environment (and clears a leading % comment), leaving
the tabular at top level where the existing capture sees it; wrappers around
ordinary text stay for the later text pass.

A preview test wraps a two-row tabular in \resizebox inside a table and checks
"Table 1: Resized" plus the cell text with no warnings.
This commit is contained in:
gloamxun committed 2026-10-27 03:05:03 +08:00
1 parent e8f7e37cf7
commit abf5e90578
4 files changed
+97 -6

No files matched your search

+91
View File
@@ -6368,6 +6368,7 @@ fn geometry_and_body_with_images(
// body before `\begin{document}` so its commands are consumed as setup
// instead of being typeset as text at the top of page one.
let source = lift_at_begin_document(source);
let source = unwrap_scaled_tables(&source);
let source = expand_valign(&source);
let source = canonicalize_at_catcodes(&source);
let source = collapse_definition_bodies(&source);
@@ -6456,6 +6457,68 @@ fn geometry_and_body_with_images(
(body, geometry)
}
/// Remove `\resizebox`/`\scalebox` wrappers whose content contains a table
/// environment, so the tabular capture sees the table directly. Wrappers
/// around ordinary text are left for the later text pass.
fn unwrap_scaled_tables(source: &str) -> String {
let mut output = String::new();
let mut rest = source;
loop {
let mut next: Option<(usize, &str)> = None;
for command in ["\\resizebox*", "\\resizebox", "\\scalebox"] {
if let Some(index) = rest.find(command) {
let better = next.is_none_or(|(known, _)| index < known);
if better {
next = Some((index, command));
}
}
}
let Some((index, command)) = next else {
output.push_str(rest);
break;
};
output.push_str(&rest[..index]);
let after = rest[index + command.len()..].trim_start();
let arguments = if command.starts_with("\\resizebox") {
2
} else {
1
};
let mut cursor = after;
let mut parsed = true;
for _ in 0..arguments {
if let Some((_, used)) = braced_group(cursor) {
cursor = cursor[used..].trim_start();
} else {
parsed = false;
break;
}
}
if !parsed {
output.push_str(command);
rest = &rest[index + command.len()..];
continue;
}
let Some((content, used)) = braced_group(cursor) else {
output.push_str(command);
rest = &rest[index + command.len()..];
continue;
};
if content.contains("\\begin{tabular")
|| content.contains("\\begin{longtable")
|| content.contains("\\begin{table")
{
let clean = content.trim_start().trim_start_matches('%').trim_start();
output.push_str(clean);
rest = &cursor[used..];
} else {
output.push_str(command);
rest = &rest[index + command.len()..];
}
}
output
}
/// Turn an explicit `\par` token into a paragraph break.
fn expand_par_breaks(body: &str) -> String {
let mut output = String::with_capacity(body.len());
@@ -16998,6 +17061,34 @@ A definition.
assert!(!text.contains("??"), "{text}");
}
#[test]
fn scaled_tables_render_their_content() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{table}\n\\resizebox{\\textwidth}{!}{%\n\\begin{tabular}{ll}\nA & B \\\\\nC & D \\\\\n\\end{tabular}}\n\\caption{Resized}\n\\end{table}\n\n\\end{document}\n";
let json = preview_json(source).expect("preview");
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
assert_eq!(
value["warnings"].as_array().map(Vec::len),
Some(0),
"{}",
value["warnings"]
);
let pages = value["pages"].as_array().expect("pages");
let mut text = String::new();
for page in pages {
if let Some(svg) = page["svg"].as_str() {
text.push_str(
&svg.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>(),
);
}
}
let text: String = text.split_whitespace().collect();
assert!(text.contains("Table1:Resized"), "{text}");
assert!(text.contains("A"), "{text}");
assert!(text.contains("D"), "{text}");
}
#[test]
fn scaled_box_wrappers_keep_their_content() {
let source = "\\documentclass{article}\n\\begin{document}\n\nA \\scalebox{0.5}{small} B \\resizebox{2cm}{!}{wide} C \\rotatebox{90}{turned} D \\raisebox{2pt}{lifted} E.\n\n\\end{document}\n";