在下面的例子中,为什么节点锚点的位置不一致?
\input tikz
\usetikzlibrary{arrows,shapes.multipart}
\tikzset{
font=\tt, >= stealth, every picture/.style={thick}, pointer/.style={*->},
box/.style={draw, inner sep=2ex, fill=black!10, rounded corners,
rectangle, rectangle split, rectangle split parts=2,
rectangle split ignore empty parts=false, rectangle split horizontal}}
\tikzpicture[every node/.append style={at end}]
\node[box] (1) {};
\draw[pointer] (1.text) -- +(0,-1) node[anchor=north,box] (2) {};
\draw[pointer] (2.text) -- +(0,-1) node[anchor=north] {1};
\draw[pointer] (2.two) -- +(0,-1) node[anchor=north] {2};
\draw[pointer] (1.two) -- +(1,0) node[anchor=base west,box] (3) {};
\draw[pointer] (3.text) -- +(0,-1) node[anchor=north] {3};
\draw[pointer] (3.two) -- +(0,-1) node[anchor=north] {4};
\endtikzpicture
\bye
理想情况下,我希望黑点正好位于中心。
答案1
我不确定你采取的方法是否正确。在 pgfmanual 中,你可以看到没有给出不同部分的中心。我认为某个部分(例如“一”)的中心是$(1.west)!.5!(1.one split)$
。我快速编写了下一个代码,可以编写一些更优雅的代码。
更新版本 2:我找到了一种创建每个部分中心坐标的方法,但我找不到如何使用样式创建圆圈。(c1 3)设计节点第一部分的中心(3)。代码中有一些修改,例如at end
\input tikz
\usetikzlibrary{arrows,shapes.multipart,calc}
\tikzset{
font=\tt,
>= stealth,
every picture/.style={thick},
box/.style={at end,
draw,
inner sep=2ex,
fill=black!10,
rounded corners,
rectangle,
rectangle split,
rectangle split parts=2,
rectangle split ignore empty parts=false,
rectangle split horizontal,
append after command={%
\pgfextra{%
\let\mainnode=\tikzlastnode}
coordinate (c1 \mainnode) at ($(\mainnode.west)!.5!(\mainnode.one split)$)
coordinate (c2 \mainnode) at ($(\mainnode.one split)!.5!(\mainnode.east)$)}}}
\tikzpicture
\node[box] (1) {};
\draw[->] (c1 1) -- +(0,-1) node[anchor=north,box] (2) {};
\draw[->] (c1 2) -- +(0,-1) node[anchor=north] {1};
\draw[->] (c2 2) -- +(0,-1) node[anchor=north] {2};
\draw[->] (c2 1) -- +(1,0) node[anchor=west,box] (3) {};
\draw[->] (c1 3) -- +(0,-1) node[anchor=north] {3};
\draw[->] (c2 3) -- +(0,-1) node[anchor=north] {4};
\foreach \i in {1,2,3}{\draw[fill=black] (c1 \i)circle(2pt) (c2 \i)circle(2pt);}
\endtikzpicture
\bye
与 Marc 解决方案的区别
\node[box] (1) { };
\draw[] (1.one|-1.west) circle(2pt);
\draw[fill=black] ($(1.west)!.5!(1.one split)$)circle(2pt);
给出
更新版本 3。可以直接绘制圆圈,但在这种情况下,您不能同时绘制箭头。代码很有趣,也许有人可以为箭头找到一个好主意(当我们使用时只有箭头box
)
\input tikz
\usetikzlibrary{arrows,shapes.multipart,calc}
\tikzset{
font=\tt,
>= stealth,
every picture/.style={thick},
box/.style={
draw,
inner sep=2ex,
fill=black!10,
rounded corners,
rectangle,
rectangle split,
rectangle split parts=2,
rectangle split ignore empty parts=false,
rectangle split horizontal,
append after command={%
\pgfextra{\let\mainnode=\tikzlastnode
\coordinate (c1 \mainnode) at ($(\mainnode.west)!.5!(\mainnode.one split)$);
\coordinate (c2 \mainnode) at ($(\mainnode.one split)!.5!(\mainnode.east)$);
\draw[fill=black] (c1 \mainnode)circle(2pt) (c2 \mainnode)circle(2pt);}
}}}
\tikzpicture
\node[box] (1) {};
\path (c1 1) -- +(0,-1) node[anchor=north,box] (2) {};
\draw[->] (c1 2) -- +(0,-1) node[anchor=north] {1};
\draw[->] (c2 2) -- +(0,-1) node[anchor=north] {2};
\path (c2 1) -- +(1,0) node[anchor=west,box] (3) {};
\draw[->] (c1 3) -- +(0,-1) node[anchor=north] {3};
\draw[->] (c2 3) -- +(0,-1) node[anchor=north] {4};
\draw[->] (c1 1) -- (2.north);
\draw[->] (c2 1) -- (3.west);
\endtikzpicture
\bye
我们得到了相同的结果。
答案2
看起来这是一个错误。有两个原因。指针样式导致箭头的起始位置错误。除此之外,方框文本部分内的定位看起来也可疑。下面我将展示如何使用普通箭头并计算(看起来是)文本部分的中心来解决这些问题two
。
\usetikzlibrary{calc}
\usetikzlibrary{arrows,shapes.multipart}
\begin{document}
\tikzset{
font=\tt, >= stealth, every picture/.style={thick}, pointer/.style={*->},
box/.style={draw, inner sep=2ex, fill=black!10, rounded corners,
rectangle, rectangle split, rectangle split parts=2,
rectangle split ignore empty parts=false, rectangle split horizontal}}
\tikzpicture[every node/.append style={at end}]
\node[box] (1) {};
\draw[->] (1.mid) -- +(0,-1) node[anchor=north,box] (2) {};
\draw[->] (2.mid) -- +(0,-1) node[anchor=north] {1};
\draw[->] (2.two|-2.east) -- +(0,-1) node[anchor=north] {2};
\draw[->] (1.two|-1.east) -- +(1,0) node[anchor=base west,box] (3) {};
\draw[->] (3.mid) -- +(0,-1) node[anchor=north] {3};
\draw[->] (3.two|-3.east) -- +(0,-1) node[anchor=north] {4};
\endtikzpicture