typst_library/math/
frac.rs

1use typst_syntax::Spanned;
2
3use crate::diag::bail;
4use crate::foundations::{elem, Content, Value};
5use crate::math::Mathy;
6
7/// A mathematical fraction.
8///
9/// # Example
10/// ```example
11/// $ 1/2 < (x+1)/2 $
12/// $ ((x+1)) / 2 = frac(a, b) $
13/// ```
14///
15/// # Syntax
16/// This function also has dedicated syntax: Use a slash to turn neighbouring
17/// expressions into a fraction. Multiple atoms can be grouped into a single
18/// expression using round grouping parenthesis. Such parentheses are removed
19/// from the output, but you can nest multiple to force them.
20#[elem(title = "Fraction", Mathy)]
21pub struct FracElem {
22    /// The fraction's numerator.
23    #[required]
24    pub num: Content,
25
26    /// The fraction's denominator.
27    #[required]
28    pub denom: Content,
29}
30
31/// A binomial expression.
32///
33/// # Example
34/// ```example
35/// $ binom(n, k) $
36/// $ binom(n, k_1, k_2, k_3, ..., k_m) $
37/// ```
38#[elem(title = "Binomial", Mathy)]
39pub struct BinomElem {
40    /// The binomial's upper index.
41    #[required]
42    pub upper: Content,
43
44    /// The binomial's lower index.
45    #[required]
46    #[variadic]
47    #[parse(
48        let values = args.all::<Spanned<Value>>()?;
49        if values.is_empty() {
50            // Prevents one element binomials
51            bail!(args.span, "missing argument: lower");
52        }
53        values.into_iter().map(|spanned| spanned.v.display()).collect()
54    )]
55    pub lower: Vec<Content>,
56}