Add overbrace and underbrace math atoms

This commit is contained in:
gloamxun committed 2026-10-20 13:02:09 +08:00
1 parent 833253a936
commit 4e892005cd
2 files changed
+35

No files matched your search

+33
View File
@@ -27,6 +27,10 @@ pub enum MathAtom {
Underline {
base: Box<MathAtom>,
},
Brace {
base: Box<MathAtom>,
above: bool,
},
Binomial {
numerator: Box<MathAtom>,
denominator: Box<MathAtom>,
@@ -205,6 +209,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
depth: base_box.depth,
}
}
MathAtom::Brace { base, above } => {
let base_box = layout_math(base, font);
MathBox {
width: base_box.width.max(4 * 65_536),
height: base_box.height + if *above { 2 * 65_536 } else { 0 },
depth: base_box.depth + if *above { 0 } else { 2 * 65_536 },
}
}
MathAtom::Binomial {
numerator,
denominator,
@@ -404,6 +416,16 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
after,
)
}
"overbrace" | "underbrace" => {
let (base, after) = parse_atom(chars, next);
(
MathAtom::Brace {
base: Box::new(base),
above: command == "overbrace",
},
after,
)
}
"binom" | "dbinom" | "tbinom" => {
let (numerator, after) = parse_atom(chars, next);
let (denominator, after) = parse_atom(chars, after);
@@ -558,6 +580,17 @@ mod tests {
assert!(indexed.height > plain.height);
}
#[test]
fn braces_add_height_or_depth() {
let font = Cmr10Metrics::default();
let over = layout_math(&parse_math("\\overbrace{x+y}"), &font);
let under = layout_math(&parse_math("\\underbrace{x+y}"), &font);
let plain = layout_math(&parse_math("x+y"), &font);
assert!(over.height > plain.height);
assert_eq!(over.depth, plain.depth);
assert!(under.depth > plain.depth);
assert_eq!(under.height, plain.height);
}
#[test]
fn binom_stacks_two_arguments() {
let font = Cmr10Metrics::default();
+2
View File
@@ -110,6 +110,8 @@ a single cost function.
- `MathAtom::Accent { accent, base }` parses `\hat`, `\widehat`,
`\tilde`, `\widetilde`, `\bar`, `\overline` and `\vec`; it adds
accent height without changing the base width.
- `MathAtom::Brace { base, above }` parses `\overbrace`/`\underbrace` and
adds 2pt of height or depth.
- `MathAtom::Binomial { numerator, denominator }` parses
`\binom{n}{k}` / `\dbinom` / `\tbinom`; layout stacks the arguments with a
4pt delimiter allowance.