summaryrefslogtreecommitdiff
path: root/crates/typst-library/src/math/class.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/typst-library/src/math/class.rs')
-rw-r--r--crates/typst-library/src/math/class.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/crates/typst-library/src/math/class.rs b/crates/typst-library/src/math/class.rs
new file mode 100644
index 00000000..0d6a370b
--- /dev/null
+++ b/crates/typst-library/src/math/class.rs
@@ -0,0 +1,36 @@
+use super::*;
+
+/// Forced use of a certain math class.
+///
+/// This is useful to treat certain symbols as if they were of a different
+/// class, e.g. to make text behave like a binary operator.
+///
+/// # Example
+/// ```example
+/// $x class("relation", "<=") 5$
+/// ```
+///
+/// Display: Class
+/// Category: math
+#[element(LayoutMath)]
+pub struct ClassElem {
+ /// The class to apply to the content.
+ #[required]
+ pub class: MathClass,
+
+ /// The content to which the class is applied.
+ #[required]
+ pub body: Content,
+}
+
+impl LayoutMath for ClassElem {
+ fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
+ ctx.style(ctx.style.with_class(self.class()));
+ let mut fragment = ctx.layout_fragment(&self.body())?;
+ ctx.unstyle();
+
+ fragment.set_class(self.class());
+ ctx.push(fragment);
+ Ok(())
+ }
+}