From e460da1ce7436242e9c356a23b97d6a474085544 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 3 Jan 2023 12:40:14 +0100 Subject: Split up math library --- library/src/math/script.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 library/src/math/script.rs (limited to 'library/src/math/script.rs') 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, + /// The superscript. + pub sup: Option, +} + +#[node] +impl ScriptNode { + fn construct(_: &Vm, args: &mut Args) -> SourceResult { + 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(()) + } +} -- cgit v1.2.3