Mathematica: How to prevent evaluation of In[-1]?

爱⌒轻易说出口 提交于 2019-12-05 13:35:07

You could try InString[]

In[1]:= 2 + 2
Out[1]= 4

In[2]:= InString[-1]
Out[2]= "\\(2 + 2\\)"
Alexey Popkov

Here is another way:

In[1]:= 2 + 2
With[{line = $Line - 1}, HoldForm[In[line]] /. DownValues[In]]

Out[1]= 4

Out[2]= 2+2

Edit:

This method does not work properly with Unevaluated:

In[1]:= Unevaluated[2 + 2]
With[{line = $Line - 1}, HoldForm[In[line]]] /. (DownValues[In])
Out[1]= Unevaluated[2 + 2]
Out[2]= 2 + 2

I have opened a separate question on this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!