# How can I improve this family tree in TikZ?

To answer a question about how to draw family tree on LaTeX-Community.org, I created an example:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[
man/.style={rectangle,draw,fill=blue!20},
woman/.style={rectangle,draw,fill=red!20,rounded corners=.8ex},
grandchild/.style={grow=down,xshift=1em,anchor=west,
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
first/.style={level distance=6ex},
second/.style={level distance=12ex},
third/.style={level distance=18ex},
level 1/.style={sibling distance=5em}]
% Parents
\coordinate
child[grow=left] {node[man,anchor=east]{Jim}}
child[grow=right] {node[woman,anchor=west]{Jane}}
child[grow=down,level distance=0ex]
[edge from parent fork down]
% Children and grandchildren
child{node[man] {Alfred}
child[grandchild,first] {node[man]{Joe}}
child[grandchild,second] {node[woman]{Heather}}
child[grandchild,third] {node[woman] {Barbara}}}
child{node[woman] {Berta}
child[grandchild,first] {node[man]{Howard}}}
child {node[man] {Charles}}
child {node[woman]{Doris}
child[grandchild,first] {node[man]{Nick}}
child[grandchild,second] {node[woman]{Liz}}};
\end{tikzpicture}
\end{document}

How can this tree be improved? Some things don't seem perfect, such as

• starting with \coordinate as root node,
• shifting the parent nodes,
• specifying an explicit level distance for the grand children,
• vertical edges are overlapping.
-
Perhaps this question: Joining two branches of a family tree might give you some ideas. – Alan Munn Dec 30 '11 at 21:52
Instead of \coordinate, you can use \node[inner sep=0pt](root) {}. In your tree, some edges are drawn twice or three times. The method to build this schema is perhaps wrong. This is not exactly a family tree. For one generation, childs are on a horizontal line and then for the next generation on a vertical line. There is something I don't understand . – Alain Matthes Dec 31 '11 at 6:50
I concur with Altermundus. Is there a reason for the vertical placing? Perhaps horizontal space is expensive. – qubyte Dec 31 '11 at 7:38
@Altermundus Yes, usually horizontal space is more limited, both on paper and even more on vertical scrolling e-reading devices. Further, I'm generally interested in making organizational charts, not strictly with generations on a line. And the original question on LaTeX-Community.org was like this. – Stefan Kottwitz Dec 31 '11 at 12:33

Some marginal improvements can be achieved by setting the width of all the boxes, as I have done below. However, this particular choice of tree-structure (even for an organization chart is difficult to accomodate on an A3 or A4 paper). Imagine the grandchildren spaced horizontally and we are in trouble if we need to grow the tree one further level.

A better approach is to draw such charts as a directory tree. These type of grids are more economical in terms of horizontal spacing. Here is an example drawn using dirtree.

Since the figure mostly consist of text, the using of boxing is extraneous in the example above, however the dirtree can easily be extended to hold tikz nodes rather than text. Another advantage of the above is that in the particular example I have shown in the illustration information was captured in a more intuitive and semantic way,

This also permitted automatic recalculation of totals and the production of a summary table. This is perhaps not a full answer, but you are asking to produce a layout which will break in most circumstances. The proper solution is to produce a dirtree type solution either with dirtree or with TikZ following such a pattern.

The MWE would produce the image above.

\documentclass{minimal}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{trees}
\def\name#1{\hbox to 50pt{#1\rule{10pt}{0pt}}}
\begin{document}
\begin{tikzpicture}[
man/.style={rectangle,draw,fill=gray!30},
woman/.style={rectangle,draw,fill=gray!10},
grandchild/.style={grow=down,xshift=1em,anchor=west,
edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}},
first/.style={level distance=6ex},
second/.style={level distance=12ex},
third/.style={level distance=18ex},
level 1/.style={sibling distance=70pt}]
% Parents
\coordinate
child[grow=left] {node[man,anchor=east]{\name{Jim}}}
child[grow=right] {node[woman,anchor=west]{\name{Jane}}}
child[grow=down,level distance=0ex]
[edge from parent fork down]
% Children and grandchildren
child{node[man] {\name{Alfred}}
child[grandchild,first] {node[man]{\name{Joe}}}
child[grandchild,second] {node[woman]{\name{Heather}}}
child[grandchild,third] {node[woman] {\name{Barbara}}}}
child{node[woman] {\name{Berta}}
child[grandchild,first] {node[man]{\name{Howard}}}}
child {node[man] {\name{Charles}}
child[grandchild,first] {node[man]{\name{Howard}}}}
child {node[woman]{\name{Doris}}
child[grandchild,first] {node[man]{\name{Nick}}}
child[grandchild,second] {node[woman]{\name{Liz}}}};
\end{tikzpicture}
\end{document}
-
I'm also trying to draw a family tree (without any real knowledge of tikz) and I found this very useful. I have one question though. How do I draw a marriage connection such as the one seen between Jim and Jane further down in the tree (on the level of the children or the grandchildren)? – Mårten Apr 22 '13 at 19:31
@Mårten I am a bit busy now, will you post this as a new question and I am sure someone will come up with an answer very quickly. – Yiannis Lazarides Apr 22 '13 at 21:40
Sure, I just did. – Mårten Apr 23 '13 at 1:59

I can provide a new answer based on a new package named genealogytree. On the time of writing, the version needed is 0.90 (2015/05/22). This package was specifically designed to typeset such kind of diagrams. For auto-layout, a layered graph has to be used. Therefore, I put the grandchildren in one layer:

\documentclass{article}
\usepackage[all]{genealogytree}

\begin{document}

\begin{genealogypicture}[
timeflow=down,               % time flows down
processing=tcbox*,           % draw nodes with tcolorbox
node size from=8mm to 5cm,   % width of nodes
level size=6mm,              % height of nodes
level distance=8mm,          % generation distance
% redefine default setting for female,male,neuter:
tcbset={
female/.style={colback=red!20,arc=1mm},
male/.style={colback=blue!20,sharp corners},
},
%
box={colback=black,size=fbox,before upper=\strut},    % node settings
%
edges={foreground={black,line width=0.25mm},    % edge settings
background={white,line width=0.5mm}}
]

%%%%%% the graph %%%%%%
child{
g[male]{Jim}
p[female]{Jane}
child{
g[male]{Alfred}
c[male]{Joe}
c[female]{Heather}
c[female]{Barbara}
}
child{
g[female]{Berta}
c[male]{Howard}
}
c[male]{Charles}
child{
g[female]{Doris}
c[male]{Nick}
c[female]{Liz}
}
}
\end{genealogypicture}

\end{document}

-
This is an excellent package! – Yiannis Lazarides Jun 8 '15 at 3:54
@YiannisLazarides Thank you :-) – Thomas F. Sturm Jun 8 '15 at 6:04

Just a simple way to draw a directory-style tree using forest:

\documentclass[tikz, border=5pt, multi]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{forest}
for tree={
parent anchor=south west,
child anchor=south west,
anchor=mid west,
inner sep=1pt,
l sep+=-1.5em,
grow'=0,
align=left,
draw=gray,
rounded corners=2pt,
inner color=gray!10,
outer color=gray!20,
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
},
edge={gray},
text=gray!50!black,
font=\sffamily,
if level=1{
edge={{Circle[length=2pt]}-},
}{
edge={shorten <=-1pt, {Circle[length=2pt]}-{Circle[length=2pt]}}
},
if n children=0{}{
delay={
prepend={[,phantom, calign with current]}
}
},
before computing xy={
l=15pt
}
},
[root
[child 1
[child 1's child 1]
[child 1's child 2]
[child 1's child 3]
]
[child 2
[child 2's child 1]
[child 2's child 2]
[child 2's child 3]
]
[child 3
[child 3's child 1]
[child 3's child 2]
[child 3's child 3]
]
]
\end{forest}
\end{document}

-