Skip to main content

typst_library/math/
op.rs

1use ecow::EcoString;
2use unicode_math_class::MathClass;
3
4use crate::foundations::{Content, NativeElement, Scope, SymbolElem, elem};
5use crate::layout::HElem;
6use crate::math::{ClassElem, Mathy, THIN, upright};
7use crate::text::TextElem;
8
9/// A text operator in an equation.
10///
11/// = Example <example>
12/// ```example
13/// $ tan x = (sin x)/(cos x) $
14/// $ op("custom",
15///      limits: #true)_(n->oo) n $
16/// ```
17///
18/// = #short-or-long[Predefined][Predefined Operators] <predefined>
19/// Typst predefines the operators `arccos`, `arcsin`, `arctan`, `arg`, `cos`,
20/// `cosh`, `cot`, `coth`, `csc`, `csch`, `ctg`, `deg`, `det`, `dim`, `exp`,
21/// `gcd`, `lcm`, `hom`, `id`, `im`, `inf`, `ker`, `lg`, `lim`, `liminf`,
22/// `limsup`, `ln`, `log`, `max`, `min`, `mod`, `Pr`, `sec`, `sech`, `sin`,
23/// `sinc`, `sinh`, `sup`, `tan`, `tanh`, `tg` and `tr`.
24#[elem(title = "Text Operator", Mathy)]
25pub struct OpElem {
26    /// The operator's text.
27    #[required]
28    pub text: Content,
29
30    /// Whether the operator should show attachments as limits in display mode.
31    #[default(false)]
32    pub limits: bool,
33}
34
35macro_rules! ops {
36    ($($name:ident $(: $value:literal)? $(($tts:tt))?),* $(,)?) => {
37        pub(super) fn define(math: &mut Scope) {
38            $({
39                let operator = EcoString::from(ops!(@name $name $(: $value)?));
40                math.define(
41                    stringify!($name),
42                    // Latex also uses their equivalent of `TextElem` here.
43                    OpElem::new(TextElem::new(operator).into())
44                        .with_limits(ops!(@limit $($tts)*))
45                        .pack()
46                );
47            })*
48
49            let dif = |d| {
50                HElem::new(THIN.into()).with_weak(true).pack()
51                    + ClassElem::new(MathClass::Unary, upright(SymbolElem::packed(d))).pack()
52            };
53            math.define("dif", dif('d'));
54            math.define("Dif", dif('D'));
55        }
56    };
57    (@name $name:ident) => { stringify!($name) };
58    (@name $name:ident: $value:literal) => { $value };
59    (@limit limits) => { true };
60    (@limit) => { false };
61}
62
63ops! {
64    arccos,
65    arcsin,
66    arctan,
67    arg,
68    cos,
69    cosh,
70    cot,
71    coth,
72    csc,
73    csch,
74    ctg,
75    deg,
76    det (limits),
77    dim,
78    exp,
79    gcd (limits),
80    lcm (limits),
81    hom,
82    id,
83    im,
84    inf (limits),
85    ker,
86    lg,
87    lim (limits),
88    liminf: "lim inf" (limits),
89    limsup: "lim sup" (limits),
90    ln,
91    log,
92    max (limits),
93    min (limits),
94    mod,
95    Pr (limits),
96    sec,
97    sech,
98    sin,
99    sinc,
100    sinh,
101    sup (limits),
102    tan,
103    tanh,
104    tg,
105    tr,
106}