Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

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.