1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
10 | [Item("LaTeX/PDF Formatter", "Formatter for symbolic expression trees for use with latex.")]
|
---|
11 | public class SymbolicExpressionTreeLatexFormatter : NamedItem, ISymbolicExpressionTreeStringFormatter {
|
---|
12 | private readonly static Dictionary<string, string> SymbolNamesMap = new Dictionary<string, string>
|
---|
13 | {
|
---|
14 | {"ProgramRootSymbol", "Prog"},
|
---|
15 | {"StartSymbol","RPB"},
|
---|
16 | {"Addition", "$+$"},
|
---|
17 | {"Subtraction", "$-$"},
|
---|
18 | {"Multiplication", "$\\times$"},
|
---|
19 | {"Division", "$\\div$"},
|
---|
20 | {"Logarithm", "$\\log$"},
|
---|
21 | {"Exponential", "$\\exp$"}
|
---|
22 | };
|
---|
23 | private readonly ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> layoutEngine;
|
---|
24 | private readonly SymbolicExpressionTreeLayoutAdapter layoutAdapter;
|
---|
25 |
|
---|
26 | public SymbolicExpressionTreeLatexFormatter() {
|
---|
27 | layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
|
---|
28 | layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
|
---|
29 | }
|
---|
30 |
|
---|
31 | protected SymbolicExpressionTreeLatexFormatter(SymbolicExpressionTreeLatexFormatter original, Cloner cloner)
|
---|
32 | : base(original, cloner) {
|
---|
33 | layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>();
|
---|
34 | layoutAdapter = new SymbolicExpressionTreeLayoutAdapter();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
38 | return new SymbolicExpressionTreeLatexFormatter(this, cloner);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
42 | var layoutNodes = layoutAdapter.Convert(symbolicExpressionTree).ToList();
|
---|
43 | layoutEngine.Reset();
|
---|
44 | layoutEngine.Root = layoutNodes[0];
|
---|
45 | foreach (var ln in layoutNodes)
|
---|
46 | layoutEngine.AddNode(ln.Content, ln);
|
---|
47 | layoutEngine.CalculateLayout();
|
---|
48 | var nodeCoordinates = layoutEngine.GetNodeCoordinates();
|
---|
49 | var sb = new StringBuilder();
|
---|
50 | var nl = Environment.NewLine;
|
---|
51 | sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
|
---|
52 | "\\usepackage{tikz}" + nl +
|
---|
53 | "\\begin{document}" + nl +
|
---|
54 | "\\begin{tikzpicture}" + nl +
|
---|
55 | "\\def\\ws{1}" + nl +
|
---|
56 | "\\def\\hs{0.7}" + nl);
|
---|
57 |
|
---|
58 | const double ws = 1.0, hs = 0.7; // some scaling factors useful for fine-tuning document appearance before latex compilation
|
---|
59 | var nodes = symbolicExpressionTree.IterateNodesBreadth().ToList();
|
---|
60 | var nodeIndices = new Dictionary<ISymbolicExpressionTreeNode, int>();
|
---|
61 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
62 | var node = nodes[i];
|
---|
63 | nodeIndices.Add(node, i);
|
---|
64 | var symbolName = node.Symbol.Name;
|
---|
65 | var nodeName = SymbolNamesMap.ContainsKey(symbolName) ? SymbolNamesMap[symbolName] : symbolName;
|
---|
66 | var coord = nodeCoordinates[node];
|
---|
67 |
|
---|
68 | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1},\\hs*{2}) {{{3}}};", i, ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
|
---|
69 | }
|
---|
70 |
|
---|
71 | for (int i = 0; i < nodes.Count; ++i) {
|
---|
72 | foreach (var s in nodes[i].Subtrees) {
|
---|
73 | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", i, nodeIndices[s]));
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | sb.AppendLine("\\end{tikzpicture}" + nl + "\\end{document}" + nl);
|
---|
78 |
|
---|
79 | return sb.ToString();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private static string EscapeLatexString(string s) {
|
---|
83 | return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|