2
$\begingroup$

To begin, this is my first post on stack exchange, so if my question is not up to par please let me know so I can make the appropriate edits. Now for my question: why is it that parsing a string in StandardForm often gives strange results? How can this be avoided? Take the following case. Consider some quantity

 M= X[{2,0},{2,0}]+X[{3,0},{1,0}]

I want to convert it to a string in StandardForm and then parse the result by splitting at the plus sign. First, the obvious result without StandardForm comes from

Str[F_]:=ToString[F]
StringSplit[Str[M],"+"]

and is

{"X[{2, 0}, {2, 0}] ", " X[{3, 0}, {1, 0}]"}

Great. But now I impose standard form,

Str[F_]:=ToString[F,StandardForm]
StringSplit[Str[M],"+"]

and suddenly the output is

      {"\!\(\(\(X[\(\(\(\({2, 0}\)\), \(\({2, 0}\)\)\)\)]\)\) ", " \(\(X[\(\
\(\(\({3, 0}\)\), \(\({1, 0}\)\)\)\)]\)\)\)"}

Why is this happening and how can I avoid it? For various reasons, it is really a necessity for me to use StandardForm here, so I can't just leave that out.

Thanks!

$\endgroup$

closed as off-topic by m_goldberg, user9660, MarcoB, RunnyKine, Alexey Popkov May 2 '16 at 15:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, Community, MarcoB, RunnyKine, Alexey Popkov
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    $\begingroup$ The extra "\"s are from the string representation of the boxes. Why not splitting the expression and then convert to standard form? For example ToString[#, StandardForm] & /@ (M /. Plus -> List) $\endgroup$ – xslittlegrass May 1 '16 at 5:09
  • $\begingroup$ I was quite frankly not even aware of the concept of boxes until I read your comment - thank you for the help! At the risk of being very off topic, could I ask another question related to this project? How do I stop mathematica from combining terms in expressions into powers - ie I want things to stay in the form X[...]X[...] instead of (X[...])^2, both during evaluation and at output. Thanks again! $\endgroup$ – physicist-trying-to-program May 1 '16 at 23:01
  • $\begingroup$ Use HoldForm for this: HoldForm[x[1] x[1]] (but it is off-topic, of course). $\endgroup$ – Alexey Popkov May 2 '16 at 15:40