# How to get the outermost command evaluated first?

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}]]]

• Could you give a small working example? – Marius Ladegård Meyer Nov 17 '14 at 13:02
• @MariusLadegårdMeyer Sure, I'll add a little toy piece of the code I'm trying to get working to the main text. – 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}]]" *)

• Works like a charm! Thanks :-) – quantum_loser Nov 17 '14 at 13:54
• @quantum_loser, my pleasure. Thanks for the accept. – 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 ;-)

• How does this differ from what you wrote in your question? I get "{(-1,-1), (-1,1), (1,-1), (1,1)}" in both cases... – Marius Ladegård Meyer Nov 17 '14 at 13:48
• @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. – quantum_loser Nov 17 '14 at 13:51
• Same here, MMA 10 on OS X 10.9. Btw, is the backslash after & in the last expression a typo? I assumed it was. – Marius Ladegård Meyer Nov 17 '14 at 14:19
• @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. – quantum_loser Nov 17 '14 at 14:23