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

+89 -10
View File
@@ -293,23 +293,61 @@ fn pdftex_pages(source: &str) -> Option<Vec<String>> {
fn page_alnum_text(page: &texrra_tex_core::Page) -> String {
let mut text = String::new();
for item in &page.items {
let VerticalItem::Paragraph(paragraph) = item else {
continue;
};
for line in &paragraph.lines {
for item in line {
if let HorizontalItem::Box(box_) = item
&& let Some(character) = box_.character
&& character.is_ascii_alphanumeric()
{
text.push(character);
match item {
VerticalItem::Paragraph(paragraph) => {
for line in &paragraph.lines {
for item in line {
if let HorizontalItem::Box(box_) = item
&& let Some(character) = box_.character
&& character.is_ascii_alphanumeric()
{
text.push(character);
}
}
}
}
VerticalItem::Math { atom, .. } => math_alnum_text(atom, &mut text),
_ => {}
}
}
text
}
fn math_alnum_text(atom: &texrra_tex_core::math::MathAtom, text: &mut String) {
use texrra_tex_core::math::MathAtom;
match atom {
MathAtom::Symbol(character) if character.is_ascii_alphanumeric() => text.push(*character),
MathAtom::Text(value) => {
text.extend(value.chars().filter(char::is_ascii_alphanumeric));
}
MathAtom::Row(atoms) => {
for atom in atoms {
math_alnum_text(atom, text);
}
}
MathAtom::Classed { body, .. } => math_alnum_text(body, text),
MathAtom::Script { base, sub, sup } => {
math_alnum_text(base, text);
if let Some(sub) = sub {
math_alnum_text(sub, text);
}
if let Some(sup) = sup {
math_alnum_text(sup, text);
}
}
MathAtom::Limits { base, sub, sup } => {
math_alnum_text(base, text);
if let Some(sub) = sub {
math_alnum_text(sub, text);
}
if let Some(sup) = sup {
math_alnum_text(sup, text);
}
}
_ => {}
}
}
/// Lay `paragraphs` out with the route-D scrolling builder using article
/// metrics that match the pdfTeX fixtures (`\hsize` 345pt, `\topskip` 12pt,
/// `\vsize` 550pt, `\parskip` 0).
@@ -874,6 +912,47 @@ fn widow_penalty_forces_a_break_before_the_last_line() {
}
}
#[test]
fn display_skips_push_a_display_to_the_next_page() {
let source = "\\documentclass[10pt]{article}\n\
\\pdfpagewidth=612bp \\pdfpageheight=792bp\n\
\\pagestyle{empty}\n\
\\begin{document}\n\
\\hsize=345pt \\parindent=0pt \\topskip=12pt \\vsize=60pt \\abovedisplayskip=45pt \\belowdisplayskip=20pt\n\
Before text.\\par\n\\[ x \\]\nAfter text.\\par\n\\end{document}\n";
let Some(reference) = pdftex_pages(&source) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let style = ParagraphStyle {
line_width: 345 * PT,
par_indent: 0,
line_height: 12 * PT,
..ParagraphStyle::default()
};
let before = Unit::paragraph_with_style(1, "Before text.", style.clone());
let font = texrra_tex_core::cmr10::Cmr10Metrics::default();
let mut display = Unit::math(2, texrra_tex_core::math::parse_math("x"), &font);
display.space_before = 45 * PT;
let mut after = Unit::paragraph_with_style(3, "After text.", style);
after.space_before = 20 * PT;
let mut document = Document::new(60 * PT, vec![before, display, after]);
document.top_skip = 12 * PT;
document.max_depth = 5 * PT;
let engine = Engine::layout_paged(&document).expect("paged layout");
let ours: Vec<String> = engine.pages().iter().map(page_alnum_text).collect();
assert_eq!(
ours.len(),
reference.len(),
"route-D produced {} pages, pdfLaTeX {}",
ours.len(),
reference.len()
);
for (index, (ours, reference)) in ours.iter().zip(&reference).enumerate() {
assert_eq!(ours, reference, "text on page {}", index + 1);
}
}
#[test]
fn pages_break_like_pdftex_for_single_line_paragraphs() {
let paragraphs: Vec<String> = (0..100)