Compact consecutive Times heading glue

This commit is contained in:
gloamxun committed 2026-10-28 23:39:01 +08:00
1 parent 16a795bca2
commit 13b85b542a
7 files changed
+87 -8

No files matched your search

+63
View File
@@ -13921,6 +13921,25 @@ fn build_document_with_labels(
&geometry.bibliography,
geometry.ref_name.as_deref().unwrap_or("References"),
);
if geometry.roman_family.as_deref() == Some("ptm") {
// TeX puts a subsection directly under a section with only its normal
// heading glue. The route heading boxes are taller and the two
// \space_before values add up; compact only this consecutive-heading
// case, leaving body paragraphs untouched.
let mut previous_heading: Option<usize> = None;
for index in 0..paragraphs.len() {
if paragraphs[index].heading_level.is_some() {
if let Some(previous) = previous_heading {
paragraphs[previous].space_before =
(paragraphs[previous].space_before - 9 * SCALE).max(0);
paragraphs[index].space_before = 0;
}
previous_heading = Some(index);
} else if !paragraphs[index].text.trim().is_empty() {
previous_heading = None;
}
}
}
// Table-of-contents entries carry an invisible marker naming the heading
// whose page number they show; resolve it once the units exist.
let mut toc_targets: Vec<(usize, String)> = Vec::new();
@@ -15316,6 +15335,26 @@ mod tests {
out
}
#[test]
fn debug_arxiv_spacing() {
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
let template = std::fs::read_to_string(root.join("fixtures/arxiv-template/template.tex"))
.expect("template");
let sty =
std::fs::read_to_string(root.join("fixtures/arxiv-template/arxiv.sty")).expect("sty");
let assembled = template.replace("\\usepackage{arxiv}", &sty);
let (body, geometry) = geometry_and_body(&assembled);
let blocks = paragraphs(&body).expect("paragraphs");
let document = build_document(&blocks, &geometry);
for (index, unit) in document.units.iter().enumerate().skip(18).take(18) {
eprintln!(
"{index:3} space={} text={:?}",
unit.space_before,
&unit.source.replace('\n', " ")[..unit.source.len().min(35)]
);
}
}
#[test]
fn display_math_is_split_to_the_measure() {
let mut formula = String::from("$$");
@@ -20058,6 +20097,30 @@ left & right \\
assert_eq!(after.space_shrink, 5 * SCALE);
}
#[test]
fn consecutive_times_headings_use_tex_glue() {
let source = "\\renewcommand{\\rmdefault}{ptm}\n\\section{One}\n\\subsection{Two}\n\nBody.";
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body).expect("paragraphs");
let document = build_document(&paragraphs, &geometry);
let section = document
.units
.iter()
.find(|unit| unit.source == "1 One" || unit.source == "One")
.expect("section unit");
let subsection = document
.units
.iter()
.find(|unit| unit.source == "1.1 Two" || unit.source == "Two")
.expect("subsection unit");
assert_eq!(subsection.space_before, 0);
assert!(
section.space_before <= 3 * SCALE,
"section glue was not compacted: {}",
section.space_before
);
}
#[test]
fn times_display_math_uses_arxiv_skips() {
let source = "\\documentclass{article}\n\\renewcommand{\\rmdefault}{ptm}\n\\begin{document}\n\nBefore.\n\n\\[ x \\]\n\nAfter.\n\n\\end{document}\n";