我正在尝试通过使用相对位置手动放置节点来构建图表。但我在放置节点的方式上遇到了一些问题。我尝试将其包含在 MWE 中:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex}
]
\small
\node (a) [test node] {First node};
\node (b) [test node, above right=of a] {Upper branch 1};
\node (c) [test node, below right=of a] {Lower branch 1};
\begin{scope}[red]
\foreach \pos/\n in {above right/x, right/y, below right/z}
{
\node (\n) [circle, draw, \pos=of a] {};
\foreach \a in {north, center, south}
{
\draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
}
}
\foreach \n in {x, y, z}
\draw (0,2 -| \n.center) -- ++(0,-4);
\node [circle, draw, below=of a] at (3,0) {};
\end{scope}
\end{tikzpicture}
\end{document}
如您所见,圆圈和文本节点相对于第一个节点的位置不一致。其次,三个圆圈在水平方向上不对齐。第三个“额外”之处是,最后一个圆圈与按键below
和 x 方向的明确位置相结合放置,第三个方面也不一致。
无需手动添加坐标,正确的放置这些节点(具有不同的形状和不同的放置方式)的方法是什么?我需要组合不同的形状,并且需要明确设置一些 x 坐标以正确分布不同分支中的节点...
编辑:澄清一下:我想垂直对齐圆形和矩形,并且我想水平对齐它们的西锚点。
答案1
\node (b) [test node, above right=of a] {Upper branch 1};
设置anchor
节点b
为south west
不是到west
。然后b.south west
位于 的右侧 1cm 和 的上方 1cm 处。如果节点是一个圆,则锚点和之间存在 - 方向a.north east
的差异。x
south west
west
如果的锚点b
应该是west
,则south west
必须使用anchor=west
后选项above right=of a
。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex},
node distance=1cm and 1cm
]
\small
\node (a) [test node] {First node};
\node (b) [test node, above right=of a,anchor=west] {Upper branch 1};
\node (c) [test node, below right=of a,anchor=west] {Lower branch 1};
\begin{scope}[red]
\foreach \pos/\n in {above right/x, right/y, below right/z}
{
\node (\n) [circle, draw, \pos=of a,anchor=west] {};
\foreach \a in {north, center, south}
{
\draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
}
}
\foreach \n in {x, y, z}
\draw (0,2 -| \n.center) -- ++(0,-4);
\node [circle, draw, below=of a] at (3,0) {};
\end{scope}
\draw[green](a.east)--+(1,0);
\draw[blue](a.north east)--++(1,0)--+(0,1);
\draw[orange](a.south east)--++(1,0)--+(0,-1);
\end{tikzpicture}
\end{document}
也许你想将west
锚点相对于east
以下锚点进行定位a
:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex},
node distance=1cm and 1cm
]
\small
\node (a) [test node] {First node};
\node (b) [test node, above right=of a.east,anchor=west] {Upper branch 1};
\node (c) [test node, below right=of a.east,anchor=west] {Lower branch 1};
\begin{scope}[red]
\foreach \pos/\n in {above right/x, right/y, below right/z}
{
\node (\n) [circle, draw, \pos=of a.east,anchor=west] {};
\foreach \a in {north, center, south}
{
\draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
}
}
\foreach \n in {x, y, z}
\draw (0,2 -| \n.center) -- ++(0,-4);
\node [circle, draw, below=of a] at (3,0) {};
\end{scope}
\draw[green](a.east)--+(1,0);
\draw[blue](a.east)++(1,0)--+(0,1);
\draw[orange](a.east)++(1,0)--+(0,-1);
\draw[purple](a.center)--++(0,-1)--+(3,0);
\end{tikzpicture}
\end{document}