Click here to Skip to main content
15,851,090 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 2024MIT2 min read 44.1K   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

Release Notes for Version

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:



  • 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 
Yes, in .NET code the display is through a WebView2 control and by making use of MathJax.[^] component. Basically, you have to insert this two scripts in the html document:

<script src=""></script>
<script id="MathJax-script" async="async" src=""></script>

and just add the MathML code in the document's body.

I forgot to say that if the page's content is dynamic and so changes the initial MathML code, to refresh the display of the formula, you should call in the javascript of the page 'MathJax.typeset();' (without the quotes).

modified 23-May-22 10:30am.

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 
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.