Render acknowledgement environments as a heading

The acks, acknowledgments and acknowledgements environments were reported as
unsupported. A body pass now rewrites their \begin into an unnumbered
"Acknowledgments" \section* and drops the matching \end, and the environment
names count as supported so the preview warning stays empty.

A test wraps "Thanks." in \begin{acks}, then checks the heading and body text
render with no warnings and no leaked environment names.
This commit is contained in:
gloamxun committed 2026-10-27 02:24:23 +08:00
1 parent 0a4bc0ade5
commit 723b9e220f
4 files changed
+46 -6

No files matched your search

+40
View File
@@ -6395,6 +6395,7 @@ fn geometry_and_body_with_images(
.replace("\\makeatother", "");
body = expand_environments(&body, &geometry.environments);
body = expand_keywords(&body);
body = expand_acks(&body);
body = drop_bibliography_commands(&body);
let savebox_base = geometry.base_font_size.unwrap_or(655_360);
body = expand_saveboxes(
@@ -6513,6 +6514,19 @@ fn drop_bibliography_commands(body: &str) -> String {
/// Turn `\keywords{...}` into a plain "Keywords: ..." paragraph. Class files
/// implement it with \addvspace and \rightskip machinery that route-D does not
/// model yet.
/// Render the common acknowledgement environments as an unnumbered heading.
fn expand_acks(body: &str) -> String {
let mut out = body.to_string();
for name in ["acks", "acknowledgments", "acknowledgements"] {
out = out.replace(
&format!("\\begin{{{name}}}"),
"\\section*{Acknowledgments}\n",
);
out = out.replace(&format!("\\end{{{name}}}"), "");
}
out
}
fn expand_keywords(body: &str) -> String {
let mut out = String::with_capacity(body.len());
let mut rest = body;
@@ -13242,6 +13256,9 @@ const SUPPORTED_ENVIRONMENTS: &[&str] = &[
"flushleft",
"flushright",
"minipage",
"acks",
"acknowledgments",
"acknowledgements",
"verbatim",
"tabular",
"longtable",
@@ -16608,6 +16625,29 @@ A definition.
assert!(!svg.contains("numberwithin"), "{svg}");
}
#[test]
fn acknowledgement_environments_render_a_heading() {
let source = "\\documentclass{article}\n\\begin{document}\n\nBody.\n\n\\begin{acks}\nThanks.\n\\end{acks}\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 svg = value["pages"][0]["svg"].as_str().expect("svg");
let text: String = svg
.split('>')
.filter_map(|piece| piece.split('<').next())
.collect::<String>()
.split_whitespace()
.collect();
assert!(text.contains("Acknowledgments"), "{text}");
assert!(text.contains("Thanks."), "{text}");
assert!(!text.contains("acks"), "{text}");
}
#[test]
fn column_and_double_page_commands_are_handled() {
let source = "\\documentclass{article}\n\\begin{document}\n\n\\onecolumn\n\nFirst.\n\n\\cleardoublepage\n\nSecond.\n\n\\twocolumn\n\nThird.\n\n\\end{document}\n";