Reserve display skips around math

TeX surrounds each display with \abovedisplayskip above and \belowdisplayskip
below, but route-D only reserved the paragraph's own spacing, so displays sat
directly against the surrounding text. Parse the four registers (including the
short forms), default to the article-class 10/11/12pt values, and add them to
the math unit and the unit that follows it.

The glue parser now also accepts the class-file spelling
(`\abovedisplayskip 7\p@ \@plus 2\p@ \@minus 5\p@`), register copies such as
`\belowdisplayskip \abovedisplayskip`, `\z@`/`\p@` dimensions and
space-separated assignments, which clears the display-skip warnings the arXiv
template produced.

A page oracle sets a 45pt above skip and pins the display moving to the second
page exactly as pdfLaTeX lays it out; unit tests cover the defaults, overrides
and the class-file spelling.
This commit is contained in:
gloamxun committed 2026-10-26 22:49:41 +08:00
1 parent 8ac2e828df
commit 2f9b18c95c
4 files changed
+265 -21

No files matched your search

+169 -7
View File
@@ -231,6 +231,11 @@ struct PageGeometry {
max_depth: Option<i64>,
club_penalty: Option<i64>,
widow_penalty: Option<i64>,
/// `\abovedisplayskip` and friends, as width/stretch/shrink.
above_display_skip: Option<(i64, i64, i64)>,
below_display_skip: Option<(i64, i64, i64)>,
above_display_short_skip: Option<(i64, i64, i64)>,
below_display_short_skip: Option<(i64, i64, i64)>,
split_top_skip: Option<i64>,
split_max_depth: Option<i64>,
hyphen_penalty: Option<i64>,
@@ -2621,6 +2626,20 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.par_skip = Some(width);
geometry.par_skip_stretch = Some(stretch);
geometry.par_skip_shrink = Some(shrink);
} else if let Some(spacing) = glue_assignment(trimmed, "abovedisplayskip") {
geometry.above_display_skip = Some(spacing);
} else if glue_copy(trimmed, "belowdisplayskip", "abovedisplayskip") {
geometry.below_display_skip = geometry.above_display_skip;
} else if let Some(spacing) = glue_assignment(trimmed, "belowdisplayskip") {
geometry.below_display_skip = Some(spacing);
} else if let Some(spacing) = glue_assignment(trimmed, "abovedisplayshortskip") {
geometry.above_display_short_skip = Some(spacing);
} else if let Some(spacing) = glue_assignment(trimmed, "belowdisplayshortskip") {
geometry.below_display_short_skip = Some(spacing);
} else if let Some(spacing) = setlength_glue(trimmed, "abovedisplayskip") {
geometry.above_display_skip = Some(spacing);
} else if let Some(spacing) = setlength_glue(trimmed, "belowdisplayskip") {
geometry.below_display_skip = Some(spacing);
} else if let Some(spacing) = glue_assignment(trimmed, "itemsep") {
geometry.item_sep = Some(spacing);
} else if let Some(spacing) = glue_assignment(trimmed, "topsep") {
@@ -4628,21 +4647,49 @@ fn sfcode_assignment(line: &str) -> Option<(char, i64)> {
fn glue_assignment(line: &str, name: &str) -> Option<(i64, i64, i64)> {
let rest = line.strip_prefix('\\')?;
let rest = rest.strip_prefix(name)?;
let rest = rest.trim_start().strip_prefix('=')?;
let rest = rest.trim();
let rest = rest.trim_start();
// TeX accepts both `\parskip=6pt` and `\abovedisplayskip 10pt`; class
// files use the space-separated form.
let rest = rest.strip_prefix('=').unwrap_or(rest).trim();
let (width, rest) = split_glue_part(rest)?;
let (stretch, rest) = match rest.trim_start().strip_prefix("plus") {
let rest = rest.trim_start();
let (stretch, rest) = match rest
.strip_prefix("plus")
.or_else(|| rest.strip_prefix("\\@plus"))
{
Some(rest) => split_glue_part(rest.trim_start())?,
None => (0, rest),
};
let shrink = match rest.trim_start().strip_prefix("minus") {
let rest = rest.trim_start();
let shrink = match rest
.strip_prefix("minus")
.or_else(|| rest.strip_prefix("\\@minus"))
{
Some(rest) => split_glue_part(rest.trim_start())?.0,
None => 0,
};
Some((width, stretch, shrink))
}
/// Match a register copy such as `\belowdisplayskip \abovedisplayskip`.
fn glue_copy(line: &str, name: &str, source: &str) -> bool {
let Some(rest) = line.strip_prefix('\\') else {
return false;
};
let Some(rest) = rest.strip_prefix(name) else {
return false;
};
let rest = rest.trim_start();
let rest = rest.strip_prefix('=').unwrap_or(rest).trim();
rest == format!("\\{source}")
}
fn split_glue_part(text: &str) -> Option<(i64, &str)> {
let text = text.trim_start();
// TeX's internal dimension macros: \z@ is 0pt and \p@ is 1pt.
if let Some(rest) = text.strip_prefix("\\z@") {
return Some((0, rest));
}
let end = text
.find(|character: char| {
!(character.is_ascii_digit() || character == '.' || character == '-')
@@ -4650,11 +4697,11 @@ fn split_glue_part(text: &str) -> Option<(i64, &str)> {
.unwrap_or(text.len());
let number: f64 = text[..end].parse().ok()?;
let unit_text = &text[end..];
let (unit, rest) = ["pt", "bp", "in", "cm", "mm", "pc"]
let (unit, rest) = ["pt", "bp", "in", "cm", "mm", "pc", "\\p@"]
.iter()
.find_map(|unit| unit_text.strip_prefix(unit).map(|rest| (*unit, rest)))?;
let factor = match unit {
"pt" => 65_536.0,
"pt" | "\\p@" => 65_536.0,
"bp" => 65_782.0,
"in" => 4_736_286.0,
"cm" => 1_864_680.0,
@@ -9516,6 +9563,18 @@ fn color_switch(text: &str) -> Option<(String, String)> {
None
}
/// LaTeX's article-class display skips for 10pt, 11pt and 12pt.
fn display_skip_defaults(base_font_size: i64) -> ((i64, i64, i64), (i64, i64, i64)) {
const PT: i64 = 65_536;
if base_font_size >= 12 * PT {
((12 * PT, 3 * PT, 7 * PT), (12 * PT, 3 * PT, 7 * PT))
} else if base_font_size >= 11 * PT {
((11 * PT, 3 * PT, 6 * PT), (11 * PT, 3 * PT, 6 * PT))
} else {
((10 * PT, 2 * PT, 5 * PT), (10 * PT, 2 * PT, 5 * PT))
}
}
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
build_document_with_labels(paragraphs, geometry, &mut HashMap::new(), &mut Vec::new())
}
@@ -9603,7 +9662,7 @@ fn build_document_with_labels(
}
}
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
let base_units: Vec<Unit> = paragraphs
let mut base_units: Vec<Unit> = paragraphs
.iter()
.enumerate()
.map(|(index, paragraph)| {
@@ -9949,6 +10008,22 @@ fn build_document_with_labels(
.collect();
// Footnotes follow their anchor paragraph as insertion units, so the page
// builder reserves room for them on the page that mentions them.
let (default_above_display, default_below_display) =
display_skip_defaults(geometry.base_font_size.unwrap_or(655_360));
let above_display = geometry.above_display_skip.unwrap_or(default_above_display);
let below_display = geometry.below_display_skip.unwrap_or(default_below_display);
for index in 0..base_units.len().min(paragraphs.len()) {
if paragraphs[index].math {
base_units[index].space_before += above_display.0;
base_units[index].space_stretch += above_display.1;
base_units[index].space_shrink += above_display.2;
}
if index > 0 && paragraphs[index - 1].math {
base_units[index].space_before += below_display.0;
base_units[index].space_stretch += below_display.1;
base_units[index].space_shrink += below_display.2;
}
}
let mut units: Vec<Unit> = Vec::with_capacity(base_units.len());
let mut unit_for_paragraph = Vec::with_capacity(paragraphs.len());
let mut footnote_count = 0usize;
@@ -13451,6 +13526,93 @@ A definition.
assert_eq!(document.widow_penalty, 88);
}
#[test]
fn display_math_uses_article_display_skips() {
let source = "\\documentclass{article}\n\\begin{document}\n\nBefore.\n\n\\[ x \\]\n\nAfter.\n\n\\end{document}\n";
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs(&body).expect("paragraphs");
let document = build_document(&paragraphs, &geometry);
let math_index = document
.units
.iter()
.position(|unit| {
unit.items
.iter()
.any(|item| matches!(item, VerticalItem::Math { .. }))
})
.expect("display math unit");
let math = &document.units[math_index];
assert_eq!(math.space_before, 10 * SCALE);
assert_eq!(math.space_stretch, 2 * SCALE);
assert_eq!(math.space_shrink, 5 * SCALE);
let after = &document.units[math_index + 1];
assert_eq!(after.space_before, 10 * SCALE);
assert_eq!(after.space_stretch, 2 * SCALE);
assert_eq!(after.space_shrink, 5 * SCALE);
}
#[test]
fn display_skips_accept_class_file_spelling() {
let source = "\\documentclass{article}\n\\abovedisplayskip 7\\p@ \\@plus 2\\p@ \\@minus 5\\p@\n\\abovedisplayshortskip \\z@ \\@plus 3\\p@\n\\belowdisplayskip \\abovedisplayskip\n\\belowdisplayshortskip 4\\p@ \\@plus 3\\p@ \\@minus 3\\p@\n\\begin{document}\n\nBefore.\n\n\\[ x \\]\n\nAfter.\n\n\\end{document}\n";
let (_body, geometry) = geometry_and_body(source);
assert_eq!(
geometry.above_display_skip,
Some((7 * SCALE, 2 * SCALE, 5 * SCALE))
);
assert_eq!(geometry.above_display_short_skip, Some((0, 3 * SCALE, 0)));
assert_eq!(
geometry.below_display_skip,
Some((7 * SCALE, 2 * SCALE, 5 * SCALE))
);
assert_eq!(
geometry.below_display_short_skip,
Some((4 * SCALE, 3 * SCALE, 3 * SCALE))
);
}
#[test]
fn display_skip_registers_override_the_class_defaults() {
let source = "\\documentclass{article}\n\\abovedisplayskip=30pt plus 1pt minus 2pt\n\\belowdisplayskip=40pt plus 3pt minus 4pt\n\\abovedisplayshortskip=1pt plus 2pt\n\\belowdisplayshortskip=3pt minus 1pt\n\\begin{document}\n\nBefore.\n\n\\[ x \\]\n\nAfter.\n\n\\end{document}\n";
let (body, geometry) = geometry_and_body(source);
assert_eq!(
geometry.above_display_skip,
Some((30 * SCALE, SCALE, 2 * SCALE))
);
assert_eq!(
geometry.below_display_skip,
Some((40 * SCALE, 3 * SCALE, 4 * SCALE))
);
assert_eq!(
geometry.above_display_short_skip,
Some((SCALE, 2 * SCALE, 0))
);
assert_eq!(
geometry.below_display_short_skip,
Some((3 * SCALE, 0, SCALE))
);
let paragraphs = paragraphs(&body).expect("paragraphs");
let document = build_document(&paragraphs, &geometry);
let math_index = document
.units
.iter()
.position(|unit| {
unit.items
.iter()
.any(|item| matches!(item, VerticalItem::Math { .. }))
})
.expect("display math unit");
let math = &document.units[math_index];
assert_eq!(
(math.space_before, math.space_stretch, math.space_shrink),
(30 * SCALE, SCALE, 2 * SCALE)
);
let after = &document.units[math_index + 1];
assert_eq!(
(after.space_before, after.space_stretch, after.space_shrink),
(40 * SCALE, 3 * SCALE, 4 * SCALE)
);
}
#[test]
fn geometry_reads_demerit_registers() {
let source = "\\documentclass{article}\n\\hyphenpenalty=77\n\\exhyphenpenalty=88\n\\doublehyphendemerits=1234\n\\finalhyphendemerits=4321\n\\linepenalty=20\n\\adjdemerits=999\n\\begin{document}\n\nSome words here.\n\n\\end{document}\n";