我很难理解 TikZ 如何使用空间
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{red/.style={fill=red,circle,inner sep=0pt}}
\tikzset{green/.style={fill=green,circle,inner sep=0pt}}
\begin{tikzpicture}[x=1cm,y=1cm]
\draw[help lines,step=.2] (-2,-1) grid (-1,0);
\draw[help lines,line width=.5pt,step=1] (-2,-1) grid (2,1);
\foreach \x in {-2,-1}
\node[anchor=north] at (\x,-1) {\x};
\foreach \y in {-1,0}
\node[anchor=east] at (-2,\y) {\y};
\node [rectangle,inner xsep=20mm,inner ysep=10mm,line width=.5pt](fr){};
\node [red,anchor=center] at (fr.south west){};
\node [green,anchor=center] at (-2,-1)(sw){};
\node [green,right=2mm of sw.center](r){};
\end{tikzpicture}
\end{document}
1) 如果你编译上面的代码,你会发现用相对坐标放置的红点和用数字坐标放置的绿点没有完全重叠。这是为什么呢?
2) 标记为 r 的节点没有放置在应在的位置,而是偏右了一点,尽管指定了与 sw.center 的距离。为什么?
答案1
在放置红色节点时,会考虑线宽。对于第二个绿点,会使用节点边框作为参考,您应该on grid
忽略这一点。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\tikzset{red/.style={fill=red,circle,inner sep=0pt}}
\tikzset{green/.style={fill=green,circle,inner sep=0pt}}
\begin{tikzpicture}[x=1cm,y=1cm]
\draw[help lines,step=.2] (-2,-1) grid (-1,0);
\draw[help lines,line width=.5pt,step=1] (-2,-1) grid (2,1);
\foreach \x in {-2,-1}
\node[anchor=north] at (\x,-1) {\x};
\foreach \y in {-1,0}
\node[anchor=east] at (-2,\y) {\y};
\node [rectangle,inner xsep=20mm,inner ysep=10mm,line width=.5pt](fr){};
\node [red,anchor=center] at ($(fr.south west)+(.25pt,.25pt)$) {};
\node [green,anchor=center] at (-2,-1) (sw) {};
\node [green,anchor=center,on grid,right=2mm of sw](r){};
\end{tikzpicture}
\end{document}