From 764bc188503186726e6b0547acd59beb1d8e6afa Mon Sep 17 00:00:00 2001 From: GloamXun Date: Wed, 28 Oct 2026 00:32:56 +0800 Subject: [PATCH] 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. --- crates/texrra-tex-core/src/math.rs | 18 ++++++++++++++++++ crates/texrra-tex-core/src/render.rs | 21 +++++++++++++++++++++ docs/TEX_FIDELITY_ROADMAP.md | 2 +- docs/WEB_ROUTE_D_EXPERIMENT.md | 2 +- src/route_d.rs | 3 ++- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 300b3c6..087f7fa 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -85,6 +85,12 @@ pub enum MathAtom { body: Box, tag: String, }, + /// A display equation tag placed at the right edge of a fixed measure. + TaggedWidth { + body: Box, + tag: String, + width: i64, + }, Limits { base: Box, sub: Option>, @@ -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)); diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 7eeda72..97b05d6 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -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); diff --git a/docs/TEX_FIDELITY_ROADMAP.md b/docs/TEX_FIDELITY_ROADMAP.md index 595ee78..ba47dfd 100644 --- a/docs/TEX_FIDELITY_ROADMAP.md +++ b/docs/TEX_FIDELITY_ROADMAP.md @@ -48,7 +48,7 @@ execution is not a goal. - project `.bib` entries now produce author-year labels and natbib-style parenthetical citations; inline `thebibliography` without labels remains numeric, and the full natbib option surface is not implemented; - - display math is approximate; + - display math is approximate; equation tags now sit at the right margin; - PNG/JPEG/BMP floats now reserve their image height, embed the raster with its real pixel dimensions, centre the image within the float, and emit it into the SVG preview as a PNG data URI. Table floats are centred with their diff --git a/docs/WEB_ROUTE_D_EXPERIMENT.md b/docs/WEB_ROUTE_D_EXPERIMENT.md index 044f0fb..12b7cec 100644 --- a/docs/WEB_ROUTE_D_EXPERIMENT.md +++ b/docs/WEB_ROUTE_D_EXPERIMENT.md @@ -148,7 +148,7 @@ PDF was rendered page-by-page next to the route-D output. | Citations | natbib author-year `(Kour and Saabne, 2014a; …)` | author-year `(Kour and Saabne, 2014a; Keshet et al., 2016)` for `.bib`; numeric for unlabeled inline bibliographies | | Table | booktabs rules, `Size (µm)`, `~100`, `10^6` | centred with rules; `Size (µm)`, `~100`, `10⁶` | | Figure | framed rule box above the caption | centred framed rule body (raster) on page 3 | -| Display equation | fully typeset fraction/product/sum | approximate, visibly jumbled | +| Display equation | fully typeset fraction/product/sum | approximate body; equation number now at the right margin | | References | author-year, italic venue, DOI link | author-year entries; plain venue text, no DOI | | Float order | figure/table on page 3 with section 3.4 | figure/table on page 3 above 3.3/3.4 | diff --git a/src/route_d.rs b/src/route_d.rs index 442da43..6d0621c 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -13989,9 +13989,10 @@ fn build_document_with_labels( .map(|number| format!("({number})")) }); let atom = match tag { - Some(tag) => MathAtom::Tagged { + Some(tag) => MathAtom::TaggedWidth { body: Box::new(atom), tag, + width: line_width, }, None => atom, };