# ToExpression Error when parsing MathML in XML

My code for parsing MathML to be readable in Mathematica:

ToExpression @ ImportString @ ExportString[#, "XML"]& /@ Cases[
xml, XMLElement["math", _, _],∞
]


The MathML contents:

<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<ci> MM_e</ci>
<ci> mem </ci>
<ci> k </ci>
<ci> sp_3 </ci>
<ci> sp_1 </ci>
<ci> Km </ci>
</apply>
[/itex]


I am getting this error when extracting MathML from a XML file. :

ToExpression::esntx: Could not parse FormBox[TagBox[RowBox[{MM_e,RowBox[{(,RowBox[{<<9>>}],)}]}],MathMLContentTag,AutoDelete->True],TraditionalForm] as input.

The math should display like this:

MM_e(mem,k,sp_3,sp_1,Km)


But it only displays somewhat correctly when I copy MathML content to Mathematica which then asks

"Do you want to paste the typeset expression?"

MM_e(mem,k,3 sp_,1 sp_,Km)


Although when using DisplayForm there are no errors but with other math content, it does not simplify unless I use Shift+Enter on the output.

DisplayForm @ ImportString @ ExportString[#, "XML"] & /@ Cases[
xml, XMLElement["math", _, _], ∞
]


How can I fix this error without using DisplayForm?

• There is no expression behind this so keeping it in boxes makes more sense. Why isn't DisplayForm ok? You can replace it with RawBoxes but that does not change much. – Kuba Jan 16 '18 at 23:14
• DisplayForm does not simplify unless I use Shift+Enter on the output. RawBoxes gives me other errors with other examples. – MathProtein Jan 17 '18 at 0:04
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Jan 17 '18 at 0:34
• What do you mean by simplify of MM_e(mem,k,3 sp_,1 sp_,Km) which is not a valid Mathematica syntax? – Kuba Jan 17 '18 at 17:27

## 1 Answer

The problem was with the underscore symbol. I fixed it by changing it to another symbol [LetterSpace] and Math displays perfectly.