Search code examples
javascriptmathmathmlmathematical-notation

"simple math syntax" to MathML converter


I'd like something that converts a simple calculator like ascii math syntax to mathML.

I found this: http://www1.chapman.edu/~jipsen/mathml/asciimath.html

But I don't understand how to use it.. I'd like to make it work from the command line for example, so that I feed it some math formula and get back the mathMl version. How could I do it? Is there any other program like this, maybe in a less browser oriented language than javascript?


Solution

  • Perl has Text::ASCIIMathML, which works quite well.

    Adapted from the Synopsys section:

    #!/usr/bin/perl
    
    use strict;
    use warning;
    use Text::ASCIIMathML;
    
    my $parser = Text::ASCIIMathML->new;
    
    my $ASCIIMathML = "int_0^1 e^x dx";
    
    print $parser->TextToMathML($ASCIIMathML);
    

    gives (reformatted for legibility):

    <math>
      <mstyle>
        <mrow><msubsup><mo>&#x222B;</mo><mn>0</mn><mn>1</mn></msubsup></mrow>
        <msup><mi>e</mi><mi>x</mi></msup>
        <mrow><mi>d</mi><mi>x</mi></mrow>
      </mstyle>
    </math>