0
$\begingroup$

I entered the following code into the Input cell. And the code expressed as RowBox was obtained using the Show Expression menu.

enter image description here

I want to get a Plain Text in the form of InputForm again from this code.

expr = Cell[BoxData[RowBox[{"Plot", "[", 
RowBox[{RowBox[{RowBox[{"Sin", "[", SuperscriptBox["x", "2"], "]"}], "+", 
SqrtBox[FractionBox["x", "2"]]}], ",", RowBox[{"{", RowBox[{"x", ",", "0", ",", "3"}], 
"}"}]}], "]"}]], "Input"]

How do I write code to get Plain Text in InputForm format? The text I want to get is as follows.

In[1]:= text = ... Code using 'expr' ...
Out[1]= Plot[Sin[x^2] + Sqrt[x/2], {x, 0, 3}]

I will continue my next work using the results obtained like this (=text).

$\endgroup$
4
  • 1
    $\begingroup$ Does this fit your needs 1411? So First[FrontEndExecute[FrontEnd`ExportPacket[First@NotebookRead[PreviousCell[]], "PlainText"]]] $\endgroup$
    – Kuba
    Commented Nov 13, 2023 at 7:36
  • $\begingroup$ In fact, the problem I am finally trying to solve is as follows. mathematica.stackexchange.com/questions/292526 $\endgroup$
    – Milk
    Commented Nov 13, 2023 at 7:43
  • $\begingroup$ Oh, thank you. I see a clue to the solution! I think I can solve the rest!! $\endgroup$
    – Milk
    Commented Nov 13, 2023 at 7:57
  • $\begingroup$ Eliminating the Cell/BoxData wrappers, you can use ToExpression, e.g., ToExpression[expr[[1,1]], StandardForm, Defer] $\endgroup$
    – Carl Woll
    Commented Nov 13, 2023 at 22:14

0

Browse other questions tagged or ask your own question.