我想找出为什么 TikZ 的行为不像我预期的那样。我举了个小例子:
\node[dot_label_se={$y_3$}, below=1.5cm of point_y2] (point_y3) {};
\node[dot_label_se={$z_3$}, below right=1.5cm and 2cm of point_y2] (point_z3) {};
\node[dot_label_se={$v_3$}, below=3cm of point_v1] (point_v3) {};
问题出在点 z3 和 v3 上。首先看一下 z3 的定位,它与 y3 相同,但向右移动了 2cm,那么为什么高度略有不同?
对于 v3,我尝试将其相对于 v1 放置 3 厘米(2 x 1.5 厘米),结果与将 v2 放置在 v1 下方 1.5 厘米处以及将 v3 放置在 v2 下方 1.5 厘米处不同。节点定位不是相对于节点中心吗?
编辑:(完整工作示例-仅要点)
\documentclass[10pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{arevtext, arevmath}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{arrows.meta,calc}
\tikzset{
dot_label_se/.style={circle, inner sep=2pt, fill, label={[text depth=0pt, text height=0.5ex, xshift=-0.07cm]-35:#1}, name=#1},
dot_label_sw/.style={circle, inner sep=2pt, fill, label={[text depth=0pt, text height=0.5ex, xshift=0.14cm]215:#1}, name=#1},
}
\begin{document}
\begin{figure}
\begin{tikzpicture}[font=\sffamily\small\em]
\node[dot_label_se={$y_1$}, left=1cm] (point_y1) {};
\node[dot_label_se={$z_1$}, right=1cm] (point_z1) {};
\node[dot_label_se={$x_1$}, left=2cm of point_y1] (point_x1) {};
\node[dot_label_sw={$v_1$}, right=2cm of point_z1] (point_v1) {};
\node[dot_label_se={$x_2$}, below=1.5cm of point_x1] (point_x2) {};
\node[dot_label_sw={$y_2$}, below=1.5cm of point_y1] (point_y2) {};
\node[dot_label_se={$x_3$}, below=1.5cm of point_x2] (point_x3) {};
\node[dot_label_se={$y_3$}, below=1.5cm of point_y2] (point_y3) {};
\node[dot_label_se={$z_3$}, below right=1.5cm and 2cm of point_y2] (point_z3) {};
\node[dot_label_se={$v_3$}, below=3cm of point_v1] (point_v3) {};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
编辑:正如我在评论中提到的,问题在于在确定节点之间的距离时考虑节点大小。看来最好的解决方案是添加选项on grid
并按原样使用 MWE。
但是,我擅自重写了您的 MWE。看看我的 MWE 是否可以接受:
\documentclass[tikz,
border=3mm,
]{standalone}
\usetikzlibrary{arrows.meta,
decorations,decorations.markings,
positioning
}
\begin{document}
\begin{tikzpicture}[on grid,
node distance = 15mm and 15mm,
dot/.style args = {#1/#2}{circle, fill=black,
inner sep=0pt, outer sep=0pt, minimum size=5pt,
label={[node distance=2pt, inner sep=1pt]#1:{#2}},
node contents={}},
DL/.style = {postaction={decorate},
decoration = {markings,
mark=at position 0.6 with {\arrow{Stealth[length=3mm]}}
}
},
]
\node (n11) [dot=-45/$x_1$];
\node (n12) [dot=-45/$y_1$,right=of n11];
\node (n13) [dot=-45/$z_1$,right=of n12];
\node (n14) [dot=-45/$v_1$,right=of n13];
\foreach \i [count=\j from 1] in {2,3,4}{\draw[DL] (n1\j) -- (n1\i);}
\node (n21) [dot=-45/$x_2$,below=of n11];
\node (n22) [dot=225/$y_2$,right=of n21];
\draw[DL] (n21) -- (n22);
\node (n31) [dot=-45/$x_3$,below=of n21];
\node (n32) [dot=-45/$y_3$,below=of n22];
\node (n33) [dot=-45/$z_3$,below=30mm of n13];
\node (n34) [dot=-45/$v_3$,below=30mm of n14];
\foreach \i [count=\j from 1] in {2,3,4}{\draw[DL] (n3\j) -- (n3\i);}
\draw[DL] (n22) -- (n33);
\end{tikzpicture}
\end{document}
注意:节点定位使用 TikZ 库中确定的语法positioning
。MWE 中使用的语法已弃用。
答案2
您假设节点的行为与点类似,但实际上它们被分配了宽度和高度。因此,当使用相对定位时,您的计算会略有偏差。
尝试将它们对齐到边缘(例如,在左侧),然后从那里使用相对定位。从您的示例重构(这不是平均能量损失,否则可能会有更全面的答案),它可能看起来像这样:
\node[dot_label_se={$x_1$}] (point_x1) {};
\node[dot_label_se={$y_1$}, right=2cm of point_x1] (point_y1) {};
\node[dot_label_se={$z_1$}, right=2cm of point_y1] (point_z1) {};
\node[dot_label_se={$x_2$}, below=1.5cm of point_x1] (point_x2) {};
\node[dot_label_se={$y_2$}, right=2cm of point_x2] (point_y2) {};
\node[dot_label_se={$x_3$}, below=1.5cm of point_x2] (point_x3) {};
\node[dot_label_se={$y_3$}, right=2cm of point_x1] (point_y3) {};
\node[dot_label_se={$z_3$}, right=2cm of point_y3] (point_z3) {};
通过只在一个方向上扩展,您可以最大限度地减少出现问题的可能性。如果您仍然无法对齐它们,您可以添加一个键,将minimum width
所有节点定义为相同且足够高的值。
答案3
为了避免在定位其他节点时考虑节点形状,请考虑添加.center
节点名称和anchor=center
。例如:
\node[dot_label_se={$y_3$}, below=1.5cm of point_y2.center,anchor=center] (point_y3) {};
\node[dot_label_se={$z_3$}, below right=1.5cm and 2cm of point_y2.center,anchor=center] (point_z3) {};
\node[dot_label_se={$v_3$}, below=3cm of point_v1.center,anchor=center] (point_v3) {};
请考虑以下示例:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[bul/.style = {fill,circle,inner sep=3pt}]
\node [bul](1) {};
\node [bul,below=1.5 of 1](2) {};
\node [bul,below=1.5 of 2](3) {};
\node [bul,right=2 of 1](4) {};
\node [bul,below right=1.5 and 2 of 2](5) {};
\draw (4)--(1)--(2)--(3)--(5);
\end{tikzpicture}
\end{document}
现在这个:
\begin{tikzpicture}[bul/.style = {fill,circle,inner sep=3pt}]
\node [bul](1) {};
\node [bul,below=1.5 of 1.center,anchor=center](2) {};
\node [bul,below=1.5 of 2.center,anchor=center](3) {};
\node [bul,right=2 of 1.center,anchor=center](4) {};
\node [bul,below right=1.5 and 2 of 2.center,anchor=center](5) {};
\draw (4)--(1)--(2)--(3)--(5);
\end{tikzpicture}