wiki:TreeMagic

Version 10 (modified by stefan, 6 years ago) (diff)

--

About

Tree Magic allows to draw beautiful (Tikz) trees within a few seconds by providing a much simpler syntax than the original Tikz library.

For example the tree

http://common.jle.cc/features/tree.png

is generated by the code:

f(g(a,b),h(c))
highlight[paint=red](g, a, b)

System requirements

  • You need to have Haskell (ghc) installed as well as Parsec (called approximately libghc-parsec).

Getting started

  • To start Tree Magic, simply write
    %!begin{tree}
    
    and press (alt+enter). This will automatically generate the following code:
    %!begin{tree}{magic:023916255455191981}
    % 
    %!end{tree}{magic:023916255455191981}
    
  • Trees now can simply be written as terms, for example f(g(a,b),h(c)) for the tree

http://common.jle.cc/features/tree_simple.png

Then we have

%!begin{tree}{magic:023916255455191981}
%f(g(a,b),h(c))
%!end{tree}{magic:023916255455191981}
  • We again press (alt+enter) to invoke Tree Magic. Then the corresponding Tikz code is generated and inserted in the document:
    %!begin{tree}{magic:023916255455191981}
    %f(g(a,b),h(c))
    \node (f) {f}
        child { node (g) {g}
          child { node (a) {a}
        }
          child { node (b) {b}
        }
      }
        child { node (h) {h}
          child { node (c) {c}
        }
      };
    \begin{pgfonlayer}{background}
    \end{pgfonlayer}
    
    %!end{tree}{magic:023916255455191981}
    
  • Here the whole Latex source code of this example:
    \documentclass{article}
    
    \usepackage{tikz}
    \usetikzlibrary{arrows,backgrounds,calc,trees}
    
    \begin{document}
    \thispagestyle{empty}
    
    \begin{tikzpicture}[thick,level distance=12mm,
      paint/.style={very thick,draw=#1!50!black,fill=#1,opacity=.3}]
    %!begin{tree}{magic:023916255455191981}
    %f(g(a,b),h(c))
    \node (f) {f}
        child { node (g) {g}
          child { node (a) {a}
        }
          child { node (b) {b}
        }
      }
        child { node (h) {h}
          child { node (c) {c}
        }
      };
    \begin{pgfonlayer}{background}
    \end{pgfonlayer}
    
    %!end{tree}{magic:023916255455191981}
    \end{tikzpicture}
    
    \end{document}
    

Highlighting parts of the tree

  • We continue the previous example. To highlight a the nodes {g,a,b}, simply write highlight[paint=red](g, a, b)
    %!begin{tree}{magic:023916255455191981}
    %f(g(a,b),h(c))
    %highlight[paint=red](g,a,b)
    ... generated code ...
    %!end{tree}{magic:023916255455191981}
    
    Then press (alt+enter) to invoke Tree Magic, and you obtain the tree:

http://common.jle.cc/features/tree.png

  • Of course we can highlight multiple parts of the tree:
    %!begin{tree}{magic:023916255455191981}
    %f(g(a,b),h(c))
    %highlight[paint=red](g,a,b)
    %highlight[paint=blue](f,g,h,c,h)
    ... generated code ...
    %!end{tree}{magic:023916255455191981}
    
    Then press (alt+enter) to invoke Tree Magic, and you obtain the tree:

http://common.jle.cc/features/tree_highlight.png

Complex nodes (math mode)

  • To create complex nodes (e.g. with mathematical formulas), we can surround the node with braces ('{' and '}') or quotes ('"').
  • For example,
    %{$f^2(x)$}({$g_h$}(a,b),{h+3}(c))
    %highlight[paint=red](g_h,a,b)
    %highlight[paint=blue](f^2x,g_h,h+3,c,h+3)
    
    will result in the tree:

http://common.jle.cc/features/tree_math.png

  • Note the naming of the nodes in the highlight section! You find more on the naming of nodes in the following section.

Node naming

  • By default, the names of the nodes are derived from the content of the nodes by removing all symbols '()[]{}\$'. Thus the node $f^2(x)$ will be named f^2x.
  • You may use custom names by node:name. For example {$f^2(x)$}:root will assign the name 'root' to the node. Then the code:
    %{$f^2(x)$}:root({$g_h$}(a,b),{h+3}(c))
    %highlight[paint=red](g_h,a,b)
    %highlight[paint=blue](root,g_h,h+3,c,h+3)
    
    results in the same picture as above, but now we use 'root' to refer to '$f^2(x)$' in the highlight section.
  • You can use these names to refer to the nodes from Tikz. For example to add an arrow from 'c' to the root, add
    \draw [->,very thick] (c) to[bend right=80] (root);
    
    after the Tree Magic section. This results in:

http://common.jle.cc/features/tree_naming.png

Tikz options

  • You may pass Tikz drawing options to a node via '[options]' after the node name. For example:
    %f[rectangle,fill=green!50,draw=green!50!black](g(a,b),h(c))
    %highlight[paint=red](g,a,b)
    
    will result in:

http://common.jle.cc/features/tree_options.png

  • We can provide more Tikz options as follows:
    %f([options1] [options2] g [options3](a,b),h(c))
    
    which results in the following options being passed to child 'g':
    ... [options1] child [options2] { node [options3] (g) {g} ...
    

For example:

%f( [] [grow=180] g [rectangle,fill=green!50,draw=green!50!black] (a,b),h(c))

results in the Tikz code:

\node (f) {f}
    []child[grow=180] { node[rectangle,fill=green!50,draw=green!50!black] (g) {g}
      child { node (a) {a}
    }
      child { node (b) {b}
    }
  }
    child { node (h) {h}
      child { node (c) {c}
    }
  };

and the tree:

http://common.jle.cc/features/tree_more_options.png