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.

Consider the expression

TreeForm[a+b^2+c^3+d]

which displays as

enter image description here

This is a nice Graphics object, as I can prove by typing //FullForm after it, like this

enter image description here

and getting

Graphics[Annotation[
  GraphicsComplex[
   List[List[1.29777, 1.29777], List[0., 0.648886], 
   List[0.648886, 0.648886], List[0.324443, 0.], List[0.973329, 0.], 
   List[1.94 
... 
    List[Tooltip[
      Inset[Framed[Style[Plus, "StandardForm", "Output", Rule[LineColor, 

It has framed elements in it, as I can prove by either looking at the text above, or by typing

enter image description here

But, the TreeForm expression itself does not have any framed elements in it. For instance

TreeForm[a+b^2+c^3+d] /. (x_Framed :> Print[x])

produces the original graphic

Somehow, the Notebook is not allowing me to capture and rewrite the expression form of the graphic -- the only way I know to access it is by cutting and pasting the graphical form of the object manually by mouse into the places where I need to manipulate it. This is no good. I need SecretEvaluator, such that

SecretEvaluator[TreeForm[a+b^2+c^3+d]] /. (x_Framed :> Print[x])

produces the same output as

share|improve this question

3 Answers 3

up vote 8 down vote accepted

The reason your original code fails is that the TreeFrom object is only formatted as Graphics object, meaning that it converted for display rather that as part of the normal evaluation sequence.

You can convert to and from box form to recover your Graphics object:

tf = TreeForm[a + b^2 + c^3 + d];

gr = tf // ToBoxes // ToExpression

gr /. (x_Framed :> Print[x]);

enter image description here

share|improve this answer
    
ToBoxes and also MakeBoxes are the top-level clues to understanding how expressions are rendered and converted. –  Reb.Cabin Apr 11 at 12:56
    
@Reb.Cabin Thanks for the Accept. –  Mr.Wizard Apr 11 at 17:23

From this answer

Network`GraphPlot`ExprTreePlot[a+b^2+c^3+d] /. (x_Framed :> Print[x])
share|improve this answer

Building on swish's answer, I would write

nodes = Cases[Network`GraphPlot`ExprTreePlot[a + b^2 + c^3 + d], _Framed, ∞];

This has the advantage of allowing you to work with the individual node objects; for example:

nodes[[2]]

a-node

If want output that looks like your printed output just evaluate

Column@nodes

all-nodes

share|improve this answer

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.