diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-01-27 11:57:00 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-01-27 11:57:00 +0100 |
| commit | 13efa128c855637a7fe3351a4579383359d1be1b (patch) | |
| tree | ab4e699eb1ecbb0f8323a05ffdaf9b8be494b500 | |
| parent | a8fd64f9289b92614b9e6c16e909ec0c45429027 (diff) | |
Macro for math operators
| -rw-r--r-- | library/src/math/mod.rs | 5 | ||||
| -rw-r--r-- | library/src/math/op.rs | 86 | ||||
| -rw-r--r-- | library/src/math/spacing.rs | 2 |
3 files changed, 54 insertions, 39 deletions
diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index 45e33daf..cbe636b1 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -53,6 +53,7 @@ pub fn module() -> Module { let mut math = Scope::deduplicating(); math.def_func::<FormulaNode>("formula"); math.def_func::<LrNode>("lr"); + math.def_func::<OpNode>("op"); math.def_func::<FloorFunc>("floor"); math.def_func::<CeilFunc>("ceil"); math.def_func::<AbsFunc>("abs"); @@ -75,8 +76,8 @@ pub fn module() -> Module { math.def_func::<FrakNode>("frak"); math.def_func::<MonoNode>("mono"); math.def_func::<BbNode>("bb"); - define_spacings(&mut math); - define_operators(&mut math); + spacing::define(&mut math); + op::define(&mut math); Module::new("math").with_scope(math) } diff --git a/library/src/math/op.rs b/library/src/math/op.rs index a7d29166..cf1f9105 100644 --- a/library/src/math/op.rs +++ b/library/src/math/op.rs @@ -48,42 +48,56 @@ impl LayoutMath for OpNode { } } -/// Hook up all operators. -pub(super) fn define_operators(math: &mut Scope) { - math.define("arccos", op("arccos", false)); - math.define("arcsin", op("arcsin", false)); - math.define("arctan", op("arctan", false)); - math.define("arg", op("arg", false)); - math.define("cos", op("cos", false)); - math.define("cosh", op("cosh", false)); - math.define("cot", op("cot", false)); - math.define("coth", op("coth", false)); - math.define("csc", op("csc", false)); - math.define("deg", op("deg", false)); - math.define("det", op("det", true)); - math.define("dim", op("dim", false)); - math.define("exp", op("exp", false)); - math.define("gcd", op("gcd", true)); - math.define("hom", op("hom", false)); - math.define("inf", op("inf", true)); - math.define("ker", op("ker", false)); - math.define("lg", op("lg", false)); - math.define("lim", op("lim", true)); - math.define("ln", op("ln", false)); - math.define("log", op("log", false)); - math.define("max", op("max", true)); - math.define("min", op("min", true)); - math.define("Pr", op("Pr", true)); - math.define("sec", op("sec", false)); - math.define("sin", op("sin", false)); - math.define("sinh", op("sinh", false)); - math.define("sup", op("sup", true)); - math.define("tan", op("tan", false)); - math.define("tanh", op("tanh", false)); - math.define("liminf", op("lim inf", true)); - math.define("limsup", op("lim sup", true)); +macro_rules! ops { + ($($name:ident $(: $value:literal)? $(($tts:tt))?),* $(,)?) => { + pub(super) fn define(math: &mut Scope) { + $(math.define( + stringify!($name), + OpNode { + text: ops!(@name $name $(: $value)?).into(), + limits: ops!(@limit $($tts)*), + }.pack() + );)* + } + }; + (@name $name:ident) => { stringify!($name) }; + (@name $name:ident: $value:literal) => { $value }; + (@limit limits) => { true }; + (@limit) => { false }; } -fn op(name: &str, limits: bool) -> Content { - OpNode { text: name.into(), limits }.pack() +ops! { + arccos, + arcsin, + arctan, + arg, + cos, + cosh, + cot, + coth, + csc, + deg, + det (limits), + dim, + exp, + gcd (limits), + hom, + mod, + inf (limits), + ker, + lg, + lim (limits), + ln, + log, + max (limits), + min (limits), + Pr (limits), + sec, + sin, + sinh, + sup (limits), + tan, + tanh, + liminf: "lim inf" (limits), + limsup: "lim sup" (limits), } diff --git a/library/src/math/spacing.rs b/library/src/math/spacing.rs index d5a7603d..cf86b698 100644 --- a/library/src/math/spacing.rs +++ b/library/src/math/spacing.rs @@ -7,7 +7,7 @@ const THICK: Em = Em::new(5.0 / 18.0); const QUAD: Em = Em::new(1.0); /// Hook up all spacings. -pub(super) fn define_spacings(math: &mut Scope) { +pub(super) fn define(math: &mut Scope) { math.define("thin", HNode::strong(THIN).pack()); math.define("med", HNode::strong(MEDIUM).pack()); math.define("thick", HNode::strong(THICK).pack()); |
