TikZ 中的“网格”节点定位实际上是如何工作的?

TikZ 中的“网格”节点定位实际上是如何工作的?

我以为我知道on gridTikZ 选项(参见 2.10 版手册第 16.5.3 节第 186-187 页)是如何工作的,但我得到的结果与我的预期不同。

我有一个简单的例子:

\documentclass{article}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \begin{scope}[red]
    \coordinate (a) at (0,0);
    \node[below=of a,draw] (anode) {};
    \draw[-|] (anode) -- (a);
    \node (b) at (.3,0) {};
    \node[below=of b,draw] (bnode) {};
    \draw[-|] (bnode) -- (b);
  \end{scope}
  \begin{scope}[green,dashed]
    \coordinate (a) at (0,0);
    \node[on grid,below=of a,draw] (anode) {};
    \draw[->] (anode) -- (a);
    \node (b) at (.3,0) {};
    \node[on grid,below=of b,draw] (bnode) {};
    \draw[->] (bnode) -- (b);
  \end{scope}
\end{tikzpicture}
\end{document}

其结果如下图所示:

“网格”节点定位选项的图示

我本来期望(并希望……)左侧的绿色虚线节点与右侧的绿色虚线节点位于相同的高度。(据我所知,所有其他图片元素都是正确的。)从手册中的描述,我不明白为什么不是这样:

当您将above=1cm of somenode设置on grid为 true 时,新节点将以这样的方式放置,即它的中心位于 中心上方 1cm 处somenode

我的例子中唯一非标准的事情是参考节点是一个coordinate,但我不明白为什么它会导致观察到的行为,因为这只是一个只有中心的节点。

我将非常感激对观察到的行为的解释,并且也许能够找到解决方法来获得我期望的行为。

答案1

除非您自己将其设置为零,否则节点将具有,inner sep而坐标则没有。并且此外,anode不会创建几何点。inner sep=0您的代码将提供所有

\documentclass{article}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \begin{scope}[red]
    \coordinate (a) at (0,0);
    \node[inner sep=0,below=of a,draw] (anode) {};
    \draw[-|] (anode) -- (a);
    \node[inner sep=0] (b) at (.3,0) {};
    \node[inner sep=0,below=of b,draw] (bnode) {};
    \draw[-|] (bnode) -- (b);
  \end{scope}
  \begin{scope}[green,dashed]
    \coordinate (a) at (0,0);
    \node[inner sep=0,on grid,below=of a,draw] (anode) {};
    \draw[->] (anode) -- (a);
    \node[inner sep=0] (b) at (.3,0) {};
    \node[inner sep=0,on grid,below=of b,draw] (bnode) {};
    \draw[->] (bnode) -- (b);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

inner sep请注意,即使设置为零,右侧的水平红线也低于左侧的水平红线。(更改\node[inner sep=0] (b) at (.3,0) {};\coordinate (b) at (.3,0);将修复此问题,并且这是让事情正确的选择之一

更换

\coordinate (a) at (0,0);

\node[inner sep=0cm] (a) at (0,0) {};

在范围内,给出

\documentclass{article}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \begin{scope}[red]
    \coordinate (a) at (0,0);
    %\node[] (a) at (0,0) {};
    \node[inner sep=0,below=of a,draw] (anode) {};
    \draw[-|] (anode) -- (a);
    \node[inner sep=0] (b) at (.3,0) {};
    \node[inner sep=0,below=of b,draw] (bnode) {};
    \draw[-|] (bnode) -- (b);
  \end{scope}
  \begin{scope}[green,dashed]
    %\coordinate (a) at (0,0);
    %\node[] (a) at (0,0) {};
    \node[inner sep=0cm] (a) at (0,0) {};
    \node[inner sep=0,on grid,below=of a,draw] (anode) {};
    \draw[->] (anode) -- (a);
    \node[inner sep=0] (b) at (.3,0) {};
    %\coordinate (b) at (.3,0);
    \node[inner sep=0,on grid,below=of b,draw] (bnode) {};
    \draw[->] (bnode) -- (b);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果没有inner sep = 0我们将会得到,

\documentclass{article}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \begin{scope}[red]
    \coordinate (a) at (0,0); 
    \node[below=of a,draw] (anode) {};
    \draw[-|] (anode) -- (a);
    \node[] (b) at (.3,0) {};
%    \coordinate (b) at (.3,0);
    \node[below=of b,draw] (bnode) {};
    \draw[-|] (bnode) -- (b);
  \end{scope}
  \begin{scope}[green,dashed]
    %\coordinate (a) at (0,0);
    %\node[] (a) at (0,0) {};
    \node (a) at (0,0) {};
    \node[on grid,below=of a,draw] (anode) {};
    \draw[->] (anode) -- (a);
    \node (b) at (.3,0) {};
    %\coordinate (b) at (.3,0);
    \node[on grid,below=of b,draw] (bnode) {};
    \draw[->] (bnode) -- (b);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

选项 -2在您的代码中替换\node[inner sep=0] (b) at (.3,0) {};\coordinate (b) at (.3,0);

\documentclass{article}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \begin{scope}[red]
    \coordinate (a) at (0,0);
    \node[below=of a,draw] (anode) {};
    \draw[-|] (anode) -- (a);
    \coordinate (b) at (.3,0);
    \node[below=of b,draw] (bnode) {};
    \draw[-|] (bnode) -- (b);
  \end{scope}
  \begin{scope}[green,dashed]
    \coordinate (a) at (0,0);
    \node[on grid,below=of a,draw] (anode) {};
    \draw[->] (anode) -- (a);
    \coordinate (b) at (.3,0);
    \node[on grid,below=of b,draw] (bnode) {};
    \draw[->] (bnode) -- (b);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我不明白为什么事实并非如此:

当您将网格设置为 true 并将 above=1cm of somenode 设置为 true 时,新节点将被放置在其中心位于 somenode 中心上方 1cm 的位置。

我的例子中唯一非标准的事情是参考节点是一个坐标,但我不明白为什么它会导致观察到的行为,因为这只是一个只有中心的节点。

鉴于以上所有事实,我不知道该如何回答这个问题。也许一些专家可以对此作出解释。

答案2

问题是,如果属于而不是 ,那么 网格 和below = of <name>似乎没有任何效果(参见底部的摘要图)。您可以使用而不是(我画了一个网格只是为了视觉帮助):<name>\coordinate\node\nodeinner sep=0pt\coordinate

\documentclass{article}
\usepackage{tikz}
  \usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \draw[help lines,gray!20] (-1,-2) grid[step=2mm] (1,1);
  \begin{scope}[red]
    \coordinate (a) at (0,0);
    \node[below=of a,draw] (anode) {};
    \draw[-|] (anode) -- (a);
    \node (b) at (.3,0) {};
    \node[below=of b,draw] (bnode) {};
    \draw[-|] (bnode) -- (b);
  \end{scope}
  \begin{scope}[green,dashed]
    \node[inner sep=0pt] (a) at (0,0) {};
    \node[on grid,below = of a,draw] (anode) {};
    \draw[->] (anode) -- (a);
    \node (b) at (.3,0) {};
    \node[on grid,below = of b,draw] (bnode) {};
    \draw[->] (bnode) -- (b);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

下图总结了由s 和son grid的所有可能组合形成的对的选项行为。绘制了节点(图中的正方形);使用小圆圈来显示 s 的中心和s 的位置。\coordinate\node\node\coordinate

可以看出,below = of <name><name>属于某个坐标时,使用 会使on grid不起作用(图上的红色十字),这解释了原始代码的问题:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[scale=1.3,transform shape,node distance=1cm]
\draw[help lines,gray!20] (-0.5,-2) grid[step=2mm] (7.5,1);
\draw[help lines] (-0.5,0) -- +(-0.6,0);
\draw[help lines] (-0.5,-1) -- +(-0.6,0);
\draw[help lines,<->] (-0.8,0) -- node[fill=white] {\tiny 1cm} (-0.8,-1);

\node[align=left,anchor=south] at (3.5,1) {behaviour of \texttt{on grid} with nodes and coordinates};

% a and b are coordinates (on grid true or false produce the same result)
\coordinate (a) at (0,0);
\coordinate[below=of a] (b);
\foreach \point in {a,b}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}

% a and b are coordinates (on grid true or false produce the same result)
\coordinate (a) at (1,0);
\coordinate[on grid,below=of a] (b);
\foreach \point in {a,b}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}

% c is a coordinate and d is a node
\coordinate (c) at (2,0);
\node[draw,below=of c] (d) {};
\foreach \point in {c,d}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}

% c is a coordinate and d is an on grid node (on grid has no effect)
\coordinate (c) at (3,0);
\node[draw,on grid,below = of c] (d) {};
\foreach \point in {c,d}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}
\draw[thick,red] ([yshift=-15pt]d.north west) -- ([yshift=-15pt]d.south east);
\draw[thick,red] ([yshift=-15pt]d.north east) -- ([yshift=-15pt]d.south west);

% e is a node and f is a coordinate
\node[draw] (e) at (4,0) {};
\coordinate[below=of e] (f);
\foreach \point in {e,f}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}

% e is a node and f is an on grid coordinate (on grid has effect)
\node[draw] (e) at (5,0) {};
\coordinate[on grid,below=of e] (f);
\foreach \point in {e,f}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}

% g is a node and h is a node
\node[draw] (g) at (6,0) {};
\node[draw,below=of g] (h) {};
\foreach \point in {g,h}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}

% g is a node and h is an on grid node  (on grid has effect)
\node[draw] (g) at (7,0) {};
\node[draw,on grid,below = of g] (h) {};
\foreach \point in {g,h}
{
  \draw (\point) circle (1pt);
  \node[label=left:\tiny\point] at (\point) {};
}
\end{tikzpicture}

\end{document}

在此处输入图片描述

当然,使用 a\node代替 a\coordinate并不是一回事,可能需要进行一些额外的调整,但您可以使用inner sepouter sep来获得所需的结果:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\node (a) at (0,0) {};
\node[inner sep=0pt] (b) at (0,1) {};
\node[inner sep=0pt,outer sep=-2\pgflinewidth] (c) at (0,2) {};
\draw (-1,0) -- (a) -- (1,0);
\draw (-1,1) -- (b) -- (1,1);
\draw (-1,2) -- (c) -- (1,2);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容