Skip to content

nfejzic/alemat

Repository files navigation

Build CI Documentation Crates

alemat - MathML Builder

What is "alemat"?

Al-Alemat is arabic for tags. MathML is markup language that uses tags (similar to other markup languages such as XML or HTML) to build mathematic notation.

What is alemat for?

alemat is a Rust crate for building MathMl documents. The goal is to provide type-safe and ergonomic API for building and rendering MathMl elements.

Examples:

In general, you can check out the tests/ directory for API examples and tests/snapshots/ directory for the rendered output.

Here are some of the examples:

let output = MathMl::with_content(
    Radical::builder()
        .index("2")
        .content(alemat::children![
            Num::from(1),
            Operator::from("+"),
            SubSup::builder()
                .base(Ident::from("n"))
                .subscript(Num::from(2))
                .supscript(Num::from(3))
                .build(),
        ])
        .build(),
)
.render();

This is rendered out to:

<math>
  <msqrt>
    <mn>
      1
    </mn>
    <mo>
      +
    </mo>
    <msubsup>
      <mi>
        n
      </mi>
      <mn>
        2
      </mn>
      <mn>
        3
      </mn>
    </msubsup>
  </msqrt>
</math>

which looks like this:

$\sqrt[2]{1 + n_{2}^{3}}$

The crate exposes some macros for better ergonomics when building elements. For example the children! macro can combine arbitrary elements into a single array. Internally, this is done by converting each element into the Element enum, and storing that in the list.

There's the row! macro for building an mrow of elements. And there are also macros for creating a table_row! and table!. For example:

let out = MathMl::with_content(alemat::children![
    Frac::builder()
        .num(Ident::from("A"))
        .denom(Num::from(2))
        .build(),
    Operator::eq(),
    alemat::row![
        Operator::lparens(),
        alemat::table![
            [Num::from(1), Num::from(2), Num::from(3)],
            [Num::from(4), Num::from(5), Num::from(6)],
            [Num::from(7), Num::from(8), Num::from(9)],
        ],
        Operator::rparens(),
    ]
])
.render();

This generates the MathMl for a matrix:

<math>
  <mfrac>
    <mi>
      A
    </mi>
    <mn>
      2
    </mn>
  </mfrac>
  <mo>
    =
  </mo>
  <mrow>
    <mo>
      (
    </mo>
    <mtable>
      <mtr>
        <mtd>
          <mn>
            1
          </mn>
        </mtd>
        <mtd>
          <mn>
            2
          </mn>
        </mtd>
        <mtd>
          <mn>
            3
          </mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>
            4
          </mn>
        </mtd>
        <mtd>
          <mn>
            5
          </mn>
        </mtd>
        <mtd>
          <mn>
            6
          </mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>
            7
          </mn>
        </mtd>
        <mtd>
          <mn>
            8
          </mn>
        </mtd>
        <mtd>
          <mn>
            9
          </mn>
        </mtd>
      </mtr>
    </mtable>
    <mo>
      )
    </mo>
  </mrow>
</math>

which looks like this:

$A = \begin{pmatrix} 1 &amp; 2 &amp; 3\\ 4 &amp; 5 &amp; 6\\ 7 &amp; 8 &amp; 9\\ \end{pmatrix}$

License

This project is licensed under the Apache 2.0 license. See the LICENSE file for more details.