我想通过将 TikZ 树视为一个带有锚点的节点来将其与另一个节点对齐。目前,我只能对齐树的根节点,而不能对齐整个树本身。我使用 PGF 2.10。
下图显示了我所拥有的:
我想要的是左节点和树水平对齐,并且箭头从左节点的东锚点指向树的西锚点,即或多或少是根节点的左子节点,如下所示:
请忽略两张图片的外边框。
你知道怎样实现这个目标吗?
我的测试代码如下。我用它编译xelatex
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning,trees}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength{\imagewidth}
\newlength{\imagescale}
\begin{document}
\begin{tikzpicture}[
->, >=stealth', shorten >=1pt, semithick, node distance=1cm, level distance=7mm, level/.style={sibling distance=10mm/#1},
block/.style = {draw, rectangle, rounded corners, minimum height=1cm},
every node/.style={circle, draw, fill=none, anchor=north}
]
\node [block] (SOURCE DOCUMENT) {\parbox{2cm}{\centering source document}};
\node (SOURCE TREE)[right=of SOURCE DOCUMENT] {}
child { node {} }
child { node {}
child { node {} }
child { node {} }
};
\draw [->] (SOURCE DOCUMENT) edge (SOURCE TREE);
\end{tikzpicture}
\end{document}
答案1
使用fit
库,您可以创建附加节点,该节点将包含树中的每个节点。之后,使用它的锚点来执行您的调整。
该解决方案的结果如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning,trees,fit}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newlength{\imagewidth}
\newlength{\imagescale}
\begin{document}
\begin{tikzpicture}[
->, >=stealth', shorten >=1pt, semithick, node distance=1cm, level distance=7mm, level/.style={sibling distance=10mm/#1},
block/.style = {draw, rectangle, rounded corners, minimum height=1cm},
every node/.style={circle, draw, fill=none, anchor=north}
]
\node (SOURCE TREE) {}
child { node {} }
child { node {}
child { node {} }
child { node {} }
};
\node[fit=(SOURCE TREE) (SOURCE TREE-1) (SOURCE TREE-2) (SOURCE TREE-2-1) (SOURCE TREE-2-2),
draw=none, rectangle, inner sep=0] (whole tree) {};
\node[block, left=of whole tree] (SOURCE DOCUMENT) {\parbox{2cm}{\centering source document}};
\draw (SOURCE DOCUMENT) -- (whole tree);
\end{tikzpicture}
\end{document}
答案2
不要将树放在框的右侧,而是将框放在(选定的、适当命名的)子节点的左侧:
...
\node (SOURCE TREE) {}
child { node (x) {} }
child { node {}
child { node {} }
child { node {} }
};
\node [block,left=of x] (SOURCE DOCUMENT) {\parbox{2cm}{\centering source document}};
\draw [->] (SOURCE DOCUMENT) edge (x);
...