Click here to Skip to main content
15,881,812 members
Articles / Web Development / HTML
Tip/Trick

MathML to/from Plain Text Converter

Rate me:
Please Sign up or sign in to vote.
4.99/5 (27 votes)
17 Apr 2024MIT2 min read 57.1K   2.3K   39   21
Converts MathML coded string to/from plain text string
The code consists of two classes in order to parse the MathML string. The first class, MathToString prepares the string. The second class, ParseML, does the parsing. Similarly, to get MathML code from text one class prepares and the other does the conversion.

Image 1

Introduction

There seem to be few resources converting MathML code into plain text. A good reason is there is no consensus in how to format some math expressions. But for many expressions involving (+,-,/,*,^,=) operators, here is one possible converter.

The Classes Engaged

  • Class MathToString prepares the string with the MathML code so that class ParseML can perform the detailed parsing.

Preparation

First, the spaces are replaced by empty strings and some special characters are replaced too. Also, some tags not involved in the math expression, like style tags, are removed. Then the code goes over from the most insider <mfrac>...</mfrac>, <msup>...</msup>, <mrow>...</mrow>, <msqrt>...</msqrt> tags to the most outer, being parsed and replaced, enclosing them in between special characters so that later they can be recovered by ParseML class.

Using the Code

To convert, just call the shared method MathToString.convertToString():

VB.NET
Dim converted as String = MathMLToString.convertToString(MathMLcodeToConvert)

To convert text to MathML, call convertStringToMathML().

Basic Principles

The parsing method is a recursive-descent parsing: Parsing Expressions by Recursive Descent.

Evaluation method E calls T for any addition or subtraction, but T calls first F for any multiplication or subtraction, and F calls first P for any power possible power operation. P calls first v to get next token. If there is a "(" token, v calls recursively to T.

E --> T {( "+" | "-" ) T}
T --> F {( "*" | "/" ) F}
F --> P ["^" F]
P --> v | "(" E ")" | "-" T

History

  • 12th May, 2022: Initial version

Version 1.0.3.0

Now, demo zip file contains a setup file.

JavaScript, .NET, and Core applications have been re-coded, because MathML is an application of XML and to reflect tags can be 'recursively' nested, I mean, an element can appear inside itself or any level below its children elements. For example:

XML
<mfrac>
   <mrow>
     <mi>-1</mi>
   </mrow>
   <mn>
     <mn>3</mn>
   </mn>
</mfrac>

 

Version 2.0.0.0 (2024-03-25)

The code has been rewritten, improved and expanded. For example, it now supports including equations in LaTex language such as the following:

latex
\begin{array}{*{20}c} {x = \frac{{ - b \pm \sqrt {b^2 - 4ac} }}{{2a}}} &
{{\text{when}}} & {ax^2 + bx + c = 0} \\ \end{array}

To this end, by putting the equation in LaTex in the MathML textbox and clicking on 'Convert to Plain Text' the Latex code is replaced by its translation to MathML and converted to plain text.

 

Version 2.0.5.0 (2024-04-17)

Some more samples have been added.

Arranges have been made so that square and curly brackets are reflected in the conversion.

ConvertStringToMathML.EvaluateFromInnerToOuter() is a new method and, as its name indicates, it examines from most inner paired parentheses and/or brackets to most outer.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseThanks Pin
Amitav Mohanty 202215-Mar-24 16:10
Amitav Mohanty 202215-Mar-24 16:10 
GeneralRe: Thanks Pin
Xavier Junqué i de Fortuny16-Mar-24 11:32
mvaXavier Junqué i de Fortuny16-Mar-24 11:32 
QuestionMathML ... I know what it is and I'm seeing more of it these days. Pin
RedDk13-Mar-24 12:51
RedDk13-Mar-24 12:51 
AnswerRe: MathML ... I know what it is and I'm seeing more of it these days. Pin
Xavier Junqué i de Fortuny13-Mar-24 13:02
mvaXavier Junqué i de Fortuny13-Mar-24 13:02 
PraiseMathML para/do Plain Text Converter Pin
Member 78935317-Jun-22 7:16
Member 78935317-Jun-22 7:16 
QuestionUTN #28 compatibility? Pin
Doug Ewell6-Jun-22 8:56
Doug Ewell6-Jun-22 8:56 
AnswerRe: UTN #28 compatibility? Pin
Xavier Junqué i de Fortuny6-Jun-22 10:11
mvaXavier Junqué i de Fortuny6-Jun-22 10:11 
Questionhow do you display these formulas? Pin
Southmountain22-May-22 17:15
Southmountain22-May-22 17:15 
AnswerRe: how do you display these formulas? Pin
Xavier Junqué i de Fortuny22-May-22 20:25
mvaXavier Junqué i de Fortuny22-May-22 20:25 
GeneralRe: how do you display these formulas? Pin
Southmountain24-May-22 19:26
Southmountain24-May-22 19:26 
GeneralRe: how do you display these formulas? Pin
Xavier Junqué i de Fortuny25-May-22 23:08
mvaXavier Junqué i de Fortuny25-May-22 23:08 
PraiseGracias! Pin
hairy_kiwi16-May-22 2:55
hairy_kiwi16-May-22 2:55 
GeneralRe: Gracias! Pin
Xavier Junqué i de Fortuny16-May-22 6:52
mvaXavier Junqué i de Fortuny16-May-22 6:52 
GeneralRe: Gracias! Pin
hairy_kiwi16-May-22 13:38
hairy_kiwi16-May-22 13:38 
GeneralRe: Gracias! Pin
Xavier Junqué i de Fortuny17-May-22 10:43
mvaXavier Junqué i de Fortuny17-May-22 10:43 
PraiseRe: Gracias! Pin
hairy_kiwi17-May-22 10:59
hairy_kiwi17-May-22 10:59 
QuestionMessage Closed Pin
13-May-22 1:14
cns inflatable13-May-22 1:14 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA12-May-22 19:18
professionalȘtefan-Mihai MOGA12-May-22 19:18 
GeneralRe: My vote of 5 Pin
Xavier Junqué i de Fortuny13-May-22 2:54
mvaXavier Junqué i de Fortuny13-May-22 2:54 
Questioncan not download your source code Pin
Southmountain12-May-22 14:10
Southmountain12-May-22 14:10 
AnswerRe: can not download your source code Pin
Xavier Junqué i de Fortuny13-May-22 2:50
mvaXavier Junqué i de Fortuny13-May-22 2:50 
GeneralRe: can not download your source code Pin
Southmountain13-May-22 4:17
Southmountain13-May-22 4:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.