summaryrefslogtreecommitdiff
path: root/library/src/math/script.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-01-03 12:40:14 +0100
committerLaurenz <laurmaedje@gmail.com>2023-01-03 12:40:14 +0100
commite460da1ce7436242e9c356a23b97d6a474085544 (patch)
tree2fcc821a05813661f5e3687c26c8626346322fff /library/src/math/script.rs
parente38839c28730ae99cd4ad357e588b5e7e840badc (diff)
Split up math library
Diffstat (limited to 'library/src/math/script.rs')
-rw-r--r--library/src/math/script.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/library/src/math/script.rs b/library/src/math/script.rs
new file mode 100644
index 00000000..c0255a2e
--- /dev/null
+++ b/library/src/math/script.rs
@@ -0,0 +1,70 @@
+use super::*;
+
+/// # Script
+/// A mathematical sub- and/or superscript.
+///
+/// _Note:_ In the future, this might be unified with the [sub](@sub) and
+/// [super](@super) functions that handle sub- and superscripts in text.
+///
+/// ## Syntax
+/// This function also has dedicated syntax: Use the underscore (`_`) to
+/// indicate a subscript and the circumflex (`^`) to indicate a superscript.
+///
+/// ## Example
+/// ```
+/// $ a_i = 2^(1+i) $
+/// ```
+///
+/// ## Parameters
+/// - base: Content (positional, required)
+/// The base to which the applies the sub- and/or superscript.
+///
+/// - sub: Content (named)
+/// The subscript.
+///
+/// - sup: Content (named)
+/// The superscript.
+///
+/// ## Category
+/// math
+#[func]
+#[capable(Texify)]
+#[derive(Debug, Hash)]
+pub struct ScriptNode {
+ /// The base.
+ pub base: Content,
+ /// The subscript.
+ pub sub: Option<Content>,
+ /// The superscript.
+ pub sup: Option<Content>,
+}
+
+#[node]
+impl ScriptNode {
+ fn construct(_: &Vm, args: &mut Args) -> SourceResult<Content> {
+ let base = args.expect("base")?;
+ let sub = args.named("sub")?;
+ let sup = args.named("sup")?;
+ Ok(Self { base, sub, sup }.pack())
+ }
+}
+
+impl Texify for ScriptNode {
+ fn texify(&self, t: &mut Texifier) -> SourceResult<()> {
+ self.base.texify(t)?;
+
+ if let Some(sub) = &self.sub {
+ t.push_str("_{");
+ sub.texify_unparen(t)?;
+ t.push_str("}");
+ }
+
+ if let Some(sup) = &self.sup {
+ t.push_str("^{");
+ sup.texify_unparen(t)?;
+ t.push_str("}");
+ }
+
+ Ok(())
+ }
+}