summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/math/frac.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-10-27 19:04:55 +0100
committerGitHub <noreply@github.com>2024-10-27 18:04:55 +0000
commitbe7cfc85d08c545abfac08098b7b33b4bd71f37e (patch)
treef4137fa2aaa57babae1f7603a9b2ed7e688f43d8 /crates/typst-library/src/math/frac.rs
parentb8034a343831e8609aec2ec81eb7eeda57aa5d81 (diff)
Split out four new crates (#5302)
Diffstat (limited to 'crates/typst-library/src/math/frac.rs')
-rw-r--r--crates/typst-library/src/math/frac.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/crates/typst-library/src/math/frac.rs b/crates/typst-library/src/math/frac.rs
new file mode 100644
index 00000000..f5c4514d
--- /dev/null
+++ b/crates/typst-library/src/math/frac.rs
@@ -0,0 +1,56 @@
+use typst_syntax::Spanned;
+
+use crate::diag::bail;
+use crate::foundations::{elem, Content, Value};
+use crate::math::Mathy;
+
+/// A mathematical fraction.
+///
+/// # Example
+/// ```example
+/// $ 1/2 < (x+1)/2 $
+/// $ ((x+1)) / 2 = frac(a, b) $
+/// ```
+///
+/// # Syntax
+/// This function also has dedicated syntax: Use a slash to turn neighbouring
+/// expressions into a fraction. Multiple atoms can be grouped into a single
+/// expression using round grouping parenthesis. Such parentheses are removed
+/// from the output, but you can nest multiple to force them.
+#[elem(title = "Fraction", Mathy)]
+pub struct FracElem {
+ /// The fraction's numerator.
+ #[required]
+ pub num: Content,
+
+ /// The fraction's denominator.
+ #[required]
+ pub denom: Content,
+}
+
+/// A binomial expression.
+///
+/// # Example
+/// ```example
+/// $ binom(n, k) $
+/// $ binom(n, k_1, k_2, k_3, ..., k_m) $
+/// ```
+#[elem(title = "Binomial", Mathy)]
+pub struct BinomElem {
+ /// The binomial's upper index.
+ #[required]
+ pub upper: Content,
+
+ /// The binomial's lower index.
+ #[required]
+ #[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>,
+}