3
$\begingroup$

Of course we know,the TreeForm[a + b^2 + c^3 + d] will give a Graphics object.How to assign to a variable an expression which typesets to the same box structure as a TreeForm of a particular input?

Maybe one think we just use the var=TreeForm[a + b^2 + c^3 + d],but you will find the Head of the var is TreeForm.I record a gif for specify this case.

enter image description here

And I found this regular is apply to any *Form function.Such as InputForm

a = InputForm[Graphics[Circle[]]];
Head[a]

InputForm

I have a awkward workaround calling the clipboard can do it almost.

var = NotebookGet[CopyToClipboard[TreeForm[a + b^2 + c^3 + d]]; 
  ClipboardNotebook[]]

But there are some problem in this solution.

  1. var include all information,but the Head of var is not Graphics actually.
  2. I really don't want to call the clipboard.

So how to assign the Graphics object to a variable?

$\endgroup$
2
  • $\begingroup$ Type var=TreeForm[a+b^2+c^3+d], select the right hand side and press ctrl-shift-enter (that is evaluate in place) and evaluate the cell. $\endgroup$ Jul 11 '17 at 9:23
  • $\begingroup$ @FredSimons I know that,but that is not my after.Anyway thanks. :) $\endgroup$
    – yode
    Jul 11 '17 at 9:24
6
$\begingroup$

We have to be careful with wording.

What you really want to do is to "assign to a variable an expression which typesets to the same box structure as a TreeForm of a particular input".

And this can be achieved with:

test = ToExpression @ ToBoxes @ TreeForm[a + b];

So once you know what you want the solution is obvious.

to confirm:

test // InputForm // SequenceForm
   Graphics[Annotation[
  GraphicsComplex[{{0.4472135954999579, 0.8944271909999159}, {0., 
     0.}, {0.8944271909999159, 0.}}, {{RGBColor[0.55, 0.45, 0.45], 
     Line[{{1, 2}, {1, 3}}]}, {Tooltip[
      Inset[Framed[
        Style[Plus, {"StandardForm", "Output", GrayLevel[0]}, 
         FontSize -> Scaled[0.05]], 
        Background -> RGBColor[1., 1., 0.871], 
        FrameStyle -> GrayLevel[0.5], FrameMargins -> Inherited], 1], 
      HoldForm[a + b]], 
     Tooltip[Inset[
       Framed[Style[
         HoldForm[a], {"StandardForm", "Output", GrayLevel[0]}, 
         FontSize -> Scaled[0.05]], 
        Background -> RGBColor[1., 1., 0.871], 
        FrameStyle -> GrayLevel[0.5], FrameMargins -> Inherited], 2], 
      HoldForm[a]], 
     Tooltip[Inset[
       Framed[Style[
         HoldForm[b], {"StandardForm", "Output", GrayLevel[0]}, 
         FontSize -> Scaled[0.05]], 
        Background -> RGBColor[1., 1., 0.871], 
        FrameStyle -> GrayLevel[0.5], FrameMargins -> Inherited], 3], 
      HoldForm[b]]}}, {}], 
  VertexCoordinateRules -> {{0.4472135954999579, 
     0.8944271909999159}, {0., 0.}, {0.8944271909999159, 0.}}], 
 FrameTicks -> Automatic, PlotRange -> All, 
 PlotRangePadding -> Scaled[0.1], AspectRatio -> 1, 
 FormatType :> StandardForm]
$\endgroup$
2
  • $\begingroup$ To be honest, I cannot imagine ToBoxes can do such thing still.. $\endgroup$
    – yode
    Jul 11 '17 at 9:30
  • $\begingroup$ @yode that is exactly what is expected from ToBoxes, the main problem here is to realize that we work here with typset/boxes representation not ordinary expressions. Which is expected as we deal here with *Forms. $\endgroup$
    – Kuba
    Jul 11 '17 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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