Joining two branches of a family tree
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:
- You can manually insert some negative space before the picture to move it to the left. (Not an ideal solution, but it works)
- If you don't want nodes connected, put them into separate trees.
- You can connect nodes in separate
\Tree
s by using the\node
command to insert the node, and connecting them with a\draw
command. To have this work, you need to use theremember picture
key for the wholetikzpicture
environment; this allows reference to nodes across separatetikzpictures
. - 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}}
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 scope
s, 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 Center figure that is wider than \textwidth
\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}