7

I would like to align a TikZ tree with another node by considering the tree as a whole like as if it was a node with anchors. For the moment I am only able to align the root node of the tree, not the whole tree itself. I use PGF 2.10.

The following picture shows what I have:

what I have

What I want is that the left node and the tree are horizontally aligned and the arrow goes from the east anchor of the left node to the west anchor of the tree, i.e. more or less the left child of the root node as shown here:

what I want

Please ignore the outer border on both images.

Do you know a way to achieve this?

My test code is given below. I compile it with xelatex.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning,trees}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength{\imagewidth}
\newlength{\imagescale}

\begin{document}    
\begin{tikzpicture}[
    ->, >=stealth', shorten >=1pt, semithick, node distance=1cm, level distance=7mm, level/.style={sibling distance=10mm/#1},
    block/.style = {draw, rectangle, rounded corners, minimum height=1cm},
    every node/.style={circle, draw, fill=none, anchor=north}
]

\node [block] (SOURCE DOCUMENT) {\parbox{2cm}{\centering source document}};

\node (SOURCE TREE)[right=of SOURCE DOCUMENT] {}
    child { node {} }
    child { node {} 
        child { node {} }
        child { node {} }
    };

\draw [->] (SOURCE DOCUMENT) edge (SOURCE TREE);
\end{tikzpicture}   
\end{document}
1

2 Answers 2

3

Using fit library you may create additional node, that would contain every node in your tree. After that use it's anchors to perform your justification.

Here's result of such solution:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning,trees,fit}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength{\imagewidth}
\newlength{\imagescale}

\begin{document}
\begin{tikzpicture}[
    ->, >=stealth', shorten >=1pt, semithick, node distance=1cm, level distance=7mm, level/.style={sibling distance=10mm/#1},
    block/.style = {draw, rectangle, rounded corners, minimum height=1cm},
    every node/.style={circle, draw, fill=none, anchor=north}
]

\node (SOURCE TREE) {}
    child { node {} }
    child { node {} 
        child { node {} }
        child { node {} }
    };

\node[fit=(SOURCE TREE) (SOURCE TREE-1) (SOURCE TREE-2) (SOURCE TREE-2-1) (SOURCE TREE-2-2),
    draw=none, rectangle, inner sep=0] (whole tree) {};

\node[block, left=of whole tree] (SOURCE DOCUMENT) {\parbox{2cm}{\centering source document}};

\draw (SOURCE DOCUMENT) -- (whole tree);
\end{tikzpicture}   
\end{document}

enter image description here

2
  • Thank you. It works just how I wanted to. Listing all the nodes of the tree in the fit option is quite tedious but I found out that I can just list the "outer" child nodes (root, left child of root and rightmost node) to get the right bounding box. I also like your notation SOURCE TREE-2-1 avoiding to name the nodes explicitly. Commented Apr 24, 2011 at 14:17
  • @Ludovic: 1. on notation - this is how TikZ works. No credentials for me. 2. on tediousness - my answer to my own question may shed some light on how to avoid this: tex.stackexchange.com/questions/15147/…. 3. Listing outer nodes is completely legal. Commented Apr 24, 2011 at 14:34
1

Instead of placing the tree to the right of the box, place the box to the left of the (selected, suitably named) child node:

...
\node (SOURCE TREE) {}
    child { node (x) {} }
    child { node {} 
        child { node {} }
        child { node {} }
    };

\node [block,left=of x] (SOURCE DOCUMENT) {\parbox{2cm}{\centering source document}};

\draw [->] (SOURCE DOCUMENT) edge (x);
...
1
  • 1
    That's a good idea except that is specific to my example of a tree having an odd depth and containing edges of equal sizes. I prefer the solution given above because it works in all cases and I also have a bounding box with the fitting node that I can use to do other things. Commented Apr 24, 2011 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .