alemat

Crates.ioalemat
lib.rsalemat
version0.8.0
sourcesrc
created_at2023-11-25 23:55:28.444677
updated_at2023-12-26 19:00:51.12246
descriptionLibrary for type-safe building of MathML.
homepage
repositoryhttps://github.com/nfejzic/alemat
max_upload_size
id1048651
size215,859
Nadir Fejzić (nfejzic)

documentation

README

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 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9\\ \end{pmatrix}$

License

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

Commit count: 109

cargo fmt