diff --git a/crates/texrra-tex-core/src/math.rs b/crates/texrra-tex-core/src/math.rs index 27ce9bf..f20bf7f 100755 --- a/crates/texrra-tex-core/src/math.rs +++ b/crates/texrra-tex-core/src/math.rs @@ -27,6 +27,10 @@ pub enum MathAtom { Underline { base: Box, }, + Binomial { + numerator: Box, + denominator: Box, + }, Script { base: Box, sub: Option>, @@ -201,6 +205,18 @@ pub fn layout_math(atom: &MathAtom, font: &Cmr10Metrics) -> MathBox { 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 } => { let base_box = layout_math(base, font); MathBox { @@ -388,6 +404,17 @@ fn parse_atom(chars: &[char], index: usize) -> (MathAtom, usize) { 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" => { let (base, after) = parse_atom(chars, next); ( @@ -531,6 +558,15 @@ mod tests { 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] fn named_operators_use_upright_text_widths() { let font = Cmr10Metrics::default(); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 53c96a4..50f01ef 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -110,6 +110,9 @@ 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::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 upright `Text` atoms. - `MathAtom::Underline { base }` parses `\underline{...}` and adds 2pt depth