I'm looking for a way to print a function's arguments, not only unevaluated, but also in the way they were originally typed in.
By way of experimentation, I tried stuff like this:
echo[x___] := ToString[Unevaluated[x]];
SetAttributes[echo, HoldAll];
...but nothing quite worked. For example, with the above function, the expression
Print[TemplateApply[">``<", echo[2^2, "foo", 2 + 2, None]]]
outputs this to the screen:
> 2
Sequence[2 , foo, 2 + 2, None]<
Instead, I want the output to be this:
>2^2, "foo", 2 + 2, None<
IOW, I want the output of echo
to be a string that more closely represents the arguments as they were typed in to Mathematica1.
How can I achieve this?
1 The amount of whitespace between the arguments, however, is not too important. E.g., either 1 ,2,3
or1, 2, 3
would be OK as the value of echo[1 ,2,3]
. Similarly, preserving fancy typesetting is of secondary importance. The example above shows some of the deviations from perfect faithfulness that I'm most trying to avoid: the Sequence[...]
wrapper, the added second line to accommodate the superscript, and the omission of the double-quotes around strings.
2^2
as real superscript? I guess it should be printed then as real superscript and this is the point where it becomes hard to achieve what you like. Anyway, first you can tryToString[Unevaluated[x], InputForm]
but it will turn every fancy typesetting into usual code. $\endgroup$ – halirutan♦ May 18 '15 at 0:45Sequence[ ... ]
wrapper. BTW, getting 2^2 instead of 2<sup>2</sup> is acceptable. I'll edit my question to clarify this point. $\endgroup$ – kjo May 18 '15 at 1:28