为了说明我关于 k 间隙平面图的论文,我想找到一种优雅且可参数化的方法来绘制带有套管边的图形(以清楚地表明交点带电于哪条边)。
到目前为止,我完成的工作相当混乱,我希望可以简化。我使用 tikzpackage“intersections”在每个交叉点顶部放置一个白色圆盘,并重复应该通过交叉点而不会出现间隙的边缘。“intersections”允许命名所涉及的路径,但我找不到如何命名让命名的路径再次被绘制,而无需重新定义它们(见下面的代码和图像)。
通过“交点”计算交叉点并通过“pgfonlayer”环境将边缘放在不同层上以控制绘制顺序的组合不起作用。也许这个包与不同层上的边缘不兼容。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\tikzstyle{vertex}=[circle, draw=black, fill=black, radius = 1mm,
scale = 0.8,anchor=center]
\tikzstyle{crossing}=[circle, radius=0.5pt, fill= white]
\tikzstyle{cased}=[draw=white,line width = 12pt,
double=black,very thick, distance = 2pt]
\begin{tikzpicture}[every node/.style={vertex}]
\pgfmathtruncatemacro{\n}{4}
\foreach \pos
[evaluate=\pos as \angle using (360/\n/2)+\pos*360/\n)]
in {2,...,\n}
{%inner vertices
\path node (i\pos) at (\angle:2) {};%
};
\foreach \pos
[evaluate=\pos as \angle using (360/\n/2)+\pos*360/\n)]
in {1,2,...,\n}
{%outer vertices
\node (o\pos) at (\angle:6) {};
};
%bent outer edges
\pgfmathtruncatemacro{\distA}{10}
\path[name path=e_o1_o3] (o1.center) .. controls
+(30:\distA) and +(60:\distA) .. (o3.center);
\draw[name path=e_o2_o4] (o2.center) .. controls
+(120:\distA) and +(150:\distA) .. (o4.center);
%straight edges between inner and outer
%o4.center
\draw[name path=e_o4_i4] (o4.center) -- (i4.center);
%o2.center
\draw[name path=e_o2_i2] (o2.center) -- (i2.center);
%o3.center
\draw[name path=e_o3_i3] (o3.center) -- (i3.center);
%bent edges between inner and outer
%offset=1
\pgfmathtruncatemacro{\distI}{2}
%o1.center
\draw[name path=e_o1_i4] (o1.center) .. controls
+(-20:\distI) and +(140:\distI) .. (i4.center);
\path[name path=e_o1_i2] (o1.center) .. controls
+(-70:\distI) and +(130:\distI) .. (i2.center);
%o4.center
\draw[name path=e_o4_i3] (o4.center) .. controls
+(-110:\distI) and +(50:\distI) .. (i3.center);
%o2.center
\path[name path=e_o2_i3] (o2.center) .. controls
+(20:\distI) and +(-140:\distI) .. (i3.center);
%o3.center
\draw[name path=e_o3_i2] (o3.center) .. controls
+(160:\distI) and +(-40:\distI) .. (i2.center);
\path[name path=e_o3_i4] (o3.center) .. controls
+(110:\distI) and +(-50:\distI) .. (i4.center);
%bent edges between inner and outer
%offset=2 (opposite)
\pgfmathsetmacro{\distG}{5.5}
%o1.center
\path[name path=e_o1_i3] (o1.center) .. controls
+(-10:\distG) and +(70:\distG) .. (i3.center);
%o4.center
\path[name path=e_o4_i2] (o4.center) .. controls
+(-100:\distG) and +(-20:\distG) .. (i2.center);
%o2.center
\draw[name path=e_o2_i4] (o2.center) .. controls
+(80:\distG) and +(160:\distG) .. (i4.center);
\foreach \pos
[remember=\pos as \last (initially \n)]
in {1,2,...,\n}
{%straight outer edges
\draw[name path=e_o\pos_o\last] (o\pos.center) --
(o\last.center);
};
\foreach \pos
[remember=\pos as \last (initially \n)]
in {2,...,\n}
{%straight inner edges
\draw (i\pos.center) -- (i\last.center);
};
%crossings
\begin{scope}[every node/.style={crossing}]
%crossing e_o1_o3 and e_o2_o4
\path [draw,name intersections={of={e_o1_o3 and e_o2_o4}}]
(intersection-1) node (a) {} ;
%repeat edge drawing
\draw[name path=e_o1_o3] (o1.center) .. controls
+(30:\distA) and +(60:\distA) .. (o3.center);
%crossing e_o1_i2 and e_o2_i4
\path [draw,name intersections={of={e_o1_i2 and e_o2_i4}}]
(intersection-1) node (b) {} ;
%repeat edge drawing
\draw[name path=e_o1_i2] (o1.center) .. controls
+(-70:\distI) and +(130:\distI) .. (i2.center);
%crossing e_o1_i3 and e_o4_i4
\path [draw,name intersections={of={e_o1_i3 and e_o4_i4}}]
(intersection-1) node (d) {} ;
%repeat edge drawing
\draw[name path=e_o1_i3] (o1.center) .. controls
+(-10:\distG) and +(70:\distG) .. (i3.center);
%crossing e_o1_i3 and e_o3_i4
\path [draw,name intersections={of={e_o1_i3 and e_o3_i4}}]
(intersection-1) node (c) {} ;
%crossing e_o2_i3 and e_o3_i2
\path [draw,name intersections={of={e_o2_i3 and e_o3_i2}}]
(intersection-1) node (e) {};
%repeat edge drawing
\draw[name path=e_o2_i3] (o2.center) .. controls
+(20:\distI) and +(-140:\distI) .. (i3.center) ;
%crossing e_o2_i3 and e_o4_i2
\path [draw,name intersections={of={e_o2_i3 and e_o4_i2}}]
(intersection-1) node (f) {} ;
%crossing e_o3_i4 and e_o4_i3
\path [draw,name intersections={of={e_o3_i4 and e_o4_i3}}]
(intersection-1) node (i) {} ;
%repeat edge drawing
\draw[name path=e_o3_i4] (o3.center) .. controls
+(110:\distI) and +(-50:\distI) .. (i4.center);
%crossing e_o3_i4 and e_o4_i2
\path [draw,name intersections={of={e_o3_i4 and e_o4_i2}}]
(intersection-1) node (g) {} ;
%crossing e_o3_i3 and e_o4_i2
\path [draw,name intersections={of={e_o3_i3 and e_o4_i2}}]
(intersection-1) node (h) {} ;
%repeat edge drawing
\draw[name path=e_o4_i2] (o4.center) .. controls
+(-100:\distG) and +(-20:\distG) .. (i2.center);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
欢迎!您可以使用这个非常好的knots
库。不用自己绘制所有这些路径、查找交叉点、重新绘制它们,让knots
我们来帮我们做这些。基本上,只需将所有\draw
命令替换为\strand
,就可以了。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{knots,%bbox
}
\begin{document}
\tikzset{vertex/.style={circle, draw=black, fill=black, radius = 1mm,
scale = 0.8,anchor=center}}
\begin{tikzpicture}[every node/.style={vertex},%bezier bounding box
]
\begin{knot}[%draft mode=crossings,
knot gap=9,%<- controls the gap size
flip crossing/.list={1,...,9}]
\pgfmathtruncatemacro{\n}{4}
\foreach \pos
[evaluate=\pos as \angle using (360/\n/2)+\pos*360/\n)]
in {2,...,\n}
{%inner vertices
\path node (i\pos) at (\angle:2) {};%
};
\foreach \pos
[evaluate=\pos as \angle using (360/\n/2)+\pos*360/\n)]
in {1,2,...,\n}
{%outer vertices
\node (o\pos) at (\angle:6) {};
};
%bent outer edges
\pgfmathtruncatemacro{\distA}{10}
\strand (o2.center) .. controls
+(120:\distA) and +(150:\distA) .. (o4.center);
%straight edges between inner and outer
%o4.center
\strand (o4.center) -- (i4.center);
%o2.center
\strand (o2.center) -- (i2.center);
%o3.center
\strand (o3.center) -- (i3.center);
%bent edges between inner and outer
%offset=1
\pgfmathtruncatemacro{\distI}{2}
%o1.center
\strand (o1.center) .. controls +(-20:\distI) and +(140:\distI) .. (i4.center);
%o4.center
\strand (o4.center) .. controls +(-110:\distI) and +(50:\distI) .. (i3.center);
%o2.center
%o3.center
\strand (o3.center) .. controls +(160:\distI) and +(-40:\distI) .. (i2.center);
%bent edges between inner and outer
%offset=2 (opposite)
\pgfmathsetmacro{\distG}{5.5}
%o1.center
%o4.center
%o2.center
\strand (o2.center) .. controls
+(80:\distG) and +(160:\distG) .. (i4.center);
\foreach \pos
[remember=\pos as \last (initially \n)]
in {1,2,...,\n}
{%straight outer edges
\draw (o\pos.center) -- (o\last.center);
};
\foreach \pos
[remember=\pos as \last (initially \n)]
in {2,...,\n}
{%straight inner edges
\draw (i\pos.center) -- (i\last.center);
};
%crossings
\strand (o1) .. controls +(30:\distA) and +(60:\distA) .. (o3);
\strand (o1) .. controls +(-70:\distI) and +(130:\distI) .. (i2);
\strand (o1) .. controls +(-10:\distG) and +(70:\distG) .. (i3);
\strand (o2) .. controls +(20:\distI) and +(-140:\distI) .. (i3) ;
\strand (o3) .. controls +(110:\distI) and +(-50:\distI) .. (i4);
\strand (o4) .. controls +(-100:\distG) and +(-20:\distG) .. (i2);
\end{knot}
\end{tikzpicture}
\end{document}
请注意
- 你可以翻转路口,如图所示。我希望能够重现你的惯例,但如果没有,你随时可以取消翻转路口;
draft mode=crossings,
让您找出哪个路口属于哪个数字;\tikzstyle
已被弃用;- 如果你使用该
bbox
库,你甚至可以自动修复边界框。截至目前,你需要下载这个文件并将tikzlibrarybbox.code.tex
其放在 LaTeX 可以找到的地方,例如与您正在编译的文件相同的目录。如果我理解正确的话,这个库将来可能会成为官方库的一部分。不幸的是,我甚至无法理解最基本的 GitHub 命令和消息,所以我不确定。)