Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

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"]
share|improve this answer
Nice one. Do you understand why the output is different for code such as celldata[TeXForm[2]]? – Rojo 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 – Rojo 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 – Rojo 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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