Click here to Skip to main content
15,355,301 members
Articles / Web Development / HTML
Tip/Trick
Posted 12 May 2022

Stats

18.1K views
901 downloads
21 bookmarked

MathML to/from Plain Text Converter

Rate me:
Please Sign up or sign in to vote.
4.98/5 (18 votes)
12 Jun 2022MIT1 min read
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

Share

About the Author

No Biography provided

Comments and Discussions

 
PraiseMathML para/do Plain Text Converter Pin
Member 78935317-Jun-22 7:16
MemberMember 78935317-Jun-22 7:16 
QuestionUTN #28 compatibility? Pin
Doug Ewell6-Jun-22 8:56
MemberDoug Ewell6-Jun-22 8:56 
AnswerRe: UTN #28 compatibility? Pin
Xavier Junqué i de Fortuny6-Jun-22 10:11
MemberXavier Junqué i de Fortuny6-Jun-22 10:11 
Questionhow do you display these formulas? Pin
Southmountain22-May-22 17:15
MemberSouthmountain22-May-22 17:15 
AnswerRe: how do you display these formulas? Pin
Xavier Junqué i de Fortuny22-May-22 20:25
MemberXavier Junqué i de Fortuny22-May-22 20:25 
GeneralRe: how do you display these formulas? Pin
Southmountain24-May-22 19:26
MemberSouthmountain24-May-22 19:26 
GeneralRe: how do you display these formulas? Pin
Xavier Junqué i de Fortuny25-May-22 23:08
MemberXavier Junqué i de Fortuny25-May-22 23:08 
PraiseGracias! Pin
hairy_kiwi16-May-22 2:55
Memberhairy_kiwi16-May-22 2:55 
GeneralRe: Gracias! Pin
Xavier Junqué i de Fortuny16-May-22 6:52
MemberXavier Junqué i de Fortuny16-May-22 6:52 
GeneralRe: Gracias! Pin
hairy_kiwi16-May-22 13:38
Memberhairy_kiwi16-May-22 13:38 
GeneralRe: Gracias! Pin
Xavier Junqué i de Fortuny17-May-22 10:43
MemberXavier Junqué i de Fortuny17-May-22 10:43 
PraiseRe: Gracias! Pin
hairy_kiwi17-May-22 10:59
Memberhairy_kiwi17-May-22 10:59 
QuestionMessage Closed Pin
13-May-22 1:14
Membercns 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
MemberXavier Junqué i de Fortuny13-May-22 2:54 
Questioncan not download your source code Pin
Southmountain12-May-22 14:10
MemberSouthmountain12-May-22 14:10 
AnswerRe: can not download your source code Pin
Xavier Junqué i de Fortuny13-May-22 2:50
MemberXavier Junqué i de Fortuny13-May-22 2:50 
GeneralRe: can not download your source code Pin
Southmountain13-May-22 4:17
MemberSouthmountain13-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.