Is there a way to convert a Wolfram Language expression / code into a string expression? For example,

Convert the following including its indentations (very important),

    Print["No expressions were given to square."],

    args = Rest[$ScriptCommandLine];
	squares = Quiet @ Map[Replace[ToExpression[#], {$Failed:>ToString[#,InputForm]^2, x_ :> x^2}]&, args];
    MapThread[Print["The square of ", #1, " is ", #2, "."]&, {args, squares}]


    Print[\"No expressions were given to square.\"],

    args = Rest[$ScriptCommandLine];
	squares = Quiet @ Map[Replace[ToExpression[#], {$Failed:>ToString[#,InputForm]^2, x_ :> x^2}]&, args];
    MapThread[Print[\"The square of \", #1, \" is \", #2, \".\"]&, {args, squares}]

I am looking for a builtin function or a function f that can be written to work like

f[...code...]...string version of code....

I am trying to do this programmatically and probably will require going into the Cell expression.

  • $\begingroup$ ToString does this. $\endgroup$ – Roman Apr 22 at 15:58
  • $\begingroup$ Something like this? StringReplace[ToString[Defer[2 + 2]], "Defer[" ~~ x___ ~~ "]" :> x] $\endgroup$ – user6014 Apr 22 at 16:01
  • $\begingroup$ Why exactly do you want to do this? What will you do with the string? Why is indentation important? The indentations are not part of the underlying Mathematica expression. You refer to Cell expression, but this only exists in the FrontEnd. What do you mean by that? $\endgroup$ – Somos Apr 22 at 16:08
  • $\begingroup$ Right click on the cell, Copy As -> Plain Text $\endgroup$ – Fortsaint May 1 at 17:18

In the Front End

I assume you have this typed in a Cell in the front-end. That's the only case in which preserving indentation makes sense at the parse step. If that's the case you need simply to get the Cell expression and pass it to:

MathLink`CallFrontEnd[ExportPacket[cellExpr, "InputText"]][[1]]

If you have the CellObject and need the Cell expression you do that via NotebookRead.

If you need the CellObject you'll probably find that easiest via NextCell and PreviousCell

Regenerating Indentation

If you don't have this in the FE and you'd like to regenerate indentation you can do that as I discuss here

  • $\begingroup$ Thank it works but it seems to be adding extra spaces and newlines where they were not in the original input cell. $\endgroup$ – user13892 Apr 22 at 16:33
  • $\begingroup$ This is the output it produced for me. Its not a big deal I can clean it up with some regex but if it can literally get the input without any modification that would be ideal. "If[Length[$ScriptCommandLine] == 1,\r\n \tPrint[\"No expressions were given to square.\"],\r\n \t\r\n \targs = Rest[$ScriptCommandLine];\r\n \tsquares = \r\n Quiet @ \ Map[\r\n Replace[ToExpression[#], {$Failed :> ToString[#, InputForm]^2, \r\n x_ :> x^2}] &, args];\r\n \tMapThread[\r\n Print[\"The square of \", #1, \" is \", #2, \ \".\"] &, {args, squares}]\r\n ]" $\endgroup$ – user13892 Apr 22 at 16:33
  • $\begingroup$ Compare this with the output I am expecting. "If[Length[$ScriptCommandLine]==1,\n\tPrint[\"No expressions were given to square.\"],\n\t\n\targs = Rest[$ScriptCommandLine];\n\tsquares = Quiet @ \ Map[Replace[ToExpression[#], {$Failed:>ToString[#,InputForm]^2, x_ :> x^2}]&, args];\n\tMapThread[Print[\"The square of \", #1, \" is \", #2, \".\"]&, {args, squares}]\n]" $\endgroup$ – user13892 Apr 22 at 16:35
  • $\begingroup$ Does ExportPacket have some options other than "InputText" that can get the input text without any interpretation or modification? These functions don't seem to be documented. $\endgroup$ – user13892 Apr 22 at 16:42
  • $\begingroup$ @user13892 the issue is the PageWidth setting. Try stuff with that. $\endgroup$ – b3m2a1 Apr 22 at 17:16

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.