4

I need to display arbitrary Expressions in TreeForm, but the standard vertical layout produces too many horizontally overlapping boxes. Am I doomed to rewriting expressions as graphs and using TreePlot and GraphPlot and going down this road

http://www.wolfram.com/technology/guide/FullyAutomatedGraphLayout/

or is there something I can do in a hurry to make TreeForm look better?

Docs say

TreeForm takes the same options as TreePlot. 

so I pop over to TreePlot and see

TreePlot[{1 -> 4, 1 -> 6, 1 -> 8, 2 -> 6, {3 -> 8, "3\[Rule]8"}, 
  4 -> 5, 7 -> 8}, Left]

go back to TreeForm and try

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

but oops:

TreeForm::tlev: Warning: level specification Left is not a non-negative integer or
Infinity; ignored. >>
VividD
  • 3,660
  • 4
  • 26
  • 42
Reb.Cabin
  • 8,661
  • 1
  • 34
  • 62

2 Answers2

11

Generally - arbitrary-angle layout:

Rotate[ ... /. x_Framed -> Rotate[x, -angle], angle]

enter image description here

Now you can do it like this:

Rotate[ToExpression@ToBoxes@TreeForm[a + b^2 + c^3 + d] /. 
  x_Framed -> Rotate[x, -Pi/2], Pi/2]

Or like this:

enter image description here

P.S. =========================

Manipulate code for the record

Manipulate[Show[Rasterize@
   Rotate[ToExpression@ToBoxes@TreeForm[a + b^2 + c^3 + d] /. 
     x_Framed -> Rotate[x, -ang], ang],
  ImageSize -> 200 {1, 1}],
 {ang, 0, 2 Pi}]
Vitaliy Kaurov
  • 73,078
  • 9
  • 204
  • 355
  • ouch...totally ignored Rotate. yes, that makes absolutely sense. – Stefan Apr 10 '14 at 19:39
  • This is a really good instance of "thinking out of the box" -- composing some generic but display-specific functions (/slaps forehead) – Reb.Cabin Apr 10 '14 at 20:54
  • Interesting that the Rotate works when the TreeForm graphic is pasted directly into the Rotate call, but the following does not work (because it doesn't rotate the internal framed bits) Rotate[TreeForm[a+b^2+c^3+d], /. x_Framed -> Rotate[Framed[x], -Pi/2], Pi/2] – Reb.Cabin Apr 10 '14 at 21:17
  • 1
    This code adds an extra frame. You only need Rotate[ ... /. x_Framed -> Rotate[x, -Pi/2], Pi/2] – ArgentoSapiens Apr 10 '14 at 22:04
  • @ArgentoSapiens thank you, corrected. – Vitaliy Kaurov Apr 11 '14 at 14:43
1

I don't know if this is by any means what you're looking for, but you can use VertexCoordinateRules to determine the coordinates at which vertices should be placed:

TreeForm[a + b^2 + c^3 + d, 
    VertexCoordinateRules -> {{1, 4}, {2, 8}, {3, 8}, {4, 10}, {4, 6}, {3, 0}, 
        {4, 3}, {4, -2}, {2, 0}}]

enter image description here

Stefan
  • 5,347
  • 25
  • 32