Rename formula to equation

This commit is contained in:
Laurenz 2023-03-18 19:27:31 +01:00
parent a16726ae66
commit a69b587455
21 changed files with 102 additions and 101 deletions

View File

@ -49,8 +49,8 @@ Let's disect what's going on:
- We insert a heading with the `= Heading` syntax. One equals sign creates a top - We insert a heading with the `= Heading` syntax. One equals sign creates a top
level heading, two create a subheading and so on. level heading, two create a subheading and so on.
- [Math formulas][math] are enclosed in dollar signs. By adding extra spaces - [Mathematical equations][math] are enclosed in dollar signs. By adding extra
around the contents of a formula, we can put it into a separate block. spaces around the contents of a equation, we can put it into a separate block.
Multi-letter identifiers are interpreted as Typst definitions and functions Multi-letter identifiers are interpreted as Typst definitions and functions
unless put into quotes. This way, we don't need backslashes for things like unless put into quotes. This way, we don't need backslashes for things like
`floor` and `sqrt`. And `phi.alt` applies the `alt` modifier to the `phi` to `floor` and `sqrt`. And `phi.alt` applies the `alt` modifier to the `phi` to

View File

@ -27,7 +27,7 @@
"overbracket", "overbracket",
] ]
description: | description: |
Delimiters above or below parts of a formula. Delimiters above or below parts of an equation.
The braces and brackets further allow you to add an optional annotation The braces and brackets further allow you to add an optional annotation
below or above themselves. below or above themselves.
@ -44,7 +44,7 @@
Subscript, superscripts, and limits. Subscript, superscripts, and limits.
The `attach` function backs the `[$a_b^c$]` syntax that adds top and bottom The `attach` function backs the `[$a_b^c$]` syntax that adds top and bottom
attachments to a part of a formula. Attachments can be displayed either as attachments to a part of an equation. Attachments can be displayed either as
sub/superscripts, or limits. Typst automatically decides which is more sub/superscripts, or limits. Typst automatically decides which is more
suitable depending on the base, but you can also control this manually with suitable depending on the base, but you can also control this manually with
the `scripts` and `limits` functions. the `scripts` and `limits` functions.

View File

@ -40,10 +40,11 @@ more about their syntax and usage.
## Math mode { #math } ## Math mode { #math }
Math mode is a special markup mode that is used to typeset mathematical Math mode is a special markup mode that is used to typeset mathematical
formulas. It is entered by wrapping a formula in `[$]` characters. The formula formulas. It is entered by wrapping an equation in `[$]` characters. The
will be typeset into its own block if it starts and ends with at least one space equation will be typeset into its own block if it starts and ends with at least
(e.g. `[$ x^2 $]`). Inline math can be produced by omitting the whitespace (e.g. one space (e.g. `[$ x^2 $]`). Inline math can be produced by omitting the
`[$x^2$]`). An overview over the syntax specific to math mode follows: whitespace (e.g. `[$x^2$]`). An overview over the syntax specific to math mode
follows:
| Name | Example | See | | Name | Example | See |
| ---------------------- | ------------------------ | ------------------------ | | ---------------------- | ------------------------ | ------------------------ |

View File

@ -261,7 +261,7 @@ pub struct BlockNode {
/// ///
/// ```example /// ```example
/// #set align(center) /// #set align(center)
/// #show math.formula: set block(above: 8pt, below: 16pt) /// #show math.equation: set block(above: 8pt, below: 16pt)
/// ///
/// This sum of $x$ and $y$: /// This sum of $x$ and $y$:
/// $ x + y = z $ /// $ x + y = z $

View File

@ -50,7 +50,7 @@ use typst::diag::SourceResult;
use typst::eval::Tracer; use typst::eval::Tracer;
use typst::model::{applicable, realize, SequenceNode, StyleVecBuilder, StyledNode}; use typst::model::{applicable, realize, SequenceNode, StyleVecBuilder, StyledNode};
use crate::math::{FormulaNode, LayoutMath}; use crate::math::{EquationNode, LayoutMath};
use crate::meta::DocumentNode; use crate::meta::DocumentNode;
use crate::prelude::*; use crate::prelude::*;
use crate::shared::BehavedBuilder; use crate::shared::BehavedBuilder;
@ -245,9 +245,9 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> {
mut content: &'a Content, mut content: &'a Content,
styles: StyleChain<'a>, styles: StyleChain<'a>,
) -> SourceResult<()> { ) -> SourceResult<()> {
if content.can::<dyn LayoutMath>() && !content.is::<FormulaNode>() { if content.can::<dyn LayoutMath>() && !content.is::<EquationNode>() {
content = content =
self.scratch.content.alloc(FormulaNode::new(content.clone()).pack()); self.scratch.content.alloc(EquationNode::new(content.clone()).pack());
} }
if let Some(styled) = content.to::<StyledNode>() { if let Some(styled) = content.to::<StyledNode>() {
@ -491,7 +491,7 @@ impl<'a> ParBuilder<'a> {
|| content.is::<HNode>() || content.is::<HNode>()
|| content.is::<LinebreakNode>() || content.is::<LinebreakNode>()
|| content.is::<SmartQuoteNode>() || content.is::<SmartQuoteNode>()
|| content.to::<FormulaNode>().map_or(false, |node| !node.block(styles)) || content.to::<EquationNode>().map_or(false, |node| !node.block(styles))
|| content.is::<BoxNode>() || content.is::<BoxNode>()
{ {
self.0.push(content.clone(), styles); self.0.push(content.clone(), styles);

View File

@ -7,7 +7,7 @@ use typst::model::StyledNode;
use super::{BoxNode, HNode, Sizing, Spacing}; use super::{BoxNode, HNode, Sizing, Spacing};
use crate::layout::AlignNode; use crate::layout::AlignNode;
use crate::math::FormulaNode; use crate::math::EquationNode;
use crate::prelude::*; use crate::prelude::*;
use crate::text::{ use crate::text::{
shape, LinebreakNode, Quoter, Quotes, ShapedText, SmartQuoteNode, SpaceNode, TextNode, shape, LinebreakNode, Quoter, Quotes, ShapedText, SmartQuoteNode, SpaceNode, TextNode,
@ -324,8 +324,8 @@ enum Segment<'a> {
Text(usize), Text(usize),
/// Horizontal spacing between other segments. /// Horizontal spacing between other segments.
Spacing(Spacing), Spacing(Spacing),
/// A math formula. /// A mathematical equation.
Formula(&'a FormulaNode), Equation(&'a EquationNode),
/// A box with arbitrary content. /// A box with arbitrary content.
Box(&'a BoxNode, bool), Box(&'a BoxNode, bool),
/// Metadata. /// Metadata.
@ -339,7 +339,7 @@ impl Segment<'_> {
Self::Text(len) => len, Self::Text(len) => len,
Self::Spacing(_) => SPACING_REPLACE.len_utf8(), Self::Spacing(_) => SPACING_REPLACE.len_utf8(),
Self::Box(_, true) => SPACING_REPLACE.len_utf8(), Self::Box(_, true) => SPACING_REPLACE.len_utf8(),
Self::Formula(_) | Self::Box(_, _) | Self::Meta => NODE_REPLACE.len_utf8(), Self::Equation(_) | Self::Box(_, _) | Self::Meta => NODE_REPLACE.len_utf8(),
} }
} }
} }
@ -597,9 +597,9 @@ fn collect<'a>(
full.push(if node.double(styles) { '"' } else { '\'' }); full.push(if node.double(styles) { '"' } else { '\'' });
} }
Segment::Text(full.len() - prev) Segment::Text(full.len() - prev)
} else if let Some(node) = child.to::<FormulaNode>() { } else if let Some(node) = child.to::<EquationNode>() {
full.push(NODE_REPLACE); full.push(NODE_REPLACE);
Segment::Formula(node) Segment::Equation(node)
} else if let Some(node) = child.to::<BoxNode>() { } else if let Some(node) = child.to::<BoxNode>() {
let frac = node.width(styles).is_fractional(); let frac = node.width(styles).is_fractional();
full.push(if frac { SPACING_REPLACE } else { NODE_REPLACE }); full.push(if frac { SPACING_REPLACE } else { NODE_REPLACE });
@ -671,9 +671,9 @@ fn prepare<'a>(
items.push(Item::Fractional(v, None)); items.push(Item::Fractional(v, None));
} }
}, },
Segment::Formula(formula) => { Segment::Equation(equation) => {
let pod = Regions::one(region, Axes::splat(false)); let pod = Regions::one(region, Axes::splat(false));
let mut frame = formula.layout(vt, styles, pod)?.into_frame(); let mut frame = equation.layout(vt, styles, pod)?.into_frame();
frame.translate(Point::with_y(TextNode::baseline_in(styles))); frame.translate(Point::with_y(TextNode::baseline_in(styles)));
items.push(Item::Frame(frame)); items.push(Item::Frame(frame));
} }

View File

@ -211,7 +211,7 @@ fn items() -> LangItems {
node.pack() node.pack()
}, },
term_item: |term, description| layout::TermItem::new(term, description).pack(), term_item: |term, description| layout::TermItem::new(term, description).pack(),
formula: |body, block| math::FormulaNode::new(body).with_block(block).pack(), equation: |body, block| math::EquationNode::new(body).with_block(block).pack(),
math_align_point: || math::AlignPointNode::new().pack(), math_align_point: || math::AlignPointNode::new().pack(),
math_delimited: |open, body, close| math::LrNode::new(open + body + close).pack(), math_delimited: |open, body, close| math::LrNode::new(open + body + close).pack(),
math_attach: |base, bottom, top| { math_attach: |base, bottom, top| {

View File

@ -47,7 +47,7 @@ use crate::text::{
/// Create a module with all math definitions. /// Create a module with all math definitions.
pub fn module() -> Module { pub fn module() -> Module {
let mut math = Scope::deduplicating(); let mut math = Scope::deduplicating();
math.define("formula", FormulaNode::id()); math.define("equation", EquationNode::id());
math.define("text", TextNode::id()); math.define("text", TextNode::id());
// Grouping. // Grouping.
@ -106,7 +106,7 @@ pub fn module() -> Module {
Module::new("math").with_scope(math) Module::new("math").with_scope(math)
} }
/// A mathematical formula. /// A mathematical equation.
/// ///
/// Can be displayed inline with text or as a separate block. /// Can be displayed inline with text or as a separate block.
/// ///
@ -125,25 +125,25 @@ pub fn module() -> Module {
/// ///
/// ## Syntax /// ## Syntax
/// This function also has dedicated syntax: Write mathematical markup within /// This function also has dedicated syntax: Write mathematical markup within
/// dollar signs to create a formula. Starting and ending the formula with at /// dollar signs to create an equation. Starting and ending the equation with at
/// least one space lifts it into a separate block that is centered /// least one space lifts it into a separate block that is centered
/// horizontally. For more details about math syntax, see the /// horizontally. For more details about math syntax, see the
/// [main math page]($category/math). /// [main math page]($category/math).
/// ///
/// Display: Formula /// Display: Equation
/// Category: math /// Category: math
#[node(Show, Finalize, Layout, LayoutMath)] #[node(Show, Finalize, Layout, LayoutMath)]
pub struct FormulaNode { pub struct EquationNode {
/// Whether the formula is displayed as a separate block. /// Whether the equation is displayed as a separate block.
#[default(false)] #[default(false)]
pub block: bool, pub block: bool,
/// The content of the formula. /// The contents of the equation.
#[required] #[required]
pub body: Content, pub body: Content,
} }
impl Show for FormulaNode { impl Show for EquationNode {
fn show(&self, _: &mut Vt, styles: StyleChain) -> SourceResult<Content> { fn show(&self, _: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
let mut realized = self.clone().pack().guarded(Guard::Base(NodeId::of::<Self>())); let mut realized = self.clone().pack().guarded(Guard::Base(NodeId::of::<Self>()));
if self.block(styles) { if self.block(styles) {
@ -153,7 +153,7 @@ impl Show for FormulaNode {
} }
} }
impl Finalize for FormulaNode { impl Finalize for EquationNode {
fn finalize(&self, realized: Content, _: StyleChain) -> Content { fn finalize(&self, realized: Content, _: StyleChain) -> Content {
realized realized
.styled(TextNode::set_weight(FontWeight::from_number(450))) .styled(TextNode::set_weight(FontWeight::from_number(450)))
@ -163,7 +163,7 @@ impl Finalize for FormulaNode {
} }
} }
impl Layout for FormulaNode { impl Layout for EquationNode {
fn layout( fn layout(
&self, &self,
vt: &mut Vt, vt: &mut Vt,
@ -209,7 +209,7 @@ pub trait LayoutMath {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()>; fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()>;
} }
impl LayoutMath for FormulaNode { impl LayoutMath for EquationNode {
fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> { fn layout_math(&self, ctx: &mut MathContext) -> SourceResult<()> {
self.body().layout_math(ctx) self.body().layout_math(ctx)
} }

View File

@ -2,7 +2,7 @@ use typst::eval::Scope;
use super::*; use super::*;
/// A text operator in a math formula. /// A text operator in an equation.
/// ///
/// ## Example /// ## Example
/// ```example /// ```example

View File

@ -11,7 +11,7 @@ use super::*;
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct BoldNode { pub struct BoldNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -36,7 +36,7 @@ impl LayoutMath for BoldNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct UprightNode { pub struct UprightNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -58,7 +58,7 @@ impl LayoutMath for UprightNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct ItalicNode { pub struct ItalicNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -80,7 +80,7 @@ impl LayoutMath for ItalicNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct SerifNode { pub struct SerifNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -105,7 +105,7 @@ impl LayoutMath for SerifNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct SansNode { pub struct SansNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -130,7 +130,7 @@ impl LayoutMath for SansNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct CalNode { pub struct CalNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -155,7 +155,7 @@ impl LayoutMath for CalNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct FrakNode { pub struct FrakNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -180,7 +180,7 @@ impl LayoutMath for FrakNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct MonoNode { pub struct MonoNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -210,7 +210,7 @@ impl LayoutMath for MonoNode {
/// Category: math /// Category: math
#[node(LayoutMath)] #[node(LayoutMath)]
pub struct BbNode { pub struct BbNode {
/// The piece of formula to style. /// The content to style.
#[required] #[required]
pub body: Content, pub body: Content,
} }
@ -224,7 +224,7 @@ impl LayoutMath for BbNode {
} }
} }
/// Text properties in a formula. /// Text properties in math.
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct MathStyle { pub struct MathStyle {
/// The style variant to select. /// The style variant to select.
@ -298,7 +298,7 @@ impl MathStyle {
} }
} }
/// The size of elements in a formula. /// The size of elements in an equation.
/// ///
/// See the TeXbook p. 141. /// See the TeXbook p. 141.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]

View File

@ -77,18 +77,18 @@ pub struct LangItems {
pub enum_item: fn(number: Option<NonZeroUsize>, body: Content) -> Content, pub enum_item: fn(number: Option<NonZeroUsize>, body: Content) -> Content,
/// An item in a term list: `/ Term: Details`. /// An item in a term list: `/ Term: Details`.
pub term_item: fn(term: Content, description: Content) -> Content, pub term_item: fn(term: Content, description: Content) -> Content,
/// A mathematical formula: `$x$`, `$ x^2 $`. /// A mathematical equation: `$x$`, `$ x^2 $`.
pub formula: fn(body: Content, block: bool) -> Content, pub equation: fn(body: Content, block: bool) -> Content,
/// An alignment point in a formula: `&`. /// An alignment point in math: `&`.
pub math_align_point: fn() -> Content, pub math_align_point: fn() -> Content,
/// Matched delimiters surrounding math in a formula: `[x + y]`. /// Matched delimiters in math: `[x + y]`.
pub math_delimited: fn(open: Content, body: Content, close: Content) -> Content, pub math_delimited: fn(open: Content, body: Content, close: Content) -> Content,
/// A base with optional attachments in a formula: `a_1^2`. /// A base with optional attachments in math: `a_1^2`.
pub math_attach: pub math_attach:
fn(base: Content, bottom: Option<Content>, top: Option<Content>) -> Content, fn(base: Content, bottom: Option<Content>, top: Option<Content>) -> Content,
/// A base with an accent: `arrow(x)`. /// A base with an accent: `arrow(x)`.
pub math_accent: fn(base: Content, accent: char) -> Content, pub math_accent: fn(base: Content, accent: char) -> Content,
/// A fraction in a formula: `x/2`. /// A fraction in math: `x/2`.
pub math_frac: fn(num: Content, denom: Content) -> Content, pub math_frac: fn(num: Content, denom: Content) -> Content,
/// Dispatch a method on a library value. /// Dispatch a method on a library value.
pub library_method: fn( pub library_method: fn(
@ -126,7 +126,7 @@ impl Hash for LangItems {
self.list_item.hash(state); self.list_item.hash(state);
self.enum_item.hash(state); self.enum_item.hash(state);
self.term_item.hash(state); self.term_item.hash(state);
self.formula.hash(state); self.equation.hash(state);
self.math_align_point.hash(state); self.math_align_point.hash(state);
self.math_delimited.hash(state); self.math_delimited.hash(state);
self.math_attach.hash(state); self.math_attach.hash(state);

View File

@ -426,7 +426,7 @@ impl Eval for ast::Expr {
Self::List(v) => v.eval(vm).map(Value::Content), Self::List(v) => v.eval(vm).map(Value::Content),
Self::Enum(v) => v.eval(vm).map(Value::Content), Self::Enum(v) => v.eval(vm).map(Value::Content),
Self::Term(v) => v.eval(vm).map(Value::Content), Self::Term(v) => v.eval(vm).map(Value::Content),
Self::Formula(v) => v.eval(vm).map(Value::Content), Self::Equation(v) => v.eval(vm).map(Value::Content),
Self::Math(v) => v.eval(vm).map(Value::Content), Self::Math(v) => v.eval(vm).map(Value::Content),
Self::MathIdent(v) => v.eval(vm), Self::MathIdent(v) => v.eval(vm),
Self::MathAlignPoint(v) => v.eval(vm).map(Value::Content), Self::MathAlignPoint(v) => v.eval(vm).map(Value::Content),
@ -626,13 +626,13 @@ impl Eval for ast::TermItem {
} }
} }
impl Eval for ast::Formula { impl Eval for ast::Equation {
type Output = Content; type Output = Content;
fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> { fn eval(&self, vm: &mut Vm) -> SourceResult<Self::Output> {
let body = self.body().eval(vm)?; let body = self.body().eval(vm)?;
let block = self.block(); let block = self.block();
Ok((vm.items.formula)(body, block)) Ok((vm.items.equation)(body, block))
} }
} }

View File

@ -45,7 +45,7 @@ impl<'a> Scopes<'a> {
.ok_or("unknown variable")?) .ok_or("unknown variable")?)
} }
/// Try to access a variable immutably from within a math formula. /// Try to access a variable immutably in math.
pub fn get_in_math(&self, var: &str) -> StrResult<&Value> { pub fn get_in_math(&self, var: &str) -> StrResult<&Value> {
Ok(std::iter::once(&self.top) Ok(std::iter::once(&self.top)
.chain(self.scopes.iter().rev()) .chain(self.scopes.iter().rev())

View File

@ -237,13 +237,13 @@ fn markup_completions(ctx: &mut CompletionContext) {
ctx.snippet_completion( ctx.snippet_completion(
"math (inline)", "math (inline)",
"$${x}$", "$${x}$",
"Inserts an inline-level mathematical formula.", "Inserts an inline-level mathematical equation.",
); );
ctx.snippet_completion( ctx.snippet_completion(
"math (block)", "math (block)",
"$ ${sum_x^2} $", "$ ${sum_x^2} $",
"Inserts a block-level mathematical formula.", "Inserts a block-level mathematical equation.",
); );
} }
@ -251,7 +251,7 @@ fn markup_completions(ctx: &mut CompletionContext) {
fn complete_math(ctx: &mut CompletionContext) -> bool { fn complete_math(ctx: &mut CompletionContext) -> bool {
if !matches!( if !matches!(
ctx.leaf.parent_kind(), ctx.leaf.parent_kind(),
Some(SyntaxKind::Formula) Some(SyntaxKind::Equation)
| Some(SyntaxKind::Math) | Some(SyntaxKind::Math)
| Some(SyntaxKind::MathFrac) | Some(SyntaxKind::MathFrac)
| Some(SyntaxKind::MathAttach) | Some(SyntaxKind::MathAttach)
@ -935,10 +935,10 @@ impl<'a> CompletionContext<'a> {
/// Add completions for all font families. /// Add completions for all font families.
fn font_completions(&mut self) { fn font_completions(&mut self) {
let formula = self.before[self.cursor.saturating_sub(25)..].contains("formula"); let equation = self.before[self.cursor.saturating_sub(25)..].contains("equation");
for (family, iter) in self.world.book().families() { for (family, iter) in self.world.book().families() {
let detail = summarize_font_family(iter); let detail = summarize_font_family(iter);
if !formula || family.contains("Math") { if !equation || family.contains("Math") {
self.value_completion( self.value_completion(
None, None,
&Value::Str(family.into()), &Value::Str(family.into()),
@ -1121,7 +1121,7 @@ impl<'a> CompletionContext<'a> {
let in_math = matches!( let in_math = matches!(
self.leaf.parent_kind(), self.leaf.parent_kind(),
Some(SyntaxKind::Formula) Some(SyntaxKind::Equation)
| Some(SyntaxKind::Math) | Some(SyntaxKind::Math)
| Some(SyntaxKind::MathFrac) | Some(SyntaxKind::MathFrac)
| Some(SyntaxKind::MathAttach) | Some(SyntaxKind::MathAttach)

View File

@ -27,9 +27,9 @@ pub enum Tag {
ListMarker, ListMarker,
/// A term in a term list. /// A term in a term list.
ListTerm, ListTerm,
/// The delimiters of a math formula. /// The delimiters of an equation.
MathDelimiter, MathDelimiter,
/// An operator with special meaning in a math formula. /// An operator with special meaning in an equation.
MathOperator, MathOperator,
/// A keyword. /// A keyword.
Keyword, Keyword,
@ -138,7 +138,7 @@ pub fn highlight(node: &LinkedNode) -> Option<Tag> {
SyntaxKind::EnumMarker => Some(Tag::ListMarker), SyntaxKind::EnumMarker => Some(Tag::ListMarker),
SyntaxKind::TermItem => None, SyntaxKind::TermItem => None,
SyntaxKind::TermMarker => Some(Tag::ListMarker), SyntaxKind::TermMarker => Some(Tag::ListMarker),
SyntaxKind::Formula => None, SyntaxKind::Equation => None,
SyntaxKind::Math => None, SyntaxKind::Math => None,
SyntaxKind::MathIdent => highlight_ident(node), SyntaxKind::MathIdent => highlight_ident(node),

View File

@ -112,19 +112,19 @@ pub enum Expr {
Enum(EnumItem), Enum(EnumItem),
/// An item in a term list: `/ Term: Details`. /// An item in a term list: `/ Term: Details`.
Term(TermItem), Term(TermItem),
/// A math formula: `$x$`, `$ x^2 $`. /// A mathematical equation: `$x$`, `$ x^2 $`.
Formula(Formula), Equation(Equation),
/// A math formula: `$x$`, `$ x^2 $`. /// The contents of a mathematical equation: `x^2 + 1`.
Math(Math), Math(Math),
/// An identifier in a math formula: `pi`. /// An identifier in math: `pi`.
MathIdent(MathIdent), MathIdent(MathIdent),
/// An alignment point in a math formula: `&`. /// An alignment point in math: `&`.
MathAlignPoint(MathAlignPoint), MathAlignPoint(MathAlignPoint),
/// Matched delimiters surrounding math in a formula: `[x + y]`. /// Matched delimiters in math: `[x + y]`.
MathDelimited(MathDelimited), MathDelimited(MathDelimited),
/// A base with optional attachments in a formula: `a_1^2`. /// A base with optional attachments in math: `a_1^2`.
MathAttach(MathAttach), MathAttach(MathAttach),
/// A fraction in a math formula: `x/2`. /// A fraction in math: `x/2`.
MathFrac(MathFrac), MathFrac(MathFrac),
/// An identifier: `left`. /// An identifier: `left`.
Ident(Ident), Ident(Ident),
@ -214,7 +214,7 @@ impl AstNode for Expr {
SyntaxKind::ListItem => node.cast().map(Self::List), SyntaxKind::ListItem => node.cast().map(Self::List),
SyntaxKind::EnumItem => node.cast().map(Self::Enum), SyntaxKind::EnumItem => node.cast().map(Self::Enum),
SyntaxKind::TermItem => node.cast().map(Self::Term), SyntaxKind::TermItem => node.cast().map(Self::Term),
SyntaxKind::Formula => node.cast().map(Self::Formula), SyntaxKind::Equation => node.cast().map(Self::Equation),
SyntaxKind::Math => node.cast().map(Self::Math), SyntaxKind::Math => node.cast().map(Self::Math),
SyntaxKind::MathIdent => node.cast().map(Self::MathIdent), SyntaxKind::MathIdent => node.cast().map(Self::MathIdent),
SyntaxKind::MathAlignPoint => node.cast().map(Self::MathAlignPoint), SyntaxKind::MathAlignPoint => node.cast().map(Self::MathAlignPoint),
@ -273,7 +273,7 @@ impl AstNode for Expr {
Self::List(v) => v.as_untyped(), Self::List(v) => v.as_untyped(),
Self::Enum(v) => v.as_untyped(), Self::Enum(v) => v.as_untyped(),
Self::Term(v) => v.as_untyped(), Self::Term(v) => v.as_untyped(),
Self::Formula(v) => v.as_untyped(), Self::Equation(v) => v.as_untyped(),
Self::Math(v) => v.as_untyped(), Self::Math(v) => v.as_untyped(),
Self::MathIdent(v) => v.as_untyped(), Self::MathIdent(v) => v.as_untyped(),
Self::MathAlignPoint(v) => v.as_untyped(), Self::MathAlignPoint(v) => v.as_untyped(),
@ -696,17 +696,17 @@ impl TermItem {
} }
node! { node! {
/// A math formula: `$x$`, `$ x^2 $`. /// A mathemathical equation: `$x$`, `$ x^2 $`.
Formula Equation
} }
impl Formula { impl Equation {
/// The contained math. /// The contained math.
pub fn body(&self) -> Math { pub fn body(&self) -> Math {
self.0.cast_first_match().unwrap_or_default() self.0.cast_first_match().unwrap_or_default()
} }
/// Whether the formula should be displayed as a separate block. /// Whether the equation should be displayed as a separate block.
pub fn block(&self) -> bool { pub fn block(&self) -> bool {
let is_space = |node: Option<&SyntaxNode>| { let is_space = |node: Option<&SyntaxNode>| {
node.map(SyntaxNode::kind) == Some(SyntaxKind::Space) node.map(SyntaxNode::kind) == Some(SyntaxKind::Space)
@ -716,7 +716,7 @@ impl Formula {
} }
node! { node! {
/// Math markup. /// The contents of a mathematical equation: `x^2 + 1`.
Math Math
} }
@ -728,7 +728,7 @@ impl Math {
} }
node! { node! {
/// An identifier in a math formula: `pi`. /// An identifier in math: `pi`.
MathIdent MathIdent
} }
@ -758,12 +758,12 @@ impl Deref for MathIdent {
} }
node! { node! {
/// An alignment point in a formula: `&`. /// An alignment point in math: `&`.
MathAlignPoint MathAlignPoint
} }
node! { node! {
/// Matched delimiters surrounding math in a formula: `[x + y]`. /// Matched delimiters in math: `[x + y]`.
MathDelimited MathDelimited
} }
@ -785,7 +785,7 @@ impl MathDelimited {
} }
node! { node! {
/// A base with optional attachments in a formula: `a_1^2`. /// A base with optional attachments in math: `a_1^2`.
MathAttach MathAttach
} }
@ -813,7 +813,7 @@ impl MathAttach {
} }
node! { node! {
/// A fraction in a formula: `x/2` /// A fraction in math: `x/2`
MathFrac MathFrac
} }

View File

@ -56,18 +56,18 @@ pub enum SyntaxKind {
TermItem, TermItem,
/// Introduces a term item: `/`. /// Introduces a term item: `/`.
TermMarker, TermMarker,
/// A mathematical formula: `$x$`, `$ x^2 $`. /// A mathematical equation: `$x$`, `$ x^2 $`.
Formula, Equation,
/// Mathematical markup. /// The contents of a mathematical equation: `x^2 + 1`.
Math, Math,
/// An identifier in math: `pi`. /// An identifier in math: `pi`.
MathIdent, MathIdent,
/// An alignment point in math: `&`. /// An alignment point in math: `&`.
MathAlignPoint, MathAlignPoint,
/// Matched delimiters surrounding math in a formula: `[x + y]`. /// Matched delimiters in math: `[x + y]`.
MathDelimited, MathDelimited,
/// A base with optional attachments in a formula: `a_1^2`. /// A base with optional attachments in math: `a_1^2`.
MathAttach, MathAttach,
/// A fraction in math: `x/2`. /// A fraction in math: `x/2`.
MathFrac, MathFrac,
@ -100,7 +100,7 @@ pub enum SyntaxKind {
Star, Star,
/// Toggles emphasized text and indicates a subscript in math: `_`. /// Toggles emphasized text and indicates a subscript in math: `_`.
Underscore, Underscore,
/// Starts and ends a math formula: `$`. /// Starts and ends a mathematical equation: `$`.
Dollar, Dollar,
/// The unary plus and binary addition operator: `+`. /// The unary plus and binary addition operator: `+`.
Plus, Plus,
@ -342,7 +342,7 @@ impl SyntaxKind {
Self::EnumMarker => "enum marker", Self::EnumMarker => "enum marker",
Self::TermItem => "term list item", Self::TermItem => "term list item",
Self::TermMarker => "term marker", Self::TermMarker => "term marker",
Self::Formula => "math formula", Self::Equation => "equation",
Self::Math => "math", Self::Math => "math",
Self::MathIdent => "math identifier", Self::MathIdent => "math identifier",
Self::MathAlignPoint => "math alignment point", Self::MathAlignPoint => "math alignment point",

View File

@ -114,7 +114,7 @@ fn markup_expr(p: &mut Parser, at_start: &mut bool) {
SyntaxKind::EnumMarker if *at_start => enum_item(p), SyntaxKind::EnumMarker if *at_start => enum_item(p),
SyntaxKind::TermMarker if *at_start => term_item(p), SyntaxKind::TermMarker if *at_start => term_item(p),
SyntaxKind::RefMarker => reference(p), SyntaxKind::RefMarker => reference(p),
SyntaxKind::Dollar => formula(p), SyntaxKind::Dollar => equation(p),
SyntaxKind::LeftBracket SyntaxKind::LeftBracket
| SyntaxKind::RightBracket | SyntaxKind::RightBracket
@ -213,14 +213,14 @@ fn whitespace_line(p: &mut Parser) {
} }
} }
fn formula(p: &mut Parser) { fn equation(p: &mut Parser) {
let m = p.marker(); let m = p.marker();
p.enter(LexMode::Math); p.enter(LexMode::Math);
p.assert(SyntaxKind::Dollar); p.assert(SyntaxKind::Dollar);
math(p, |kind| kind == SyntaxKind::Dollar); math(p, |kind| kind == SyntaxKind::Dollar);
p.expect(SyntaxKind::Dollar); p.expect(SyntaxKind::Dollar);
p.exit(); p.exit();
p.wrap(m, SyntaxKind::Formula); p.wrap(m, SyntaxKind::Equation);
} }
fn math(p: &mut Parser, mut stop: impl FnMut(SyntaxKind) -> bool) { fn math(p: &mut Parser, mut stop: impl FnMut(SyntaxKind) -> bool) {
@ -631,7 +631,7 @@ fn code_primary(p: &mut Parser, atomic: bool) {
SyntaxKind::LeftBrace => code_block(p), SyntaxKind::LeftBrace => code_block(p),
SyntaxKind::LeftBracket => content_block(p), SyntaxKind::LeftBracket => content_block(p),
SyntaxKind::LeftParen => with_paren(p), SyntaxKind::LeftParen => with_paren(p),
SyntaxKind::Dollar => formula(p), SyntaxKind::Dollar => equation(p),
SyntaxKind::Let => let_binding(p), SyntaxKind::Let => let_binding(p),
SyntaxKind::Set => set_rule(p), SyntaxKind::Set => set_rule(p),
SyntaxKind::Show => show_rule(p), SyntaxKind::Show => show_rule(p),

View File

@ -1,6 +1,6 @@
// Integrated test for content fields. // Integrated test for content fields.
#let compute(formula, ..vars) = { #let compute(equation, ..vars) = {
let vars = vars.named() let vars = vars.named()
let f(node) = { let f(node) = {
let func = node.func() let func = node.func()
@ -28,14 +28,14 @@
.fold(0, (sum, v) => sum + v) .fold(0, (sum, v) => sum + v)
} }
} }
let result = f(formula.body) let result = f(equation.body)
[With ] [With ]
vars vars
.pairs() .pairs()
.map(p => $#p.first() = #p.last()$) .map(p => $#p.first() = #p.last()$)
.join(", ", last: " and ") .join(", ", last: " and ")
[ we have:] [ we have:]
$ formula = result $ $ equation = result $
} }
#compute($x y + y^2$, x: 2, y: 3) #compute($x y + y^2$, x: 2, y: 3)

View File

@ -10,7 +10,7 @@ $ sum_(i=#emoji.apple)^#emoji.apple.red i + monkey/2 $
$ x := #table(columns: 2)[x][y]/mat(1, 2, 3) $ x := #table(columns: 2)[x][y]/mat(1, 2, 3)
= #table[A][B][C] $ = #table[A][B][C] $
--- ---
// Test non-formula math directly in content. // Test non-equation math directly in content.
#math.attach($a$, top: [b]) #math.attach($a$, top: [b])
--- ---

View File

@ -26,5 +26,5 @@ $text(#red, "time"^2) + sqrt("place")$
--- ---
// Test different font. // Test different font.
#show math.formula: set text(font: "Fira Math") #show math.equation: set text(font: "Fira Math")
$ v := vec(1 + 2, 2 - 4, sqrt(3), arrow(x)) + 1 $ $ v := vec(1 + 2, 2 - 4, sqrt(3), arrow(x)) + 1 $