如下所示,使用时节点位置发生了变化chain
。如何修复?
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning, chains}
\begin{document}
\begin{tikzpicture}[start chain = arc]
% with chain
\foreach \p in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}} {
\node [draw, circle, minimum size = 6pt, on chain, join] at (\p) {};
}
% without chain
\begin{scope}[yshift = -3cm]
\foreach \p in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}} {
\node [draw, circle, minimum size = 6pt] at (\p) {};
}
\end{scope}
\end{tikzpicture}
\end{document}
答案1
当你在链名称后以或 的形式指定不指定方向(即放置规则)时,链将使用going direction
placed direction
chain default direction
也就是going right
。
这意味着,对于每个节点(第一个节点除外),将执行right = of \tikzchainprevious
(库的)选项。这本质上是:positioning
at = (\tikzchainprevious.east)
,xshift = node distance
和anchor = west
。
由于您使用了at (\p)
,第一个设置会被覆盖,但变换和锚点都会保留。
由于默认node distance
值为 1cm,因此第一个节点之后的每个节点都将向右移动该距离,并将其西锚点放置在那里。
当向图表中添加网格时,可以很好地看到这一点,因为节点现在与左侧的垂直线接触。
如果您想使用chains
不带自动放置功能的库,则无需使用任何内容direction
。
或者,您也可以使用=
方向,但=
由于 PGFKeys 的工作方式,以及没有什么需要特别注意的。
你会需要
start chain = {arc going }
,start chain = arc going {}
,start chain = arc going {=}
或者start chain = {arc going =}
。
如果方向包含=
那么它将在直接放置节点时被执行(而不会变成= of \tikzchainprevious
)。
但是 PGFKeys 会默认忽略这两种情况,因为它看到的是空的键名。
代码
\documentclass[tikz]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[
every on chain/.append style=join,
nodes={draw, circle, minimum size = 6pt}
]
\draw[help lines] (-.25,-6.25) grid (7.5,2.5);
\scoped[start chain = arc, nodes=on chain]
\foreach \p in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}}
\node at (\p) {};
\scoped[start chain = arc going {}, nodes = on chain, yshift=-3cm]
\foreach \p in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}}
\node at (\p) {};
\scoped[yshift=-6cm]
\foreach \p in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}}
\node at (\p) {};
\end{tikzpicture}
\end{document}
输出
答案2
链带有预定义的节点距离。如果将其设置为 0,或者更好的是,设置为适当的负距离,则可以避免移位。这个适当的距离是多少?您为节点指定最小尺寸6pt
,但您也进入 45 度方向。这表明这个距离6pt/sqrt(2)
加上一些外部间隔。至少从数值上看,这似乎相当合适。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning, chains,calc}
\begin{document}
\begin{tikzpicture}[start chain = arc]
% with chain, no distance modified
\foreach \p [count=\X] in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}} {
\node [draw, circle, minimum size = 6pt, on chain, join] at (\p) (u-\X) { };
\draw let \p1=(u-\X) in node[anchor=south] at (u-\X.north) {\x1}
node[anchor=north] at (u-\X.south) {\y1};
}
% chain, node distance set to 0
\begin{scope}[yshift =-3cm,start chain=arc2,node distance=0pt]
\foreach \p [count=\X] in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}} {
\node [draw, circle, minimum size = 6pt, on chain, join] at (\p) (m-\X) { };
\draw let \p1=(m-\X) in node[anchor=south] at (m-\X.north) {\x1}
node[anchor=north] at (m-\X.south) {\y1};
}
\end{scope}
% chain,
\begin{scope}[yshift =-6cm,start chain=arc3,node distance={-(6/sqrt(2))*1pt
-2*\pgfkeysvalueof{/pgf/outer xsep}-\pgflinewidth/sqrt(2)}]
\foreach \p [count=\X] in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}} {
\node [draw, circle, minimum size = 6pt, on chain, join] at (\p) (n-\X) { };
\draw let \p1=(n-\X) in node[anchor=south] at (n-\X.north) {\x1}
node[anchor=north] at (n-\X.south) {\y1};
}
\end{scope}
% without chain
\begin{scope}[yshift = -9cm]
\foreach \p [count=\X] in {{0,0}, {1,1}, {2,1.5}, {3,2}, {4, 1.5}, {5,1}, {6,0}} {
\node [draw, circle, minimum size = 6pt] at (\p)
(l-\X) {};
\draw let \p1=(l-\X) in node[anchor=south] at (l-\X.north) {\x1}
node[anchor=north] at (l-\X.south) {\y1};
}
\end{scope}
\end{tikzpicture}
\end{document}
我不知道这是否是完整的解释。