# Getting the complete Cell expression some input would generate as output

I can think of calling Print and then SelectionMove to copy the expression and then delete it. Is there any more transparent way?

-

I believe that ToBoxes (or possibly MakeBoxes) does what you want. You would need to wrap the result in a valid Cell expression like this:

celldata = Cell[BoxData @ ToBoxes @ #, "Output"] &;

Sqrt[2^x] // celldata

Cell[BoxData[SqrtBox[SuperscriptBox["2", "x"]]], "Output"]

-
Nice one. Do you understand why the output is different for code such as celldata[TeXForm[2]]? – Rojolalalalalalalalalalalalala Mar 1 '12 at 1:14
@Rojo please, how is it different? – Mr.Wizard Mar 1 '12 at 1:29
Well, the actual cell output is Cell["2", "Output",..] and the reesult of the ToBoxes func is Cell[BoxData[ InterpretationBox["\"2\"", TeXForm[2], Editable -> True, AutoDelete -> True]], "Output"]. Other forms also get some extra boxes that don't appear in the real output – Rojolalalalalalalalalalalalala Mar 1 '12 at 4:38
@Rojo that appears to be a change from v7 to v8. What do you get with MakeBoxes rather than ToBoxes? – Mr.Wizard Mar 1 '12 at 4:57
The same, no difference – Rojolalalalalalalalalalalalala Mar 1 '12 at 5:31

If you want to get any cell as a Mathematica structure, not just one you recently created, you can use NotebookGet (or just plain Get for a notebook file you don't have open) and then pick out the particular cell you want using Cases, Part or Extract. The first is particularly useful if you want pick out a bunch of cells at once, based on some common attribute (like their cell style). I find this is generally much more convenient than trying to do stuff with SelectionMove and the like.

If you aren't trying to do this programmatically, and just want to see the expression for a specific cell, you can just select the cell, and go to the "Cell" menu, and select the "Show Cell Expression" option.

-