Add overbrace and underbrace math atoms
This commit is contained in:
1 parent
833253a936
commit
4e892005cd
2 files changed
+35
No files matched your search
@@ -27,6 +27,10 @@ pub enum MathAtom {
|
|||||||
Underline {
|
Underline {
|
||||||
base: Box<MathAtom>,
|
base: Box<MathAtom>,
|
||||||
},
|
},
|
||||||
|
Brace {
|
||||||
|
base: Box<MathAtom>,
|
||||||
|
above: bool,
|
||||||
|
},
|
||||||
Binomial {
|
Binomial {
|
||||||
numerator: Box<MathAtom>,
|
numerator: Box<MathAtom>,
|
||||||
denominator: Box<MathAtom>,
|
denominator: Box<MathAtom>,
|
||||||
@@ -205,6 +209,14 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|||||||
depth: base_box.depth,
|
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 {
|
MathAtom::Binomial {
|
||||||
numerator,
|
numerator,
|
||||||
denominator,
|
denominator,
|
||||||
@@ -404,6 +416,16 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|||||||
after,
|
after,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
"overbrace" | "underbrace" => {
|
||||||
|
let (base, after) = parse_atom(chars, next);
|
||||||
|
(
|
||||||
|
MathAtom::Brace {
|
||||||
|
base: Box::new(base),
|
||||||
|
above: command == "overbrace",
|
||||||
|
},
|
||||||
|
after,
|
||||||
|
)
|
||||||
|
}
|
||||||
"binom" | "dbinom" | "tbinom" => {
|
"binom" | "dbinom" | "tbinom" => {
|
||||||
let (numerator, after) = parse_atom(chars, next);
|
let (numerator, after) = parse_atom(chars, next);
|
||||||
let (denominator, after) = parse_atom(chars, after);
|
let (denominator, after) = parse_atom(chars, after);
|
||||||
@@ -558,6 +580,17 @@ mod tests {
|
|||||||
assert!(indexed.height > plain.height);
|
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]
|
#[test]
|
||||||
fn binom_stacks_two_arguments() {
|
fn binom_stacks_two_arguments() {
|
||||||
let font = Cmr10Metrics::default();
|
let font = Cmr10Metrics::default();
|
||||||
|
|||||||
@@ -110,6 +110,8 @@ 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::Brace { base, above }` parses `\overbrace`/`\underbrace` and
|
||||||
|
adds 2pt of height or depth.
|
||||||
- `MathAtom::Binomial { numerator, denominator }` parses
|
- `MathAtom::Binomial { numerator, denominator }` parses
|
||||||
`\binom{n}{k}` / `\dbinom` / `\tbinom`; layout stacks the arguments with a
|
`\binom{n}{k}` / `\dbinom` / `\tbinom`; layout stacks the arguments with a
|
||||||
4pt delimiter allowance.
|
4pt delimiter allowance.
|
||||||
|
|||||||
Reference in new issue
Block a user