I have an expression like ToString[Stuff[...]]. I plan on having the Stuff[...] be manipulated as a string according to the output of other parts of the program. The problem is that Mathematica tries to evaluate the Stuff[...] first and since the expression is not yet complete it just returns an error.

I can't just use "Stuff[...]" because there are actually other incomplete ToString[...] expressions inside Stuff[...] and Mathematica interprets the nested "A "B" C" as two separate strings with some evaluable expression in the middle.

Thanks in advance for any help!

Edit: Here's a little piece of toy code I'm trying to get working:

ToString[Flatten[ Array[StringJoin["(", ToString[(-1)^#1], ",", ToString[(-1)^#2], ")"] &, {2, 2}]]]

  • $\begingroup$ Could you give a small working example? $\endgroup$ – Marius Ladegård Meyer Nov 17 '14 at 13:02
  • $\begingroup$ @MariusLadegårdMeyer Sure, I'll add a little toy piece of the code I'm trying to get working to the main text. $\endgroup$ – quantum_loser Nov 17 '14 at 13:03

Using Defer or HoldForm in the first argument and StandardForm in the last argument of the outer ToString:

ToString[Defer@Flatten[Array[StringJoin["(", ToString[(-1)^#1], ",",
                       ToString[(-1)^#2], ")"] &, {2, 2}]], StandardForm]
(*  "Flatten[Array["("<>ToString[(-1)^#1]<>","<>ToString[(-1)^#2]<>")"&,{2,2}]]" *)
| improve this answer | |
  • $\begingroup$ Works like a charm! Thanks :-) $\endgroup$ – quantum_loser Nov 17 '14 at 13:54
  • $\begingroup$ @quantum_loser, my pleasure. Thanks for the accept. $\endgroup$ – kglr Nov 17 '14 at 13:58

In case anyone else is interested I've come up with a rather inelegant solution. That is replacing the inner-most strings with per-defined definitions of those strings, therefore avoiding the problem of the nested quotation marks. In the example I gave the solution would be

P1 = "(";

P2 = ")";

c = ",";

"Flatten[Array[StringJoin[P1,ToString[(-1)^#1],c,ToString[(-1)^#2],P2]&\ ,{2,2}]]"

Would still love to see a more general solution to the problem if anyone can find one ;-)

| improve this answer | |
  • $\begingroup$ How does this differ from what you wrote in your question? I get "{(-1,-1), (-1,1), (1,-1), (1,1)}" in both cases... $\endgroup$ – Marius Ladegård Meyer Nov 17 '14 at 13:48
  • $\begingroup$ @MariusLadegårdMeyer That's odd, because I don't. I get the string I want. What version of Mathematica are you running? I am running 10. $\endgroup$ – quantum_loser Nov 17 '14 at 13:51
  • $\begingroup$ Same here, MMA 10 on OS X 10.9. Btw, is the backslash after & in the last expression a typo? I assumed it was. $\endgroup$ – Marius Ladegård Meyer Nov 17 '14 at 14:19
  • $\begingroup$ @MariusLadegårdMeyer Yes, it's a typo. Very odd we don't get the same thing. Only difference I can think of is that I'm running the windows version. $\endgroup$ – quantum_loser Nov 17 '14 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.