6

I'm doing this:

enter image description here

And get this:

enter image description here

But what I wanted is:

enter image description here

How can I get the desired output? Is there a way to recover the format from the cryptic output?

Note: my "real" list is way longer than just two terms and I want to keep the "pretty" format in order to show the output.

  • 1
    This isn't general enough but maybe good enough for you so: ToString[#, StandardForm] & /@ ToExpression[ "{" <> # <> "}"] &@myString – Kuba Dec 28 '15 at 20:00
  • If you do FullForm on your string you will see the underlying plain text string contains several commas. StringSplit splits on those and so the result would be pretty hard to undo. – george2079 Dec 28 '15 at 20:08
  • 2
    Please post properly-formatted, copy-and-paste-able Mathematica code rather than screenshots of your code. It's nice to be able to just copy and paste code into our own copies of Mathematica instead of re-typing expressions the OP has already written. – march Dec 28 '15 at 20:12
  • @george2079, OK, but how can I get the result? Maybe with another approach – Alvaro Fuentes Dec 28 '15 at 20:12
  • @xndrme Try the code from my comment. – Kuba Dec 28 '15 at 20:14
  • @Kubas approach is best. Just for completness you can reassemble your formatted strings with care: (StringJoin[Riffle[#, ","]] & /@ Partition[StringSplit[#, ","], 2]) &@mystring. This is specialized to the case where each term has exactly one comma in its FullForm string. – george2079 Dec 28 '15 at 20:17
  • @Kuba your solution is working but not well enough, the problem is that X_u has an assigned value. Thus what I get is something like {"{-v Sin[u], v Cos[u], a}",... – Alvaro Fuentes Dec 28 '15 at 20:53
  • @george2079 It seens to be the correct solution, at least is working for me – Alvaro Fuentes Dec 28 '15 at 20:54
  • 1
    @march If you can show me how to show X_u with the pretty format by just copying the code I'll be glad to post a copy-paste version. – Alvaro Fuentes Dec 28 '15 at 20:55
  • @xndrme. In general, that comment I left is for the future: in this particular case, the code is simple enough that it's not really a problem. That said, if you had copied and pasted, you would've have seen that the subscripted symbol would paste as Subscript[X, y] or as a long mess of SubscriptBox's and \'s (depending on how you copy and past), which would actually have shown you immediately what the problem is. That's sort of the point. (It's still a good question, though, because this stuff is not obvious.) – march Dec 28 '15 at 20:59
  • @march OK, fair enough for me, I'm still learning the way of Mathematica. – Alvaro Fuentes Dec 28 '15 at 21:02

2 Answers2

8

This might work for you. The idea is to create a PatternTest function which only returns True outside of box structures.

mysplit[s_String, c_String] := Module[{f, i = 0},
  f["\("] := i++;
  f["\)"] := i--;
  f[c] := i == 0;
  StringSplit[s, _?f]]

enter image description here

Simon Woods
  • 84,945
  • 8
  • 175
  • 324
3

a bit of an overkill answer:

start with a somewhat more involved example. The string is keyed in using control-_ for subscripts etc.

enter image description here

{"Y", "!(*SubscriptBox[(X)", " (u)])", \ "!(*SubscriptBox[(X)", " (p)])", "X", \ "!(*TagBox[SubsuperscriptBox[\"x\"", " \"0\"", " \"n\"]", " DisplayForm])", "p"}

this is a mess because StringSplit has operated on all the commas embedded in the format code.

so.. reassemble by joining parts until they are valid strings. (see https://mathematica.stackexchange.com/a/102969/2079 )

validstring[s_String] := 
 Nand[StringMatchQ[s, ___ ~~ "\!\(" ~~ ___], ToString[s] === s ]
Reap[last = Fold[ If[validstring[#1], (Sow[#1]; #2),
      p = StringJoin[#1, ",", #2]] & , First@badsplit , 
    Rest@badsplit]; If[validstring[last], Sow[last]]][[2, 1]]

enter image description here

Of course the best approach is to not use StringSplit in the first place. ( See comments )

george2079
  • 38,913
  • 1
  • 43
  • 110