I am writing my own TreeForm
function and for this I want to make sure that framed text boxes in Graphics
do not overlap. First, I set up a function that returns a framed box with some expression in it:
ExpBox[exp_]:=Text[Framed[Style[exp, 12], FrameMargins->5],FormatType->StandardForm]
ExpBox[exp_, xy_]:=Text[Framed[Style[exp, 12], FrameMargins->5],xy,
FormatType->StandardForm]
I measure the width of such a box using Rasterize
. Consider, for example, the two expressions
exp1=Fooooo*2;
exp2=Bar;
with the width
w1=First@Rasterize[ExpBox[exp1], "RasterSize"]
w2=First@Rasterize[ExpBox[exp2], "RasterSize"]
With those values I can set up a graphics with two nonoverlapping text boxes
Graphics[{ExpBox[exp1, {w1/2, 0}], ExpBox[exp2, {w1 + w2/2, 0}]},
PlotRange -> {{0, w1 + w2}, {-20, 20}}, ImageSize -> {w1 + w2, 40}]
and I get (as desired)
However, this does not work correctly as soon as a division is found within any of the expressions:
exp1=Fooooo/2;
The reason seems to be that, if there happens to be a division in an expression, then the ExpBox
is "printed" at smaller size without a containing Graphics
function.
How can I avoid this difference? Or is there another way to get the width of such a box?
Row[{ExpBox[Fooooo*2], ExpBox[Bar]}]
gives the same output as your original function, is there a reason not to do it like that instead? $\endgroup$TreeForm
variant and for this I also need to connect various boxes withLine
s. I also need vertical alignment of boxes. For this I need the coordinates of the boxes. $\endgroup$