Consume multicols wrappers

A multicols environment was unsupported, so a template wrapping its body in
\begin{multicols}{2} leaked the wrapper and its count. A body pass now strips the
begin form (optional arguments and the column count) and the matching end, and
marks both environment names as supported; the body lays out at the full
single-column measure because the engine has no column balancing.

A preview test wraps two paragraphs in multicols{2} and checks both render with
no warnings and no leaked command.
This commit is contained in:
gloamxun committed 2026-10-27 03:52:30 +08:00
1 parent 59ba5ac247
commit f71a12573e
4 files changed
+71 -6

No files matched your search

+65
View File
@@ -6422,6 +6422,7 @@ fn geometry_and_body_with_images(
body = expand_keywords(&body);
body = expand_acks(&body);
body = expand_keywords_environment(&body);
body = strip_multicols(&body);
body = body
.replace("\\begin{appendices}", "\n\n\\appendix\n\n")
.replace("\\end{appendices}", "\n\n");
@@ -10326,6 +10327,41 @@ fn optional_group(text: &str) -> Option<(String, usize)> {
Some((text[..end].to_string(), end + 1))
}
/// Drop a `multicols` wrapper: the engine lays out one column, so the body is
/// kept at the full measure.
fn strip_multicols(body: &str) -> String {
let mut output = body
.replace("\\end{multicols*}", "")
.replace("\\end{multicols}", "");
for command in ["\\begin{multicols*}", "\\begin{multicols}"] {
loop {
let Some(index) = output.find(command) else {
break;
};
let bytes = output.as_bytes();
let mut cursor = index + command.len();
while cursor < bytes.len() && bytes[cursor].is_ascii_whitespace() {
cursor += 1;
}
if cursor < bytes.len() && bytes[cursor] == b'[' {
if let Some(close) = output[cursor..].find(']') {
cursor += close + 1;
}
}
while cursor < bytes.len() && bytes[cursor].is_ascii_whitespace() {
cursor += 1;
}
if cursor < bytes.len() && bytes[cursor] == b'{' {
if let Some(close) = output[cursor..].find('}') {
cursor += close + 1;
}
}
output.replace_range(index..cursor, "\n\n");
}
}
output
}
/// Turn a `keywords` environment into a bold "Keywords:" lead-in, joining its
/// `\sep`-separated entries with commas.
fn expand_keywords_environment(body: &str) -> String {
@@ -14021,6 +14057,8 @@ const SUPPORTED_ENVIRONMENTS: &[&str] = &[
"minipage",
"acks",
"keywords",
"multicols",
"multicols*",
"appendices",
"acknowledgments",
"acknowledgements",
@@ -17443,6 +17481,33 @@ A definition.
assert!(text.contains("D"), "{text}");
}
#[test]
fn multicols_wrapper_is_consumed() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{multicols}{2}\nAlpha\n\nBeta\n\\end{multicols}\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("AlphaBeta"), "{text}");
assert!(!json.contains("multicols"), "{json}");
}
#[test]
fn keywords_environment_renders_a_lead_in() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\begin{keywords}\nalpha\\sep beta\n\\end{keywords}\n\n\\end{document}\n";