如何定位 qtree

如何定位 qtree

假設我有 2 個 qtree,我想將它們放在一起:

\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}

在此輸入影像描述

現在樹木重疊了。第一個有一個評論“la la la”,位於一個單獨的節點中。如果我沒有它,我可以將兩棵樹放入節點並定位節點。但如何定位裸 qtree?因此,它們的根將水平匹配,第二棵樹將位於“la la la”下方。

答案1

除了僅使用由 分隔的兩個不同的 TikZ 圖片之外\\,您還可以使用手動新增的節點note-1將整個第二棵樹向下移動。

您也可以使用已定義的樹節點,但需要藉助輔助座標重新建立其座標(代碼 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

確保|-2放置在 下方1

代碼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}

代碼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}

輸出

在此輸入影像描述 在此輸入影像描述

答案2

您可以scope對第二棵樹使用適當的移位:

\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}

在此輸入影像描述

相關內容