2
$\begingroup$

If a cell exists as below:

In[1] := (xyPoints = {{1,2},{2,5},{3,10}}; ListLinePlot[xyPoints])

Is it possible to get at the expression rather than the plot shown in Out[1]. In other words, can I somehow get the held CompoundExpression in In[1] rather than the evaluated plot?

$\endgroup$
5
  • 1
    $\begingroup$ DownValues[In][[2]] $\endgroup$ Commented Aug 13, 2013 at 22:07
  • $\begingroup$ Awesome. Looks like I can use something like: DownValues[In][[1]] /. RuleDelayed[x_, y_] :> HoldComplete[y] $\endgroup$
    – teedr
    Commented Aug 13, 2013 at 22:17
  • $\begingroup$ Interestingly, this does not work if there is a newline after the semicolon. $\endgroup$
    – teedr
    Commented Aug 13, 2013 at 22:38
  • 1
    $\begingroup$ You could also use Information[In] or ToExpression[InString[1], StandardForm, Hold] $\endgroup$ Commented Aug 14, 2013 at 2:12
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/111963/… $\endgroup$ Commented Aug 8, 2019 at 22:49

2 Answers 2

5
$\begingroup$

You could use InString for that:

ToExpression[InString[1], StandardForm, Hold]

Hold[xyPoints = {{1, 2}, {2, 5}, {3, 10}}; ListLinePlot[xyPoints]]

$\endgroup$
3
  • $\begingroup$ Great. Do you know if there a way to get all the lines in a cell? If a compound expression is spread over multiple lines, each line seems to correspond to a different In[x] number. $\endgroup$
    – teedr
    Commented Aug 14, 2013 at 17:51
  • $\begingroup$ @teedr You could try to experiment with DownValues[InString] for that. $\endgroup$ Commented Aug 14, 2013 at 21:41
  • $\begingroup$ In addition to the problem with multiple lines, this answer will not retain all the 2D input formatting that your original cell might have had. That means you cannot use this answer to recover exactly what you had in an input cell that you, say, accidentally deleted. To recover everything, use Carl Woll's answer to a related question. $\endgroup$ Commented Aug 8, 2019 at 22:47
3
$\begingroup$
Cells[CellLabel -> "In[1]:="] // First // NotebookRead // 
  First // MakeExpression

gives

HoldComplete[xyPoints = {{1, 2}, {2, 5}, {3, 10}}; 
 ListLinePlot[xyPoints]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.