8
$\begingroup$

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

$\endgroup$

3 Answers 3

10
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ ToBoxes and also MakeBoxes are the top-level clues to understanding how expressions are rendered and converted. $\endgroup$
    – Reb.Cabin
    Commented Apr 11, 2014 at 12:56
  • $\begingroup$ @Reb.Cabin Thanks for the Accept. $\endgroup$
    – Mr.Wizard
    Commented Apr 11, 2014 at 17:23
7
$\begingroup$

From this answer

Network`GraphPlot`ExprTreePlot[a+b^2+c^3+d] /. (x_Framed :> Print[x])
$\endgroup$
3
$\begingroup$

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

$\endgroup$

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.