Model boxed math atoms

This commit is contained in:
gloamxun committed 2026-10-20 13:24:02 +08:00
1 parent a0bb8be2ac
commit af8a34c2e2
2 files changed
+32

No files matched your search

+30
View File
@@ -30,6 +30,9 @@ pub enum MathAtom {
Negated {
base: Box<MathAtom>,
},
Boxed {
body: Box<MathAtom>,
},
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();
+2
View File
@@ -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.