Macro for math operators

This commit is contained in:
Laurenz 2023-01-27 11:57:00 +01:00
parent a8fd64f928
commit 13efa128c8
3 changed files with 54 additions and 39 deletions

View File

@ -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)
}

View File

@ -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("liminf", true));
math.define("limsup", op("limsup", 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: "liminf" (limits),
limsup: "limsup" (limits),
}

View File

@ -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());