Index: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
===================================================================
--- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs	(revision 9835)
+++ branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs	(revision 9963)
@@ -24,4 +24,6 @@
 using System.Drawing;
 using System.Drawing.Imaging;
+using System.IO;
+using System.Linq;
 using System.Windows.Forms;
 using Point = System.Drawing.Point;
@@ -33,4 +35,7 @@
     private Dictionary<ISymbolicExpressionTreeNode, VisualSymbolicExpressionTreeNode> visualTreeNodes;
     private Dictionary<Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>, VisualSymbolicExpressionTreeNodeConnection> visualLines;
+    private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> { MinHorizontalSpacing = 5, MinVerticalSpacing = 5 };
+    private readonly SymbolicExpressionTreeLayoutAdapter layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
+
 
     public SymbolicExpressionTreeChart() {
@@ -160,6 +165,5 @@
         graphics.Clear(backgroundColor);
         if (tree != null) {
-          int height = this.Height / tree.Depth;
-          DrawFunctionTree(tree, graphics, 0, 0, this.Width, height);
+          DrawFunctionTree(tree, graphics, 70, 46, 20, 50);
         }
       }
@@ -254,22 +258,19 @@
     #region methods for painting the symbolic expression tree
 
-    private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int x, int y, int width, int height) {
-      //      DrawFunctionTree(tree.Root, graphics, x, y, width, height, Point.Empty);
-      AlternateDraw(tree, graphics, 70, 46, 20, 50);
-    }
-
-    private void AlternateDraw(ISymbolicExpressionTree tree, Graphics graphics, int preferredWidth, int preferredHeight, int minDistance, int maxDistance) {
-      var tl = new TreeLayout();
-      tl.Distance = 5;
-      tl.SymbolicExpressionTree = tree;
-
-      var nodePositions = tl.GetNodeCoordinates();
-      var bounds = tl.Bounds();
+    private void DrawFunctionTree(ISymbolicExpressionTree tree, Graphics graphics, int preferredWidth, int preferredHeight, int minDistance, int maxDistance) {
+      var layoutNodes = layoutAdapter.Convert(tree).ToList();
+      layoutEngine.Reset();
+      layoutEngine.Root = layoutNodes[0];
+      foreach (var ln in layoutNodes)
+        layoutEngine.AddNode(ln.Content, ln);
+      layoutEngine.CalculateLayout();
+      var nodePositions = layoutEngine.GetNodeCoordinates();
+      var bounds = layoutEngine.Bounds();
 
       double sx = Width / bounds.Width;
       double sy = Height / bounds.Height;
 
-      double dx = tl.Distance * sx; // scaled horizontal distance
-      double dy = tl.Distance * sy; // scaled vertical distance
+      double dx = layoutEngine.MinHorizontalSpacing * sx; // scaled horizontal distance
+      double dy = layoutEngine.MinVerticalSpacing * sy; // scaled vertical distance
 
       int maxWidth = (int)Math.Round(dx);
@@ -307,86 +308,4 @@
             graphics.DrawLine(linePen, origin, target);
           }
-        }
-      }
-    }
-
-    /// <summary>
-    /// 
-    /// </summary>
-    /// <param name="node">the root of the function tree to draw</param>
-    /// <param name="graphics">graphics object to draw on</param>
-    /// <param name="x">x coordinate of drawing area</param>
-    /// <param name="y">y coordinate of drawing area</param>
-    /// <param name="width">width of drawing area</param>
-    /// <param name="height">height of drawing area</param>
-    private void DrawFunctionTree(ISymbolicExpressionTreeNode node, Graphics graphics, int x, int y, int width, int height, Point connectionPoint) {
-      VisualSymbolicExpressionTreeNode visualTreeNode = visualTreeNodes[node];
-      float center_x = x + width / 2;
-      float center_y = y + height / 2;
-      int actualWidth = width - spacing;
-      int actualHeight = height - spacing;
-
-      using (var textBrush = new SolidBrush(visualTreeNode.TextColor))
-      using (var nodeLinePen = new Pen(visualTreeNode.LineColor))
-      using (var nodeFillBrush = new SolidBrush(visualTreeNode.FillColor)) {
-
-        //calculate size of node
-        if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
-          visualTreeNode.Width = visualTreeNode.PreferredWidth;
-          visualTreeNode.Height = visualTreeNode.PreferredHeight;
-          visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
-          visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
-        }
-          //width too small to draw in desired sized
-        else if (actualWidth < visualTreeNode.PreferredWidth && actualHeight >= visualTreeNode.PreferredHeight) {
-          visualTreeNode.Width = actualWidth;
-          visualTreeNode.Height = visualTreeNode.PreferredHeight;
-          visualTreeNode.X = x;
-          visualTreeNode.Y = (int)center_y - visualTreeNode.Height / 2;
-        }
-          //height too small to draw in desired sized
-        else if (actualWidth >= visualTreeNode.PreferredWidth && actualHeight < visualTreeNode.PreferredHeight) {
-          visualTreeNode.Width = visualTreeNode.PreferredWidth;
-          visualTreeNode.Height = actualHeight;
-          visualTreeNode.X = (int)center_x - visualTreeNode.Width / 2;
-          visualTreeNode.Y = y;
-        }
-          //width and height too small to draw in desired size
-        else {
-          visualTreeNode.Width = actualWidth;
-          visualTreeNode.Height = actualHeight;
-          visualTreeNode.X = x;
-          visualTreeNode.Y = y;
-        }
-
-        //draw terminal node
-        if (node.SubtreeCount == 0) {
-          graphics.FillRectangle(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
-          graphics.DrawRectangle(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
-        } else {
-          graphics.FillEllipse(nodeFillBrush, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
-          graphics.DrawEllipse(nodeLinePen, visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height);
-        }
-
-        //draw name of symbol
-        var text = node.ToString();
-        graphics.DrawString(text, textFont, textBrush, new RectangleF(visualTreeNode.X, visualTreeNode.Y, visualTreeNode.Width, visualTreeNode.Height), stringFormat);
-
-        //draw connection line to parent node
-        if (!connectionPoint.IsEmpty && node.Parent != null) {
-          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node.Parent, node);
-          using (Pen linePen = new Pen(visualLine.LineColor)) {
-            linePen.DashStyle = visualLine.DashStyle;
-            graphics.DrawLine(linePen, connectionPoint, new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y));
-          }
-        }
-
-        //calculate areas for the subtrees according to their tree size and call drawFunctionTree
-        Point connectFrom = new Point(visualTreeNode.X + visualTreeNode.Width / 2, visualTreeNode.Y + visualTreeNode.Height);
-        int[] xBoundaries = new int[node.SubtreeCount + 1];
-        xBoundaries[0] = x;
-        for (int i = 0; i < node.SubtreeCount; i++) {
-          xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.GetSubtree(i).GetLength()) / (node.GetLength() - 1));
-          DrawFunctionTree(node.GetSubtree(i), graphics, xBoundaries[i], y + height, xBoundaries[i + 1] - xBoundaries[i], height, connectFrom);
         }
       }
@@ -455,4 +374,14 @@
     }
     #endregion
+    #region export pgf/tikz
+    private void exportLatexToolStripMenuItem_Click(object sender, EventArgs e) {
+      using (var dialog = new SaveFileDialog { Filter = "Tex (*.tex)|*.tex" }) {
+        if (dialog.ShowDialog() != DialogResult.OK) return;
+        string filename = dialog.FileName.ToLower();
+        var formatter = new SymbolicExpressionTreeLatexFormatter();
+        File.WriteAllText(filename, formatter.Format(Tree));
+      }
+    }
+    #endregion
   }
 }
