Click here to Skip to main content
15,847,653 members
Articles / Web Development / HTML

MathML to/from Plain Text Converter

Rate me:
Please Sign up or sign in to vote.
4.98/5 (23 votes)
20 Feb 2024MIT1 min read 43.5K   1.9K   31   17
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


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.


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():

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


  • 12th May, 2022: Initial version


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

PraiseMathML para/do Plain Text Converter Pin
Member 78935317-Jun-22 8:16
Member 78935317-Jun-22 8:16 
QuestionUTN #28 compatibility? Pin
Doug Ewell6-Jun-22 9:56
Doug Ewell6-Jun-22 9:56 
AnswerRe: UTN #28 compatibility? Pin
Xavier Junqué i de Fortuny6-Jun-22 11:11
mvaXavier Junqué i de Fortuny6-Jun-22 11:11 
Questionhow do you display these formulas? Pin
Southmountain22-May-22 18:15
Southmountain22-May-22 18:15 
AnswerRe: how do you display these formulas? Pin
Xavier Junqué i de Fortuny22-May-22 21:25
mvaXavier Junqué i de Fortuny22-May-22 21:25 
GeneralRe: how do you display these formulas? Pin
Southmountain24-May-22 20:26
Southmountain24-May-22 20:26 
GeneralRe: how do you display these formulas? Pin
Xavier Junqué i de Fortuny26-May-22 0:08
mvaXavier Junqué i de Fortuny26-May-22 0:08 
PraiseGracias! Pin
hairy_kiwi16-May-22 3:55
hairy_kiwi16-May-22 3:55 
Most definitely this is a much needed tool, thank you dear Sir!

Recently I needed to convert numerous large MathML(?) expressions – developed in Desmos, Geogebra and Wolfram web apps – to SageMath and JavaScript via a regex-capable text editor; it was a painfully slow task. I noticed Wolfram can ‘almost’ parse MathML direct to textual math code, however it deletes all multiplication operators (and manual re-insertion of those is almost impossible for large, multi-variable equations). I see your parser handles the multiplication(*) and, digging into the source code, the sqrt() operator – which immediately makes it very interesting and potentially, a highly useful tool for anyone experimenting in maths between such math web apps and programming code.

Unfortunately the VB executable is not so easily utilised in a Mac OSX environment – so I wonder how difficult it might be to create an equivalent parser for JavaScript and/or Python languages? Perhaps then, Wolfram / Desmos / Geogebra might be inspired to include and further develop it as a web-based parser for their own platforms; it seems like a natural tool to include on such web platforms.

In the meantime, congratulations and sincere thanks for all your effort you've put into creating this opensource project!

Hamish Mead
GeneralRe: Gracias! Pin
Xavier Junqué i de Fortuny16-May-22 7:52
mvaXavier Junqué i de Fortuny16-May-22 7:52 
GeneralRe: Gracias! Pin
hairy_kiwi16-May-22 14:38
hairy_kiwi16-May-22 14:38 
GeneralRe: Gracias! Pin
Xavier Junqué i de Fortuny17-May-22 11:43
mvaXavier Junqué i de Fortuny17-May-22 11:43 
PraiseRe: Gracias! Pin
hairy_kiwi17-May-22 11:59
hairy_kiwi17-May-22 11:59 
QuestionMessage Closed Pin
13-May-22 2:14
cns inflatable13-May-22 2:14 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA12-May-22 20:18
professionalȘtefan-Mihai MOGA12-May-22 20:18 
GeneralRe: My vote of 5 Pin
Xavier Junqué i de Fortuny13-May-22 3:54
mvaXavier Junqué i de Fortuny13-May-22 3:54 
Questioncan not download your source code Pin
Southmountain12-May-22 15:10
Southmountain12-May-22 15:10 
AnswerRe: can not download your source code Pin
Xavier Junqué i de Fortuny13-May-22 3:50
mvaXavier Junqué i de Fortuny13-May-22 3:50 
GeneralRe: can not download your source code Pin
Southmountain13-May-22 5:17
Southmountain13-May-22 5: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.