diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-10-27 19:04:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-27 18:04:55 +0000 |
| commit | be7cfc85d08c545abfac08098b7b33b4bd71f37e (patch) | |
| tree | f4137fa2aaa57babae1f7603a9b2ed7e688f43d8 /crates/typst-library/src/math/op.rs | |
| parent | b8034a343831e8609aec2ec81eb7eeda57aa5d81 (diff) | |
Split out four new crates (#5302)
Diffstat (limited to 'crates/typst-library/src/math/op.rs')
| -rw-r--r-- | crates/typst-library/src/math/op.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/crates/typst-library/src/math/op.rs b/crates/typst-library/src/math/op.rs new file mode 100644 index 00000000..ef24705a --- /dev/null +++ b/crates/typst-library/src/math/op.rs @@ -0,0 +1,103 @@ +use ecow::EcoString; + +use crate::foundations::{elem, Content, NativeElement, Scope}; +use crate::layout::HElem; +use crate::math::{upright, Mathy, THIN}; +use crate::text::TextElem; + +/// A text operator in an equation. +/// +/// # Example +/// ```example +/// $ tan x = (sin x)/(cos x) $ +/// $ op("custom", +/// limits: #true)_(n->oo) n $ +/// ``` +/// +/// # Predefined Operators { #predefined } +/// Typst predefines the operators `arccos`, `arcsin`, `arctan`, `arg`, `cos`, +/// `cosh`, `cot`, `coth`, `csc`, `csch`, `ctg`, `deg`, `det`, `dim`, `exp`, +/// `gcd`, `hom`, `id`, `im`, `inf`, `ker`, `lg`, `lim`, `liminf`, `limsup`, +/// `ln`, `log`, `max`, `min`, `mod`, `Pr`, `sec`, `sech`, `sin`, `sinc`, +/// `sinh`, `sup`, `tan`, `tanh`, `tg` and `tr`. +#[elem(title = "Text Operator", Mathy)] +pub struct OpElem { + /// The operator's text. + #[required] + pub text: Content, + + /// Whether the operator should show attachments as limits in display mode. + #[default(false)] + pub limits: bool, +} + +macro_rules! ops { + ($($name:ident $(: $value:literal)? $(($tts:tt))?),* $(,)?) => { + pub(super) fn define(math: &mut Scope) { + $({ + let operator = EcoString::from(ops!(@name $name $(: $value)?)); + math.define( + stringify!($name), + OpElem::new(TextElem::new(operator).into()) + .with_limits(ops!(@limit $($tts)*)) + .pack() + ); + })* + + let dif = |d| { + HElem::new(THIN.into()).with_weak(true).pack() + + upright(TextElem::packed(d)) + }; + math.define("dif", dif('d')); + math.define("Dif", dif('D')); + } + }; + (@name $name:ident) => { stringify!($name) }; + (@name $name:ident: $value:literal) => { $value }; + (@limit limits) => { true }; + (@limit) => { false }; +} + +ops! { + arccos, + arcsin, + arctan, + arg, + cos, + cosh, + cot, + coth, + csc, + csch, + ctg, + deg, + det (limits), + dim, + exp, + gcd (limits), + hom, + id, + im, + inf (limits), + ker, + lg, + lim (limits), + liminf: "lim inf" (limits), + limsup: "lim sup" (limits), + ln, + log, + max (limits), + min (limits), + mod, + Pr (limits), + sec, + sech, + sin, + sinc, + sinh, + sup (limits), + tan, + tanh, + tg, + tr, +} |
