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

MathML to/from Plain Text Converter

Rate me:
Please Sign up or sign in to vote.
4.98/5 (23 votes)
15 Sep 2023MIT1 min read 33.5K   1.6K   25   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.

Sample Image

Introduction

There seems 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 substraction, but T calls first F for any multiplication or substraction, 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

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

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