# Joining two branches of a family tree

I'm trying to write out a family tree for a friend, and I have a few minor issues.

My code is

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode.south)
-- +(0,-8pt)
-| (\tikzchildnode)}}}
\begin{tikzpicture}
\matrix
{
\node{\Tree
[.{}
[.{Robert - Margaret}
[.{Peter - Mary}
[.{Mary - W.P.}
[.{May - Robert}
[.{Robbie - Audrey} [.{Joan - Ewan} Isla ] [.{Gillian - Danny} Kenneth Joni Sarah Ruari ] [.{Angus - Michelle} Kirsteen Mikey ] ]]]
[.{Margaret} ]]]
[.{Robert - Janet}
[.{John - Anaple}
[.{Robert} ]]]]};\\
};
\end{tikzpicture}

\end{document}


The output looks like this:

There are a few minor issues I want to iron out.

1. Is there a way to adjust the tree so that it appears correctly in the output pdf? The scrollbar in the image is where the tree begins to spill over the margin.

2. In the first row, I don't want any bar connecting Robert - Margaret to Robert - Janet as they are not related.

3. I want to connect by marriage the Margaret and Robert of the third row, but I'm not sure how.

4. Lastly, is it possible to have the vertical connecting bars to connect at the - of each marriage, instead of the center of the whole couple?

Thank you,

-

It's hard to meet all of your requirements in an aesthetic way, but here's an attempt. Here first are some answers to your questions:

1. You can manually insert some negative space before the picture to move it to the left. (Not an ideal solution, but it works)
2. If you don't want nodes connected, put them into separate trees.
3. You can connect nodes in separate \Trees by using the \node command to insert the node, and connecting them with a \draw command. To have this work, you need to use the remember picture key for the whole tikzpicture environment; this allows reference to nodes across separate tikzpictures.
4. To have the bars connect at the centre of the marriage I've created a \marriage command which creates a fixed width tabular, with the -- as a rule. This ensures that the -- is always the centre point that the branch connects to. It's not possible to have the vertical line meet the horizontal line without a break; you can move the rule down with a negative value in its first argument if you want it to connect partially.

Here's a new version of your tree:

\documentclass{article}
\usepackage{array}
\usepackage{tikz-qtree}

% command to ensure the line connecting a marriage is centred
% change the rule command as needed; the width of the centre
% column should match the width of the rule
\newcommand*{\marriage}[2]{
\begin{tabular}{>{\raggedleft}p{.5in}@{}>{\centering}p{.2in}@{}>{}p{.5in}}%
#1 & \rule[3pt]{.2in}{\pgflinewidth} & #2
\end{tabular}}

\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode.base)
-- +(0,-8pt)
-| (\tikzchildnode)}}}
\hspace{-2in}
\begin{tikzpicture}[remember picture]

\matrix{
\node[anchor=north,inner sep=0pt]{\Tree
[.\marriage{Robert}{Margaret}
[.\marriage{Peter}{Mary}
[.\marriage{Mary}{W.P.}
[.\marriage{May}{Robert}
[.\marriage{Robbie}{Audrey} [.\marriage{Joan}{Ewan} Isla ]
[.\marriage{Gillian}{Danny} Kenneth Joni Sarah Ruari ]
[.\marriage{Angus}{Michelle} Kirsteen Mikey ] ]]]
[.\node[] (M) {Margaret}; ]]]};
&
\node[anchor=north,inner sep=0pt]{\Tree [.\marriage{Robert}{Janet}
[.\marriage{John}{Anaple}
[.\node[] (R) {Robert}; ]]] };\\
};
\draw (M) -- (M-|R.west);

\end{tikzpicture}

\end{document}


Update

The current version of the \marriage command uses a fixed width for the table cells that contain each name. Schematically, the table consists of Name1 <rule> Name2 and the width of the column containing the two names must be the same, otherwise the rule that is between the two names won't line up with the centre of the marriage.

Here's another version of the marriage command that is more dynamic: it adjusts the width of the columns to the largest of the two names. This will save some space for pairs of shorter names, compared to having a fixed width. It will also allow longer names, but with one downside: as the names get longer, they will cause the tree branches above those names to be wider to, which may lead to undesirable results.

\newlength{\widestname}

% command to ensure the line connecting a marriage is centred
% change the rule command as needed; the width of the centre
% column should match the width of the rule.
% The width of the left and right columns matches the width of the
% largest name.
\newcommand*{\marriage}[2]{
\pgfmathsetlength{\widestname}{max(\widthof{#1},\widthof{#2})}
\begin{tabular}
{>{\raggedleft}p{\widestname}@{}>{\centering}p{.2in}@{}>{}p{\widestname}}%
#1 & \rule[3pt]{.2in}{\pgflinewidth} & #2
\end{tabular}}

-
Thanks Alan, I didn't expect everything to be aesthetically compatible. Thanks for your solutions, I'll try these out when I get the chance. –  yunone Jul 17 '11 at 15:50
I tried adding last names with \marriage{Robert Balfour}{Margaret Clyde}, but the last names are put on a second line under the first name in the tree. Is there a way to keep the whole name on one line? –  yunone Jul 18 '11 at 1:15
This is controlled by the width of the two p{.5in} columns in the tabular within the \marriage command. But increasing the width of those will mess up the width of the entire tree, the branches will adapt to the total width of the tabular. To see the effect, try changing the width from .5in to 1in. I can't think of an obvious way around this at the moment. –  Alan Munn Jul 18 '11 at 3:18
@yunone P.S. I also fixed the alignment of the line between Margaret and Robert made with the \draw command. (Thanks, Jake.) You could also adapt the question using the scope mechanism that Jake mentions in his answer. –  Alan Munn Jul 18 '11 at 3:25

The unnecessary space on the left side of the tree comes from the fact that the \Tree is inside a \node (which in turn is inside a \matrix that only holds a single node). If you remove the \node{...}; from your code, the unnecessary space disappears:

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode.south)
-- +(0,-8pt)
-| (\tikzchildnode)}}}
\begin{tikzpicture}
\matrix
{
\Tree
[.{}
[.{Robert - Margaret}
[.{Peter - Mary}
[.{Mary - W.P.}
[.{May - Robert}
[.{Robbie - Audrey} [.{Joan - Ewan} Isla ] [.{Gillian - Danny} Kenneth Joni Sarah Ruari ] [.{Angus - Michelle} Kirsteen Mikey ] ]]]
[.{Margaret} ]]]
[.{Robert - Janet}
[.{John - Anaple}
[.{Robert} ]]]]\\
};
\end{tikzpicture}

\end{document}


Alan's answer can be adapted to work without the \matrix and the enclosing \node (trees can be shifted using scopes, which is also the way it is done in the examples in the TikZ-qtree manual):

\documentclass{article}
\usepackage{array}
\usepackage{tikz-qtree}

% command to ensure the line connecting a marriage is centred
% change the rule command as needed; the width of the centre
% column should match the width of the rule
\newcommand*{\marriage}[2]{
\begin{tabular}{>{\raggedleft}p{.5in}@{}>{\centering}p{.2in}@{}>{}p{.5in}}%
#1 & \rule[3pt]{.2in}{\pgflinewidth} & #2
\end{tabular}}

\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode.base)
-- +(0,-8pt)
-| (\tikzchildnode)}}}
\begin{tikzpicture}[]

\Tree
[.\marriage{Robert}{Margaret}
[.\marriage{Peter}{Mary}
[.\marriage{Mary}{W.P.}
[.\marriage{May}{Robert}
[.\marriage{Robbie}{Audrey} [.\marriage{Joan}{Ewan} Isla ]
[.\marriage{Gillian}{Danny} Kenneth Joni Sarah Ruari ]
[.\marriage{Angus}{Michelle} Kirsteen Mikey ] ]]]
[.\node[] (M) {Margaret}; ]]]

\begin{scope}[xshift=6cm]
\Tree [.\marriage{Robert}{Janet}
[.\marriage{John}{Anaple}
[.\node[] (R) {Robert}; ]]]
\end{scope}
\draw (M) -- (M-|R.west);

\end{tikzpicture}

\end{document}


Below is a screenshot of the trees without matrix and node, using Alan's dynamic marriage macro. The nodes and background rectangle are made visible to show where the whitespace comes from. Note that the tree is too wide to fit on the page completely, so one should either try to make the marriage macro more space efficient still, use a slightly smaller font size, or use the \makebox approach described in the answer to Balancing figure that is wider than document's width

\documentclass{article}
\usepackage{array}
\usepackage{tikz-qtree}
\usetikzlibrary{backgrounds}

\newlength{\widestname}

% command to ensure the line connecting a marriage is centred
% change the rule command as needed; the width of the centre
% column should match the width of the rule.
% The width of the left and right columns matches the width of the
% largest name.
\newcommand*{\marriage}[2]{
\pgfmathsetlength{\widestname}{max(\widthof{#1},\widthof{#2})}
\begin{tabular}
{>{\raggedleft}p{\widestname}@{}>{\centering}p{.2in}@{}>{}p{\widestname}}%
#1 & \rule[3pt]{.2in}{\pgflinewidth} & #2
\end{tabular}}

\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode.base)
-- +(0,-8pt)
-| (\tikzchildnode)}}}
\begin{tikzpicture}[show background rectangle, tight background,every node/.style=draw]

\Tree
[.\marriage{Robert}{Margaret}
[.\marriage{Peter}{Mary}
[.\marriage{Mary}{W.P.}
[.\marriage{May}{Robert}
[.\marriage{Robbie}{Audrey} [.\marriage{Joan}{Ewan} Isla ]
[.\marriage{Gillian}{Danny} Kenneth Joni Sarah Ruari ]
[.\marriage{Angus}{Michelle} Kirsteen Mikey ] ]]]
[.\node[] (M) {Margaret}; ]]]

\begin{scope}[xshift=6cm]
\Tree [.\marriage{Robert}{Janet}
[.\marriage{John}{Anaple}
[.\node[] (R) {Robert}; ]]]
\end{scope}
\draw (M) -- (M-|R.west);

\end{tikzpicture}

\end{document}

-
Unfortunately this doesn't solve the leftward shift problem. (the revised version of my solution still has the \hspace{-2in} command). –  Alan Munn Jul 18 '11 at 3:07
@Alan: This is a different issue: This unnecessary whitespace is caused by the fact that the tabular created by the marriage macro assigns a fixed width to all couples, which happens to be a bit too generous for "Joan and Ewan". You can see that the whitespace is introduced by this by setting show background rectangle,every node/.style=draw in the tikzpicture options (after loading \usetikzlibrary{backgrounds}. –  Jake Jul 18 '11 at 3:29
Ok. Yes, that's a problem. I could fix the \marriage command to be more dynamic so that it adjusts the tabular to the size of its arguments. –  Alan Munn Jul 18 '11 at 3:33
That can't be the whole story of the rightward shift. Even with a dynamic version of the command, the whole tree is still quite shifted. –  Alan Munn Jul 18 '11 at 4:01
I'm not sure I know what you mean. If I replace the marriage macro with your new version, the whole tree slims down a bit and moves a bit to the left. It doesn't move as far left as with the manual hspace, but that's just because of the large left page border (the classic "How do I center a really wide image" problem, tex.stackexchange.com/questions/16582/…) –  Jake Jul 18 '11 at 4:23