mirror of
https://github.com/typst/typst
synced 2025-05-14 17:15:28 +08:00
Multinomial coefficients (#2237)
This commit is contained in:
parent
1819a0b266
commit
6dab95473b
@ -29,7 +29,7 @@ pub struct FracElem {
|
|||||||
impl LayoutMath for FracElem {
|
impl LayoutMath for FracElem {
|
||||||
#[tracing::instrument(skip(ctx))]
|
#[tracing::instrument(skip(ctx))]
|
||||||
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
|
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
|
||||||
layout(ctx, &self.num(), &self.denom(), false, self.span())
|
layout(ctx, &self.num(), &[self.denom()], false, self.span())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +38,7 @@ impl LayoutMath for FracElem {
|
|||||||
/// # Example
|
/// # Example
|
||||||
/// ```example
|
/// ```example
|
||||||
/// $ binom(n, k) $
|
/// $ binom(n, k) $
|
||||||
|
/// $ binom(n, k_1, k_2, k_3, ..., k_m) $
|
||||||
/// ```
|
/// ```
|
||||||
#[elem(title = "Binomial", LayoutMath)]
|
#[elem(title = "Binomial", LayoutMath)]
|
||||||
pub struct BinomElem {
|
pub struct BinomElem {
|
||||||
@ -47,7 +48,16 @@ pub struct BinomElem {
|
|||||||
|
|
||||||
/// The binomial's lower index.
|
/// The binomial's lower index.
|
||||||
#[required]
|
#[required]
|
||||||
pub lower: Content,
|
#[variadic]
|
||||||
|
#[parse(
|
||||||
|
let values = args.all::<Spanned<Value>>()?;
|
||||||
|
if values.is_empty() {
|
||||||
|
// Prevents one element binomials
|
||||||
|
bail!(args.span, "missing argument: lower");
|
||||||
|
}
|
||||||
|
values.into_iter().map(|spanned| spanned.v.display()).collect()
|
||||||
|
)]
|
||||||
|
pub lower: Vec<Content>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LayoutMath for BinomElem {
|
impl LayoutMath for BinomElem {
|
||||||
@ -60,7 +70,7 @@ impl LayoutMath for BinomElem {
|
|||||||
fn layout(
|
fn layout(
|
||||||
ctx: &mut MathContext,
|
ctx: &mut MathContext,
|
||||||
num: &Content,
|
num: &Content,
|
||||||
denom: &Content,
|
denom: &[Content],
|
||||||
binom: bool,
|
binom: bool,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> SourceResult<()> {
|
) -> SourceResult<()> {
|
||||||
@ -93,7 +103,10 @@ fn layout(
|
|||||||
ctx.unstyle();
|
ctx.unstyle();
|
||||||
|
|
||||||
ctx.style(ctx.style.for_denominator());
|
ctx.style(ctx.style.for_denominator());
|
||||||
let denom = ctx.layout_frame(denom)?;
|
let denom = ctx.layout_frame(&Content::sequence(
|
||||||
|
// Add a comma between each element.
|
||||||
|
denom.iter().flat_map(|a| [TextElem::packed(','), a.clone()]).skip(1),
|
||||||
|
))?;
|
||||||
ctx.unstyle();
|
ctx.unstyle();
|
||||||
|
|
||||||
let around = FRAC_AROUND.scaled(ctx);
|
let around = FRAC_AROUND.scaled(ctx);
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 30 KiB |
@ -16,6 +16,10 @@ $ x = (-b plus.minus sqrt(b^2 - 4a c))/(2a) $
|
|||||||
// Test binomial.
|
// Test binomial.
|
||||||
$ binom(circle, square) $
|
$ binom(circle, square) $
|
||||||
|
|
||||||
|
---
|
||||||
|
// Test multinomial coefficients.
|
||||||
|
$ binom(n, k_1, k_2, k_3) $
|
||||||
|
|
||||||
---
|
---
|
||||||
// Error: 8-13 missing argument: lower
|
// Error: 8-13 missing argument: lower
|
||||||
$ binom(x^2) $
|
$ binom(x^2) $
|
||||||
|
Loading…
x
Reference in New Issue
Block a user