Graphics, Figures & TablesVertical alignment of subnodes in a tree

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

Vertical alignment of subnodes in a tree

Post by usr345 »

How is it possible to create a tree like this?

Image

Because I have a complex tree and wanna align the overlapping subnodes horizontally:

Image

Here is the code:

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a2paper,landscape]{geometry}
\usepackage[russian]{babel}
\usepackage[cm]{fullpage}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usepackage{verbatim}

\usetikzlibrary{trees,positioning,arrows}
\begin{document}

\begin{tikzpicture}[
% Label style
    label distance=3mm,
    every label/.style={blue},
% Event style
    root/.style={ellipse, ,draw=black, top color=white, bottom color=yellow!50,thick, inner sep=0.2cm, minimum size=3em, text centered, double},
    dir/.style={rectangle,rounded corners,thick,draw,
    			top color=white, bottom color=blue!20, text centered, inner sep=0.2cm},
    event/.style={rectangle,thick,draw,fill=yellow!20, text centered,font=\sffamily,anchor=north, inner sep=0.2cm,},
% Children and edges style
    edge from parent/.style={very thick,draw=black!70},
    edge from parent path={(\tikzparentnode.south) -- ++(0,-1.05cm)
			-| (\tikzchildnode.north)},
    level 1/.style={sibling distance=18cm,level distance=1.4cm,
			growth parent anchor=south,nodes=event},
    level 2/.style={sibling distance=3cm},
    level 3/.style={sibling distance=2cm},
    level 4/.style={sibling distance=3cm},
    level 5/.style={sibling distance=3cm},
    level 5/.style={sibling distance=3cm},
%%  For compatability with PGF CVS add the absolute option:
%   absolute
	yshift=-3cm
    ]
%% Draw events and edges
	\node (g2) [root] {com\_component}
		child {node [dir] {/site}
			child {node {component.php}}
			child {node {controller.php}}
			child {node [dir] {/views}
				child {node [dir] {/component}
					child {node {view.html.php}}
					child {node [dir] {/tmpl}
						child {node {default.php}}
						child {node {default.xml}}						
					}
				}
			}
			child {node [dir] {/models}
				child {node {component.php}}
			}
			child {node [dir] {/language}
				child {node [dir] {/en-GB}
					child {node {en-GB.com\_component.ini}}
				}
			}
		}
		child {node [dir] {/admin}
			child {node {component.php}}
			child {node {controller.php}}
			child {node [dir] {/views}
				child {node [dir] {/component}
					child {node {view.html.php}}
					child {node [dir] {/tmpl}
						child {node {default.php}}
						child {node {default\_head.php}}
						child {node {default\_body.php}}
						child {node {default\_foot.php}}
						child {node {edit.php}}
					}
				}
			}
			child {node [dir] {/models}
				child {node {components.php}}
				child {node {component.php}}
				child {node [dir] {forms}
					child {node {component.xml}}
				}
			}
			child {node [dir] {/language}
				child {node [dir] {/en-GB}
					child {node {en-GB.com\_component.sys.ini}}
				}
			}
			child {node [dir] {/sql}
				child {node [text width=3.5cm] {install.mysql.utf8.sql}}
				child {node [dir] {/updates}
					child {node [dir] {mysql}
						child {node {0.0.6.sql}}
					}
				}
				child {node {uninstall.mysql.utf8.sql}}
			}
		}
		child {node [dir] {/language}
			child {node [dir] {/en-GB}
				child {node {en-GB.ini}}
			}
		}
	;


\end{tikzpicture}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

Re: Vertical alignment of subnodes in a tree

Post by usr345 »

I found a library: /pgf/frontendlayer/tikz/libraries/tikzlibrarytrees.code.tex

Is it the file, that I should change? Plz, help, it shouldn't be hard to add this feature.
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

Vertical alignment of subnodes in a tree

Post by usr345 »

Hi. I managed to create the first approximation to the desired diagram.

Image

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,scopes,trees}

\begin{document}
\centering

\begin{tikzpicture}[grow via three points={%
	one child at (2,-1) and two children at (2, -1) and (2, -2)},
	edge from parent fork right,
	]

	\node [draw, anchor=south west] {Node 1}
        child { node [draw] {Node 2} }
        child { node [draw] {Node 3} }
        child { node [draw] {Node 4}
        	child { node [draw, edge from parent fork down] {Node 4.1} }
        	child { node [draw] {Node 4.2} }
        }
    ;
\end{tikzpicture}

\end{document}
But how can I change the forking style and anchor on the fly to get this diagram?

Image
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

Vertical alignment of subnodes in a tree

Post by usr345 »

I wanted to position the nodes, so that there will be 1cm gap between them. But it seems, that tikz doesn't count the text height and padding inside the nodes. So, they overlapped. Does anybody know, how to fix?

Image

Code: Select all

\documentclass{article}
\usepackage[a4paper,landscape]{geometry}
\usepackage[cm]{fullpage}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel} 
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows,positioning}
\begin{document}

\def\len{1}
\begin{tikzpicture}[node distance=\len cm, auto]
\tikzset{
    main_node/.style={ellipse, draw=black, top color=white, bottom color=yellow!50,thick, inner sep=1em, minimum size=3em, text centered, double},
    subnode1/.style={rectangle,rounded corners,draw=black, top color=white, bottom color=yellow!50,very thick, inner sep=1em, minimum size=3em, minimum width=3.5cm, text width=3cm, text centered,sibling distance=8cm,},
    common_node/.style={rectangle,rounded corners,draw=black, minimum size=2em, text width=3cm},
    myarrow/.style={draw},
}

\colorlet{done}{blue}
\colorlet{need_testing}{red}


\node[main_node] (top) {Status of work};

	\node[subnode1, below=\len*2 cm of top] (node_1_1) {very very very very very long text text text};
	
	\node[common_node, below=\len of node_1_1.west, anchor=west] (node_1_1_0) {2\_1};

	\node[common_node, below=\len of node_1_1_0.west, anchor=west] (node_1_1_1) {2\_2};



	\node[subnode1, left = of node_1_1] (node_1_0) {1};
	
	\node[common_node, below=\len of node_1_0.west, anchor=west] (node_1_0_0) {1\_1};

	\node[common_node, below=\len of node_1_0_0.west, anchor=west] (node_1_0_1) {1\_2};



	\node[subnode1, right = of node_1_1] (node_1_2) {3};
	
	\draw[myarrow] (top.south) -- ++(0,-\len) -| (node_1_0.north);

	\draw[myarrow] (top.south) -- ++(0,-\len) -- (node_1_1.north);

	\draw[myarrow] (top.south) -- ++(0,-\len) -| (node_1_2.north);


\end{tikzpicture} 
\end{document}
Post Reply