Add binomial math atoms
This commit is contained in:
1 parent
639f5e8f8b
commit
833253a936
2 files changed
+39
No files matched your search
@@ -27,6 +27,10 @@ pub enum MathAtom {
|
|||||||
Underline {
|
Underline {
|
||||||
base: Box<MathAtom>,
|
base: Box<MathAtom>,
|
||||||
},
|
},
|
||||||
|
Binomial {
|
||||||
|
numerator: Box<MathAtom>,
|
||||||
|
denominator: Box<MathAtom>,
|
||||||
|
},
|
||||||
Script {
|
Script {
|
||||||
base: Box<MathAtom>,
|
base: Box<MathAtom>,
|
||||||
sub: Option<Box<MathAtom>>,
|
sub: Option<Box<MathAtom>>,
|
||||||
@@ -201,6 +205,18 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox {
|
|||||||
depth: base_box.depth,
|
depth: base_box.depth,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
MathAtom::Binomial {
|
||||||
|
numerator,
|
||||||
|
denominator,
|
||||||
|
} => {
|
||||||
|
let numerator = layout_math(numerator, font);
|
||||||
|
let denominator = layout_math(denominator, font);
|
||||||
|
MathBox {
|
||||||
|
width: numerator.width.max(denominator.width) + 4 * 65_536,
|
||||||
|
height: numerator.height + denominator.height + 2 * 65_536,
|
||||||
|
depth: denominator.depth,
|
||||||
|
}
|
||||||
|
}
|
||||||
MathAtom::Underline { base } => {
|
MathAtom::Underline { base } => {
|
||||||
let base_box = layout_math(base, font);
|
let base_box = layout_math(base, font);
|
||||||
MathBox {
|
MathBox {
|
||||||
@@ -388,6 +404,17 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) {
|
|||||||
after,
|
after,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
"binom" | "dbinom" | "tbinom" => {
|
||||||
|
let (numerator, after) = parse_atom(chars, next);
|
||||||
|
let (denominator, after) = parse_atom(chars, after);
|
||||||
|
(
|
||||||
|
MathAtom::Binomial {
|
||||||
|
numerator: Box::new(numerator),
|
||||||
|
denominator: Box::new(denominator),
|
||||||
|
},
|
||||||
|
after,
|
||||||
|
)
|
||||||
|
}
|
||||||
"underline" => {
|
"underline" => {
|
||||||
let (base, after) = parse_atom(chars, next);
|
let (base, after) = parse_atom(chars, next);
|
||||||
(
|
(
|
||||||
@@ -531,6 +558,15 @@ mod tests {
|
|||||||
assert!(indexed.height > plain.height);
|
assert!(indexed.height > plain.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn binom_stacks_two_arguments() {
|
||||||
|
let font = Cmr10Metrics::default();
|
||||||
|
let binom = layout_math(&parse_math("\\binom{n}{k}"), &font);
|
||||||
|
let single = layout_math(&parse_math("n"), &font);
|
||||||
|
assert!(binom.width >= single.width);
|
||||||
|
assert!(binom.height > single.height);
|
||||||
|
assert!(binom.depth > 0);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn named_operators_use_upright_text_widths() {
|
fn named_operators_use_upright_text_widths() {
|
||||||
let font = Cmr10Metrics::default();
|
let font = Cmr10Metrics::default();
|
||||||
|
|||||||
@@ -110,6 +110,9 @@ 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::Binomial { numerator, denominator }` parses
|
||||||
|
`\binom{n}{k}` / `\dbinom` / `\tbinom`; layout stacks the arguments with a
|
||||||
|
4pt delimiter allowance.
|
||||||
- Named operators (`\max`, `\min`, `\sin`, `\log`, `\det`, ...) parse as
|
- Named operators (`\max`, `\min`, `\sin`, `\log`, `\det`, ...) parse as
|
||||||
upright `Text` atoms.
|
upright `Text` atoms.
|
||||||
- `MathAtom::Underline { base }` parses `\underline{...}` and adds 2pt depth
|
- `MathAtom::Underline { base }` parses `\underline{...}` and adds 2pt depth
|
||||||
|
|||||||
Reference in new issue
Block a user