From af8a34c2e21cdd1dc7234d9c8af913fc726fc2c1 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 13:24:02 +0800 Subject: [PATCH] Model boxed math atoms --- crates/texrra-tex-core/src/math.rs | 30 ++++++++++++++++++++++++++++++ docs/TEX_CORE.md | 2 ++ 2 files changed, 32 insertions(+) diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 02329d9..72fedf1 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -30,6 +30,9 @@ pub enum MathAtom { Negated { base: Box, }, + Boxed { + body: Box, + }, BigOperator { name: String, }, @@ -340,6 +343,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { depth: denominator.depth, } } + MathAtom::Boxed { body } => { + let inner = layout_math(body, font); + MathBox { + width: inner.width + 6 * 65_536, + height: inner.height + 3 * 65_536, + depth: inner.depth + 3 * 65_536, + } + } MathAtom::Negated { base } => { let inner = layout_math(base, font); MathBox { @@ -777,6 +788,15 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { after, ) } + "boxed" => { + let (body, after) = parse_atom(chars, next); + ( + MathAtom::Boxed { + body: Box::new(body), + }, + after, + ) + } "text" | "mathrm" => { let mut after = next; if chars.get(after) == Some(&'{') { @@ -939,6 +959,16 @@ mod tests { assert!(sum.height > plain.height); assert!(sum.depth > plain.depth); } + + #[test] + fn boxed_pads_its_body_on_all_sides() { + let font = Cmr10Metrics::default(); + let plain = layout_math(&parse_math("x"), &font); + let boxed = layout_math(&parse_math(r"\boxed{x}"), &font); + assert_eq!(boxed.width, plain.width + 6 * 65_536); + assert_eq!(boxed.height, plain.height + 3 * 65_536); + assert_eq!(boxed.depth, plain.depth + 3 * 65_536); + } #[test] fn mathinner_adds_three_mu_on_both_sides() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index e7e0ffb..cffc24e 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -206,3 +206,5 @@ for a later overlaid slash. `\pmod{n}` is a `Classed` relation, and `\not` inherits the base atom's math class for spacing. `\mathinner{...}` uses `MathClass::Inner` with 3mu on either side. + +`\boxed{x}` adds 3pt padding around its body in the math model.