SSML Builder Documentation - v1.0.1
    Preparing search index...

    Class MathElement

    SSML element for embedding mathematical expressions using MathML.

    The <math> element allows you to include Mathematical Markup Language (MathML) content within SSML documents. This enables text-to-speech engines to correctly pronounce mathematical formulas, equations, and expressions. The speech synthesizer interprets the MathML markup and converts it to natural spoken mathematics, handling complex notation like fractions, superscripts, roots, and mathematical symbols appropriately.

    MathML is an XML-based language specifically designed for describing mathematical notation and capturing both its structure and content. When embedded in SSML, it ensures that mathematical content is spoken clearly and accurately, making it essential for educational content, scientific applications, and any scenario involving mathematical expressions.

    // Simple mathematical expression
    const math = new MathElement('<mrow><mi>x</mi><mo>+</mo><mn>2</mn></mrow>');
    math.render();
    // Output: <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>x</mi><mo>+</mo><mn>2</mn></mrow></math>

    // Quadratic formula
    const quadratic = new MathElement(`
    <mrow>
    <mi>x</mi>
    <mo>=</mo>
    <mfrac>
    <mrow>
    <mo>-</mo><mi>b</mi><mo>±</mo>
    <msqrt>
    <mrow>
    <msup><mi>b</mi><mn>2</mn></msup>
    <mo>-</mo>
    <mn>4</mn><mi>a</mi><mi>c</mi>
    </mrow>
    </msqrt>
    </mrow>
    <mrow><mn>2</mn><mi>a</mi></mrow>
    </mfrac>
    </mrow>
    `);

    // Use with SSMLBuilder
    const ssml = new SSMLBuilder({ lang: 'en-US' })
    .voice('en-US-AvaNeural')
    .text('The equation is: ')
    .math('<mrow><mi>a</mi><mo>+</mo><mi>b</mi></mrow>')
    .build();
    • The math element can only contain text and MathML elements
    • Not all TTS engines fully support MathML - check service documentation
    • The MathML namespace is automatically added to the math element
    • Complex mathematical expressions may be simplified in speech output
    • Consider providing alternative text descriptions for accessibility
    • MathML content should be well-formed XML
    • Some services may have limitations on MathML complexity

    Hierarchy (View Summary)

    Index

    Constructors

    Methods

    Constructors

    • Creates a new MathElement instance.

      Parameters

      • mathML: string

        The MathML markup as a string. Should contain valid MathML elements that describe the mathematical expression. The content will be wrapped in a math element with the MathML namespace. Common MathML elements include: - <mi>: Identifier (variables like x, y) - <mn>: Number - <mo>: Operator (+, -, =, etc.) - <mrow>: Group of elements - <mfrac>: Fraction - <msqrt>: Square root - <msup>: Superscript (powers) - <msub>: Subscript - <mroot>: Root with index

      Returns MathElement

      // Simple addition
      const addition = new MathElement(
      '<mrow><mn>2</mn><mo>+</mo><mn>2</mn></mrow>'
      );

      // Variable expression
      const variable = new MathElement(
      '<mrow><mi>y</mi><mo>=</mo><mi>m</mi><mi>x</mi><mo>+</mo><mi>b</mi></mrow>'
      );

      // Fraction
      const fraction = new MathElement(
      '<mfrac><mn>1</mn><mn>2</mn></mfrac>'
      );

      // Square root
      const sqrt = new MathElement(
      '<msqrt><mn>16</mn></msqrt>'
      );

      // Power/exponent
      const power = new MathElement(
      '<msup><mi>x</mi><mn>2</mn></msup>'
      );

      // Complex formula (Pythagorean theorem)
      const pythagorean = new MathElement(`
      <mrow>
      <msup><mi>a</mi><mn>2</mn></msup>
      <mo>+</mo>
      <msup><mi>b</mi><mn>2</mn></msup>
      <mo>=</mo>
      <msup><mi>c</mi><mn>2</mn></msup>
      </mrow>
      `);

      // Integral
      const integral = new MathElement(`
      <mrow>
      <msubsup>
      <mo>∫</mo>
      <mn>0</mn>
      <mi>∞</mi>
      </msubsup>
      <mi>f</mi><mo>(</mo><mi>x</mi><mo>)</mo>
      <mi>d</mi><mi>x</mi>
      </mrow>
      `);

    Methods

    • Protected

      Escapes special XML characters in text content to ensure valid XML output.

      This method replaces XML special characters with their corresponding entity references to prevent XML parsing errors and potential security issues (XML injection). It should be used whenever inserting user-provided or dynamic text content into XML elements.

      The following characters are escaped:

      • & becomes &amp; (must be escaped first to avoid double-escaping)
      • < becomes &lt; (prevents opening of unintended tags)
      • > becomes &gt; (prevents closing of unintended tags)
      • " becomes &quot; (prevents breaking out of attribute values)
      • ' becomes &apos; (prevents breaking out of attribute values)

      This method is marked as protected so it's only accessible to classes that extend SSMLElement, ensuring proper encapsulation while allowing all element implementations to use this essential functionality.

      Parameters

      • text: string

        The text content to escape

      Returns string

      The text with all special XML characters properly escaped

      // In a render method implementation
      class TextElement extends SSMLElement {
      private text: string = 'Hello & "world" <script>';

      render(): string {
      // Escapes to: Hello &amp; &quot;world&quot; &lt;script&gt;
      return `<text>${this.escapeXml(this.text)}</text>`;
      }
      }

      // Edge cases handled correctly
      this.escapeXml('5 < 10 & 10 > 5');
      // Returns: '5 &lt; 10 &amp; 10 &gt; 5'

      this.escapeXml('She said "Hello"');
      // Returns: 'She said &quot;Hello&quot;'

      this.escapeXml("It's a test");
      // Returns: 'It&apos;s a test'

      // Prevents XML injection
      this.escapeXml('</voice><voice name="evil">');
      // Returns: '&lt;/voice&gt;&lt;voice name=&quot;evil&quot;&gt;'
    • Renders the math element as an SSML XML string.

      Generates the <math> element with the MathML namespace and the provided MathML content. The namespace attribute is required for proper interpretation of the mathematical markup. The MathML content is inserted directly without escaping as it should already be valid XML markup.

      Returns string

      The XML string representation of the math element in the format: <math xmlns="http://www.w3.org/1998/Math/MathML">mathML content</math>

      // Simple expression
      const math1 = new MathElement('<mrow><mi>x</mi><mo>+</mo><mn>1</mn></mrow>');
      console.log(math1.render());
      // Output: <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>x</mi><mo>+</mo><mn>1</mn></mrow></math>

      // Fraction
      const math2 = new MathElement('<mfrac><mn>3</mn><mn>4</mn></mfrac>');
      console.log(math2.render());
      // Output: <math xmlns="http://www.w3.org/1998/Math/MathML"><mfrac><mn>3</mn><mn>4</mn></mfrac></math>

      // Complex expression with multiple elements
      const math3 = new MathElement(`
      <mrow>
      <mi>E</mi>
      <mo>=</mo>
      <mi>m</mi>
      <msup>
      <mi>c</mi>
      <mn>2</mn>
      </msup>
      </mrow>
      `);
      console.log(math3.render());
      // Output: <math xmlns="http://www.w3.org/1998/Math/MathML">
      // <mrow>
      // <mi>E</mi>
      // <mo>=</mo>
      // <mi>m</mi>
      // <msup>
      // <mi>c</mi>
      // <mn>2</mn>
      // </msup>
      // </mrow>
      // </math>