# Is there a way to convert Wolfram Language expression to string?

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),

If[Length[$ScriptCommandLine]==1, 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}] ]  into "If[Length[$ScriptCommandLine]==1,
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.

• ToString does this. – Roman Apr 22 at 15:58
• Something like this? StringReplace[ToString[Defer[2 + 2]], "Defer[" ~~ x___ ~~ "]" :> x] – user6014 Apr 22 at 16:01
• 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? – Somos Apr 22 at 16:08
• Right click on the cell, Copy As -> Plain Text – 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:

MathLinkCallFrontEnd[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

• Thank it works but it seems to be adding extra spaces and newlines where they were not in the original input cell. – user13892 Apr 22 at 16:33
• 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 ]" – user13892 Apr 22 at 16:33 • 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]" – user13892 Apr 22 at 16:35
• 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. – user13892 Apr 22 at 16:42
• @user13892 the issue is the PageWidth` setting. Try stuff with that. – b3m2a1 Apr 22 at 17:16