1
$\begingroup$

I have an output cell and want to see the input that led to that output. How do I tell Mathematica to display that input?

$\endgroup$
4
  • $\begingroup$ Do you mean, you have an output cell, but the associated input cell has been deleted, and you want to know what that input cell was? $\endgroup$
    – Carl Woll
    Oct 6, 2018 at 15:33
  • 1
    $\begingroup$ does ??In give anything useful? $\endgroup$
    – kglr
    Oct 6, 2018 at 15:49
  • $\begingroup$ ... or DownValues[In]? $\endgroup$
    – kglr
    Oct 6, 2018 at 16:45
  • $\begingroup$ @CarlWoll, that can be one scenario where this function can be used. kglr i tried both but they are not the functions I am looking for. $\endgroup$ Oct 7, 2018 at 14:58

1 Answer 1

1
$\begingroup$

Here are 2 ways to retrieve the input cell contents corresponding to an output cell. First, if the kernel that created the cell is still active, you can use InString (In is also possible):

ToExpression[
    ToExpression[InString[105]],
    StandardForm,
    Defer
]

TensorTranspose[{{P, P}}, {1, 2, 4, 3}]

This will work even if the cell has been deleted (which it has in this notebook).

On the other hand, if the kernel that created the output is no longer active, but the cell that created the output has not been deleted, you can use Cells and CurrentValue:

NotebookGet @ Pick[
    Cells[],
    CurrentValue[Cells[], CellLabel],
    "In[200]:="
];

%/2 Pi

% // FullForm

CurrentValue[$FrontEnd, "Default2DTool"]

As you can see, this notebook has evaluated In[200] several times.

$\endgroup$
3
  • $\begingroup$ why not ToExpression[InString[200], StandardForm, Defer]? $\endgroup$
    – kglr
    Oct 7, 2018 at 17:37
  • $\begingroup$ @kglr Depends on what version of Mathematica you have. Sometime between 10.0 and 10.3 the behavior of InString changed. Your suggestion works before this change, but not after. $\endgroup$
    – Carl Woll
    Oct 7, 2018 at 17:42
  • $\begingroup$ thanks @Carl. (v9 here) $\endgroup$
    – kglr
    Oct 7, 2018 at 17:45

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.