Saurabh Palatkar
Saurabh Palatkar

Reputation: 3384

Math expressions to MathML

I am trying to build functionality as on this site where a user will enter a math expression in the text area and it will be rendered as MathML format.

Example:

Input expression string: cos(x^3)

Then the expression should be converted to MathML as:

<math xmlns='http://www.w3.org/1998/Math/MathML'>
    <mrow>
        <mi>cos</mi>
        <mo>&#8289;</mo>
        <mo>(</mo>
        <msup>
            <mi>x</mi>
            <mn>3</mn>
        </msup>
        <mo>)</mo>
    </mrow>
</math>

What is a C# solution?

Upvotes: 4

Views: 1112

Answers (1)

pln
pln

Reputation: 1198

I created a basic expression to MathML parser from a parser I had since earlier. You can download or fork the result here at BitBucket. (download link to the left).

Use the ToMathML(expression) method of the Parser class to convert the expression to MathML.

It also comes with a command line test program for testing the parser, the command for generating MathML is ml:

calc ~:> ml cos(x^3)
<math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>cos</mi><mrow><mrow><mo>(</mo><mrow><msup><mi>x</mi><mn>3</mn></msup></mrow><mo>)</mo></mrow></mrow></mrow></math>
calc ~:> ml 1/(x-1)
<math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mfrac><mn>1</mn><mrow><mrow><mo>(</mo><mrow><mi>x</mi><mo>-</mo><mn>1</mn></mrow><mo>)</mo></mrow></mrow></mfrac></mrow></math>
calc ~:>

There is no implied multiplication in the parser so you have to always use * in expressions (like 2*x).

Hopefully it is useful for your scenario. I guess it depends on how full featured you need it to be. In it's current form the parser will convert the expression to a pretty basic subset of MathML.

Feel free to fork it and continue working on it.

Upvotes: 4

Related Questions