Index: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
===================================================================
--- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs	(revision 7259)
+++ trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs	(revision 7446)
@@ -57,9 +57,5 @@
         StringBuilder strBuilder = new StringBuilder();
         constants.Clear();
-        strBuilder.AppendLine("% needs \\usepackage{amsmath}");
-        strBuilder.AppendLine("\\begin{align*}");
-        strBuilder.AppendLine("\\nonumber");
         strBuilder.AppendLine(FormatRecursively(symbolicExpressionTree.Root));
-        strBuilder.AppendLine("\\end{align*}");
         return strBuilder.ToString();
       }
@@ -92,10 +88,10 @@
     private void FormatBegin(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
       if (node.Symbol is Addition) {
-        strBuilder.Append(@" ( ");
+        strBuilder.Append(@" \left( ");
       } else if (node.Symbol is Subtraction) {
         if (node.SubtreeCount == 1) {
-          strBuilder.Append(@"- ( ");
+          strBuilder.Append(@"- \left( ");
         } else {
-          strBuilder.Append(@" ( ");
+          strBuilder.Append(@" \left( ");
         }
       } else if (node.Symbol is Multiplication) {
@@ -111,27 +107,27 @@
           strBuilder.Append(@" \cfrac{1}{" + node.SubtreeCount + @"}");
         }
-        strBuilder.Append(@" ( ");
+        strBuilder.Append(@" \left( ");
       } else if (node.Symbol is Logarithm) {
-        strBuilder.Append(@"\log ( ");
+        strBuilder.Append(@"\log \left( ");
       } else if (node.Symbol is Exponential) {
-        strBuilder.Append(@"\exp ( ");
+        strBuilder.Append(@"\exp \left( ");
       } else if (node.Symbol is Sine) {
-        strBuilder.Append(@"\sin ( ");
+        strBuilder.Append(@"\sin \left( ");
       } else if (node.Symbol is Cosine) {
-        strBuilder.Append(@"\cos ( ");
+        strBuilder.Append(@"\cos \left( ");
       } else if (node.Symbol is Tangent) {
-        strBuilder.Append(@"\tan ( ");
+        strBuilder.Append(@"\tan \left( ");
       } else if (node.Symbol is GreaterThan) {
-        strBuilder.Append(@"  ( ");
+        strBuilder.Append(@"  \left( ");
       } else if (node.Symbol is LessThan) {
-        strBuilder.Append(@"  ( ");
+        strBuilder.Append(@"  \left( ");
       } else if (node.Symbol is And) {
-        strBuilder.Append(@"   ( ");
+        strBuilder.Append(@"   \left( ");
       } else if (node.Symbol is Or) {
-        strBuilder.Append(@"   ( ");
+        strBuilder.Append(@"   \left( ");
       } else if (node.Symbol is Not) {
-        strBuilder.Append(@" \neg ( ");
+        strBuilder.Append(@" \neg \left( ");
       } else if (node.Symbol is IfThenElse) {
-        strBuilder.Append(@" \operatorname{if}  ( 0 < ");
+        strBuilder.Append(@" \operatorname{if}  \left( ");
       } else if (node.Symbol is Constant) {
         strBuilder.Append("c_{" + constants.Count + "} ");
@@ -156,4 +152,7 @@
         strBuilder.Append(LagToString(currentLag));
       } else if (node.Symbol is ProgramRootSymbol) {
+        strBuilder
+          .AppendLine("\\begin{align*}")
+          .AppendLine("\\nonumber");
       } else if (node.Symbol is Defun) {
         var defunNode = node as DefunTreeNode;
@@ -161,5 +160,5 @@
       } else if (node.Symbol is InvokeFunction) {
         var invokeNode = node as InvokeFunctionTreeNode;
-        strBuilder.Append(invokeNode.Symbol.FunctionName + @" ( ");
+        strBuilder.Append(invokeNode.Symbol.FunctionName + @" \left( ");
       } else if (node.Symbol is StartSymbol) {
         strBuilder.Append("Result & = ");
@@ -168,16 +167,16 @@
         strBuilder.Append(" ARG+" + argSym.ArgumentIndex + " ");
       } else if (node.Symbol is Derivative) {
-        strBuilder.Append(@" \cfrac{d ( ");
+        strBuilder.Append(@" \cfrac{d \left( ");
       } else if (node.Symbol is TimeLag) {
         var laggedNode = node as ILaggedTreeNode;
         currentLag += laggedNode.Lag;
       } else if (node.Symbol is Power) {
-        strBuilder.Append(@" ( ");
+        strBuilder.Append(@" \left( ");
       } else if (node.Symbol is Root) {
-        strBuilder.Append(@" ( ");
+        strBuilder.Append(@" \left( ");
       } else if (node.Symbol is Integral) {
         // actually a new variable for t is needed in all subtrees (TODO)
         var laggedTreeNode = node as ILaggedTreeNode;
-        strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 ( ");
+        strBuilder.Append(@"\sum_{t=" + (laggedTreeNode.Lag + currentLag) + @"}^0 \left( ");
       } else if (node.Symbol is VariableCondition) {
         var conditionTreeNode = node as VariableConditionTreeNode;
@@ -186,5 +185,5 @@
         p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"}   ";
         constants.Add(conditionTreeNode.Threshold);
-        strBuilder.Append(@" ( " + p + @"\cdot ");
+        strBuilder.Append(@" \left( " + p + @"\cdot ");
       } else {
         throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
@@ -221,11 +220,11 @@
         strBuilder.Append(@" < ");
       } else if (node.Symbol is And) {
-        strBuilder.Append(@" > 0  ) \land (");
+        strBuilder.Append(@" > 0  \right) \land \left(");
       } else if (node.Symbol is Or) {
-        strBuilder.Append(@" > 0  ) \lor (");
+        strBuilder.Append(@" > 0  \right) \lor \left(");
       } else if (node.Symbol is Not) {
         throw new InvalidOperationException();
       } else if (node.Symbol is IfThenElse) {
-        strBuilder.Append(@" ) , (");
+        strBuilder.Append(@" , ");
       } else if (node.Symbol is ProgramRootSymbol) {
         strBuilder.Append(@"\\" + Environment.NewLine);
@@ -236,14 +235,14 @@
         strBuilder.Append(@"\\" + Environment.NewLine + " & ");
       } else if (node.Symbol is Power) {
-        strBuilder.Append(@") ^ { \operatorname{round} (");
+        strBuilder.Append(@"\right) ^ { \operatorname{round} \left(");
       } else if (node.Symbol is Root) {
-        strBuilder.Append(@") ^ {  \cfrac{1}{ \operatorname{round} (");
+        strBuilder.Append(@"\right) ^ {  \cfrac{1}{ \operatorname{round} \left(");
       } else if (node.Symbol is VariableCondition) {
         var conditionTreeNode = node as VariableConditionTreeNode;
-        string p = @"1 / ( 1 + \exp ( - c_{" + constants.Count + "} ";
+        string p = @"1 / \left( 1 + \exp \left( - c_{" + constants.Count + "} ";
         constants.Add(conditionTreeNode.Slope);
-        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} ) ) )   ";
+        p += @" \cdot " + EscapeLatexString(conditionTreeNode.VariableName) + LagToString(currentLag) + " - c_{" + constants.Count + @"} \right) \right) \right)   ";
         constants.Add(conditionTreeNode.Threshold);
-        strBuilder.Append(@" +  ( 1 - " + p + @" ) \cdot ");
+        strBuilder.Append(@" +  \left( 1 - " + p + @" \right) \cdot ");
       } else {
         throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
@@ -253,7 +252,7 @@
     private void FormatEnd(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
       if (node.Symbol is Addition) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Subtraction) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Multiplication) {
       } else if (node.Symbol is Division) {
@@ -262,59 +261,67 @@
           strBuilder.Append(" } ");
       } else if (node.Symbol is Average) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Logarithm) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Exponential) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Sine) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Cosine) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Tangent) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is GreaterThan) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is LessThan) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is And) {
-        strBuilder.Append(@" > 0 ) ) ");
+        strBuilder.Append(@" > 0 \right) \right) ");
       } else if (node.Symbol is Or) {
-        strBuilder.Append(@" > 0 ) ) ");
+        strBuilder.Append(@" > 0 \right) \right) ");
       } else if (node.Symbol is Not) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is IfThenElse) {
-        strBuilder.Append(@" ) ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is Constant) {
       } else if (node.Symbol is LaggedVariable) {
       } else if (node.Symbol is Variable) {
       } else if (node.Symbol is ProgramRootSymbol) {
+        strBuilder
+          .AppendLine("\\end{align*}")
+          .AppendLine("\\begin{align*}")
+          .AppendLine("\\nonumber");
         // output all constant values
         if (constants.Count > 0) {
           int i = 0;
           foreach (var constant in constants) {
-            strBuilder.AppendLine(@"\\");
-            strBuilder.Append("c_{" + i + "} & = " + constant);
+            // replace "." with ".&" to align decimal points
+            var constStr = string.Format(System.Globalization.NumberFormatInfo.InvariantInfo, "{0:G5}", constant);
+            if(!constStr.Contains(".")) constStr = constStr + ".0";
+            constStr = constStr.Replace(".", "\\negthickspace&.");  // fix problem in rendering of aligned expressions
+            strBuilder.Append("c_{" + i + "}& = & " + constStr);
+            strBuilder.Append(@"\\");
             i++;
           }
         }
+        strBuilder.AppendLine("\\end{align*}");
       } else if (node.Symbol is Defun) {
       } else if (node.Symbol is InvokeFunction) {
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is StartSymbol) {
       } else if (node.Symbol is Argument) {
       } else if (node.Symbol is Derivative) {
-        strBuilder.Append(@" ) }{dt} ");
+        strBuilder.Append(@" \right) }{dt} ");
       } else if (node.Symbol is TimeLag) {
         var laggedNode = node as ILaggedTreeNode;
         currentLag -= laggedNode.Lag;
       } else if (node.Symbol is Power) {
-        strBuilder.Append(@" ) } ");
+        strBuilder.Append(@" \right) } ");
       } else if (node.Symbol is Root) {
-        strBuilder.Append(@" ) } ) } ");
+        strBuilder.Append(@" \right) } } ");
       } else if (node.Symbol is Integral) {
-        var laggedTreeNode = node as ILaggedTreeNode;
-        strBuilder.Append(@" ) ");
+        strBuilder.Append(@" \right) ");
       } else if (node.Symbol is VariableCondition) {
-        strBuilder.Append(@"\left) ");
+        strBuilder.Append(@"\right) ");
       } else {
         throw new NotImplementedException("Export of " + node.Symbol + " is not implemented.");
@@ -331,5 +338,11 @@
 
     private string EscapeLatexString(string s) {
-      return s.Replace(@"_", @"\_");
+      return "\\text{" +
+        s
+         .Replace("\\", "\\\\")
+         .Replace("{", "\\{")
+         .Replace("}", "\\}")
+         .Replace("%", "\\%")
+        + "}";
     }
   }
