Add smash math boxes

This commit is contained in:
gloamxun committed 2026-10-20 13:10:02 +08:00
1 parent 4e45a354fe
commit c311938129
2 files changed
+34

No files matched your search

+33
View File
@@ -30,6 +30,11 @@ pub enum MathAtom {
BigOperator { BigOperator {
name: String, name: String,
}, },
Smash {
body: Box<MathAtom>,
top: bool,
bottom: bool,
},
Phantom { Phantom {
body: Box<MathAtom>, body: Box<MathAtom>,
horizontal: bool, horizontal: bool,
@@ -236,6 +241,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth, depth: base_box.depth,
} }
} }
MathAtom::Smash { body, top, bottom } => {
let body = layout_math(body, font);
MathBox {
width: body.width,
height: if *top { 0 } else { body.height },
depth: if *bottom { 0 } else { body.depth },
}
}
MathAtom::Phantom { MathAtom::Phantom {
body, body,
horizontal, horizontal,
@@ -544,6 +557,17 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after, after,
) )
} }
"smash" => {
let (body, after) = parse_atom(chars, next);
(
MathAtom::Smash {
body: Box::new(body),
top: true,
bottom: true,
},
after,
)
}
"phantom" | "hphantom" | "vphantom" => { "phantom" | "hphantom" | "vphantom" => {
let (body, after) = parse_atom(chars, next); let (body, after) = parse_atom(chars, next);
( (
@@ -804,6 +828,15 @@ mod tests {
assert_eq!(calligraphic.height, upright.height); assert_eq!(calligraphic.height, upright.height);
} }
#[test]
fn smash_removes_height_but_keeps_width() {
let font = Cmr10Metrics::default();
let plain = layout_math(&parse_math("x"), &font);
let smashed = layout_math(&parse_math("\\smash{x}"), &font);
assert_eq!(smashed.width, plain.width);
assert_eq!(smashed.height, 0);
assert_eq!(smashed.depth, 0);
}
#[test] #[test]
fn phantoms_keep_only_selected_dimensions() { fn phantoms_keep_only_selected_dimensions() {
let font = Cmr10Metrics::default(); let font = Cmr10Metrics::default();
+1
View File
@@ -113,6 +113,7 @@ a single cost function.
- `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`, - `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`,
`\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds `\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds
accent height without changing the base width. accent height without changing the base width.
- `MathAtom::Smash { body, top, bottom }` implements `\smash` (zero height/depth).
- `MathAtom::Phantom { body, horizontal, vertical }` implements - `MathAtom::Phantom { body, horizontal, vertical }` implements
`\phantom`, `\hphantom`, `\vphantom` dimension-only boxes. `\phantom`, `\hphantom`, `\vphantom` dimension-only boxes.
- `MathAtom::BigOperator { name }` gives `\sum`/`\prod` 12pt width and - `MathAtom::BigOperator { name }` gives `\sum`/`\prod` 12pt width and