3

Suppose, I have 2 qtrees, that I want to put below each other:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree,tikz-qtree-compat}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\Tree
[.{1}
    [.1-1 ]
    [.\node (1) {1-2}; ]
]
\node [below = 0.5cm of 1.south] (note-1) {la la la};
\draw[-]
      (note-1.north) -- (1.south);
\Tree
[.{2}
    [.2-1 ]
    [.2-2 ]
]
\end{tikzpicture}
\end{document}

enter image description here

Now trees overlap. The 1-st has a comment 'la la la', residing in a separate node. If I didn't have it, I could put both trees into nodes and position nodes. But how to position bare qtrees? So their roots would match horizontally and the 2-nd tree would be below 'la la la'.

user4035
  • 5,035
  • 6
  • 40
  • 57
  • 1
    Can you put the two trees in two different tikzpicture environments, and add a line break between them? For example, \begin{tikzpicture} <codes for 1st tree> \end{tikzpicture} \\ \begin{tikzpicture} <codes for 2nd tree> \end{tikzpicture} – Herr K. Apr 03 '13 at 20:59

2 Answers2

6

You can use a scope with an appropriate shifting for the second tree:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-qtree,tikz-qtree-compat}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\Tree
[.1
    [.1-1 ]
    [.\node (1) {1-2}; ]
]
\node [below = 0.5cm of 1.south] (note-1) {la la la};
\draw[-]
      (note-1.north) -- (1.south);
\begin{scope}[yshift=-3cm]
\Tree
[.2
    [.2-1 ]
    [.2-2 ]
]
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Gonzalo Medina
  • 505,128
2

Besides just using two different TikZ pictures that are separated by \\, you can use the manual added node note-1 to shift the whole second tree down.

You can also use an already defined node of the Tree but you need to re-establish its coordinates by the help of an auxiliary coordinate (Code 2):

% somewhere in the tree:
          [. \node (lowest node) {1-2-2-1-1};  ]

% outside of tree:
\coordinate (aux1) at (lowest node);

% The coordinate aux1 can now be used for placement, e.g.
% below=of toptree |- aux1

The |- makes sure that the 2 is placed under the 1.

Code 1

\documentclass[tikz]{standalone}
\usepackage{tikz,tikz-qtree,tikz-qtree-compat}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\Tree
[.\node (toptree) {1};
    [.1-1 ]
    [.\node (1) {1-2}; ]
]
\node [below = 0.5cm of 1.south] (note-1) {la la la};
\draw[-] (note-1.north) -- (1.south);

\begin{scope}[every tree node/.append style={below=of toptree |- note-1.south}]
\Tree
[.2
    [.2-1 ]
    [.2-2 ]
]
\end{scope}
\end{tikzpicture}
\end{document}

Code 2

\documentclass[tikz]{standalone}
\usepackage{tikz,tikz-qtree,tikz-qtree-compat}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\Tree
[.\node (toptree) {1};
    [.1-1 ]
    [.1-2 
      [.1-2-1 
        [.1-2-2-1 
          [. \node (lowest node) {1-2-2-1-1};  ]
        ]
      ]
    ]
]
\coordinate (aux1) at (lowest node);

\begin{scope}[opacity=.5,every tree node/.append style={below=of toptree |- lowest node}]
\Tree
[.2
    [.2-1 ]
    [.2-2 ]
]
\end{scope}
\begin{scope}[every tree node/.append style={below=of toptree |- aux1}]
\Tree
[.2
    [.2-1 ]
    [.2-2 ]
]
\end{scope}
\end{tikzpicture}
\end{document}

Output

enter image description here enter image description here

Qrrbrbirlbel
  • 119,821
  • The toptree node isn't even needed as lowest node refers in below=of lowest node |- aux1 to the first node in the tree. – Qrrbrbirlbel Apr 03 '13 at 23:00