Obtain input expression from history without evaluating
In addition to assigning to In
, the Mathematica main loop assigns the input to InString
before it is parsed as an expression. You can then retrieve InString[1]
and parse the result with ToExpression
, wrapping it in Defer
to prevent it from evaluating immediately:
In[5]:= ToExpression[InString[1], StandardForm, Defer]
Out[5]= Round[SessionTime[]]
You can then edit and evaluate the resulting expression.
If you want to process the output programmatically, it will be more convenient to use Hold
in place of Defer
.
Also, note that InString
is listable, so if you're not sure of the exact input number you can pass a range, like InString[Range[10, 20]]
.
The input is stored as a downvalue in the definition of In
. If you've only typed a few expressions, run
Definition@In
which prints a list of all the values associated with the symbol In
, and copy from the line In[1] := Round[SessionTime[]]
.
But if you've typed hundreds of expressions, or expressions whose plain-text representations are long (like images), this is hard to use. To extract only a specific input number, use pattern matching:
In[4]:= Hold[In[1]] /. DownValues[In]
Out[4]= Hold[Round[SessionTime[]]]
Both methods of Mechanical snail work but with some side effects: the DownValues
method will alter the original input string a little, and the ToExpression
method as is will produce a large number of RowBox
es. A simplified method example is:
7/29/18 11:39:33 In[259]:= InString[219] // ToExpression // DisplayForm
7/29/18 11:39:33 Out[259]//DisplayForm= FindMinimum[(Log@[email protected])^2+(Log@[email protected])^2,#/.Rule->List,MaxIterations->5000]&@{q->0.1,k->0.1,l->0.3}