DEV Community

Cover image for Mastering JavaScript MathML: Writing Mathematics with MathML
Dharmendra Kumar
Dharmendra Kumar

Posted on

2 1 1 1 1

Mastering JavaScript MathML: Writing Mathematics with MathML

Mathematical Markup Language (MathML) allows you to write complex mathematical expressions in web pages seamlessly. This guide will take you through the essential topics to get started with MathML and explore its various elements.

MathML First Steps

What is MathML?

  • Definition: MathML (Mathematical Markup Language) is an XML-based markup language designed to display mathematical notations.
  • Purpose: It allows browsers to render mathematical expressions and provides a way to include math in web pages.

Basic Structure

  • MathML Tags: <math>, <mrow>, <mi>, <mo>, <mn>.
  • Example:

    <math>
        <mrow>
            <mi>x</mi>
            <mo>=</mo>
            <mn>5</mn>
        </mrow>
    </math>
    

Getting Started with MathML

Setting Up MathML

  • Integration: MathML can be embedded directly in HTML using the <math> tag.
  • Browser Support: Most modern browsers support MathML, but for complete compatibility, consider using MathJax.

Simple Expression

  • Inline Math: Adding inline mathematical notation.

    <p>The equation is <math><mrow><mi>x</mi><mo>=</mo><mn>5</mn></mrow></math>.</p>
    

MathML Text Containers

Using <mrow> and <mfrac>

  • Grouping: <mrow> groups multiple elements together.
  • Example:

    <math>
        <mrow>
            <mi>a</mi>
            <mo>+</mo>
            <mi>b</mi>
        </mrow>
    </math>
    

Fractions

  • Using <mfrac>: Represents fractions.
  • Example:

    <math>
        <mfrac>
            <mi>a</mi>
            <mi>b</mi>
        </mfrac>
    </math>
    

MathML Fractions and Roots

Fractions

  • Creating Fractions: Utilizing the <mfrac> tag.
  • Example:

    <math>
        <mfrac>
            <mi>1</mi>
            <mi>2</mi>
        </mfrac>
    </math>
    

Roots

  • Square Roots: Using <msqrt>.
  • Example:

    <math>
        <msqrt>
            <mi>x</mi>
        </msqrt>
    </math>
    

MathML Scripted Elements

Subscripts and Superscripts

  • Subscript (<msub>): Used for subscript notation.

    <math>
        <msub>
            <mi>x</mi>
            <mi>i</mi>
        </msub>
    </math>
    
  • Superscript (<msup>): Used for superscript notation.

    <math>
        <msup>
            <mi>x</mi>
            <mn>2</mn>
        </msup>
    </math>
    

Combined Scripts

  • Example:

    <math>
        <msubsup>
            <mi>x</mi>
            <mi>i</mi>
            <mn>2</mn>
        </msubsup>
    </math>
    

MathML Tables

Creating Matrices

  • Using <mtable>: Represents matrices and tables.
  • Example:

    <math>
        <mtable>
            <mtr>
                <mtd><mi>a</mi></mtd>
                <mtd><mi>b</mi></mtd>
            </mtr>
            <mtr>
                <mtd><mi>c</mi></mtd>
                <mtd><mi>d</mi></mtd>
            </mtr>
        </mtable>
    </math>
    

Three Famous Mathematical Formulas

Quadratic Formula

  • Expression:

    <math>
        <mi>x</mi>
        <mo>=</mo>
        <mfrac>
            <mrow>
                <mo>-</mo>
                <mi>b</mi>
                <mo>&#xB1;</mo>
                <msqrt>
                    <msup>
                        <mi>b</mi>
                        <mn>2</mn>
                    </msup>
                    <mo>-</mo>
                    <mn>4</mn>
                    <mi>a</mi>
                    <mi>c</mi>
                </msqrt>
            </mrow>
            <mrow>
                <mn>2</mn>
                <mi>a</mi>
            </mrow>
        </mfrac>
    </math>
    

Pythagorean Theorem

  • Expression:

    <math>
        <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>
    </math>
    

Euler's Formula

  • Expression:

    <math>
        <mi>e</mi>
        <msup>
            <mi>i</mi>
            <mi>&pi;</mi>
        </msup>
        <mo>+</mo>
        <mn>1</mn>
        <mo>=</mo>
        <mn>0</mn>
    </math>
    

Additional Topics

MathML Operators

  • Arithmetic Operators: +, -, *, /.
  • Relational Operators: =, >, <.
  • Example:

    <math>
        <mi>a</mi>
        <mo>+</mo>
        <mi>b</mi>
        <mo>=</mo>
        <mi>c</mi>
    </math>
    

MathML Accents

  • Overlines and Underlines: Using <mover> and <munder>.
  • Example:

    <math>
        <mover>
            <mi>x</mi>
            <mo>¯</mo>
        </mover>
    </math>
    

MathML Integrals and Limits

  • Integrals: Using <msubsup> and <mo>.

    <math>
        <msubsup>
            <mo>&#x222B;</mo>
            <mn>0</mn>
            <mi></mi>
        </msubsup>
        <mi>f</mi>
        <mo>(</mo>
        <mi>x</mi>
        <mo>)</mo>
        <mi>d</mi>
        <mi>x</mi>
    </math>
    
  • Limits: Using <mo> and <munder>.

    <math>
        <munder>
            <mo>lim</mo>
            <mrow>
                <mi>x</mi>
                <mo></mo>
                <mn>0</mn>
            </mrow>
        </munder>
        <mrow>
            <mi>f</mi>
            <mo>(</mo>
            <mi>x</mi>
            <mo>)</mo>
        </mrow>
    </math>
    

Conclusion

MathML is a powerful tool for embedding mathematical notation in web pages, providing a wide range of elements and functionalities. From basic expressions to complex equations and matrices, MathML ensures your mathematical content is displayed accurately and beautifully.

Start experimenting with MathML in your projects today, and bring the world of mathematics to your web pages with ease!

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay