1
$\begingroup$

I'm trying to access lines of code from another Mathematica notebook using NotebookImport

NotebookImport[FileNameJoin@{NotebookDirectory[], "QA.nb"}, _]

Stuff in Notebook that I'm importing from: F[x_]:=x^Sqrt[2]+1 Trigonometry

My Output is :

{Cell[BoxData[
   RowBox[{RowBox[{"F", "[", "x_", "]"}], ":=", 
     RowBox[{SuperscriptBox["x", SqrtBox["2"]], "+", "1"}]}]], "Code",
   CellChangeTimes -> {{3.79913*10^9, 3.79913*10^9}}, 
  CellLabel -> "In[22]:=", 
  ExpressionUUID -> 
   "8583411c-4a4d-4f21-86d8-ebfb497b6dfc"], "Trigonometry", 
 HoldComplete[Null]}

Is there any command that would display simply the line of code or is there any way of simplifying the output to the original line of code?

$\endgroup$
1
$\begingroup$

Suppose you assign your Output to c, then

Cases[Cases[c, e_Cell :> MakeExpression[e, StandardForm]], 
  ExpressionCell[line_, "Code", ___] :> 
   ToString[Unevaluated@line, InputForm], -1] // First

returns

"F[x_] := x^Sqrt[2] + 1"
$\endgroup$

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.