Place display equation tags at the right margin

Tagged display math used the body width as its box, so the equation number sat
immediately after the formula. Add a TaggedWidth atom carrying the line
measure: the body is centred in the measure and the tag is painted at its right
edge.

The arXiv equation number now matches the TeX right-margin placement; the
formula body remains approximate. Test count stays 1077.
This commit is contained in:
gloamxun committed 2026-10-28 00:32:56 +08:00
1 parent 9181bf49fe
commit 764bc18850
5 files changed
+43 -3

No files matched your search

+18
View File
@@ -85,6 +85,12 @@ pub enum MathAtom {
body: Box<MathAtom>,
tag: String,
},
/// A display equation tag placed at the right edge of a fixed measure.
TaggedWidth {
body: Box<MathAtom>,
tag: String,
width: i64,
},
Limits {
base: Box<MathAtom>,
sub: Option<Box<MathAtom>>,
@@ -1282,6 +1288,18 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: body_box.depth.max(2 * 65_536),
}
}
MathAtom::TaggedWidth { body, tag, width } => {
let body_box = layout_math(body, font);
let tag_width: i64 = tag
.chars()
.map(|character| Cmr10Metrics::Roman.width(character))
.sum();
MathBox {
width: (*width).max(body_box.width).max(tag_width),
height: body_box.height.max(7 * 65_536),
depth: body_box.depth.max(2 * 65_536),
}
}
MathAtom::Script { base, sub, sup } => {
let base_box = layout_math(base, font);
let sub_box = sub.as_deref().map(|atom| layout_math(atom, font));
+21
View File
@@ -1216,6 +1216,27 @@ impl Page {
pen += width;
}
}
MathAtom::TaggedWidth { body, tag, width } => {
let body_box = math::layout_math(body, font);
let body_x = x + ((*width - body_box.width) / 2).max(0);
Self::paint_math(body, font, body_x, baseline, size, list);
let tag_width: i64 = tag
.chars()
.map(|character| Cmr10Metrics::Roman.width(character))
.sum();
let mut pen = x + (*width).max(tag_width) - tag_width;
for character in tag.chars() {
let width = Cmr10Metrics::Roman.width(character);
list.push(DisplayItem::Text {
font: FontFace::Roman,
x: pen,
y: baseline,
size,
text: character.to_string(),
});
pen += width;
}
}
MathAtom::Script { base, sub, sup } => {
let base_box = math::layout_math(base, font);
Self::paint_math(base, font, x, baseline, size, list);