![Tikz:在图片中设置锚点以进行相对定位](https://linux22.com/image/432345/Tikz%EF%BC%9A%E5%9C%A8%E5%9B%BE%E7%89%87%E4%B8%AD%E8%AE%BE%E7%BD%AE%E9%94%9A%E7%82%B9%E4%BB%A5%E8%BF%9B%E8%A1%8C%E7%9B%B8%E5%AF%B9%E5%AE%9A%E4%BD%8D.png)
我创建了一个pic
简单的多边形,我想使用它并重复使用tikzpictures
它来创建包含相对于彼此定位的副本。我该怎么做?
如果我没有pic
,我会使用positioning
库并相对于多边形的角进行定位。我可以用图片做类似的事情吗?
预期结果比这更优雅、更可重复。谢谢。
\documentclass[class=article,border=5pt,tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{fig/.pic={code={%
\begin{scope}
\node[regular polygon,regular polygon sides=6,draw,minimum width=3cm]
(hexagon){};
\foreach \x [evaluate=\x] in {1,...,6}
{\node[draw,shape=circle,fill=white,opacity=1,minimum size=6mm] at (hexagon.corner \x){}; }
\end{scope}
}}}%
\begin{document}
\begin{tikzpicture}[thick,x=1cm,y=1cm]
\path (0,0) pic {fig};
\path (2.265,1.305) pic {fig}; # inelegant + imprecise!
\path (4.53,0) pic {fig};
\path (2.265,-1.305) pic {fig};
\end{tikzpicture}
\end{document}
答案1
在这种情况下,我不会使用pic
但是append after command
。一个好处是你可以使用节点的锚点。
\documentclass[class=article,border=5pt,tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{myhex/.style={regular polygon,regular polygon sides=6,draw,
outer sep=0pt,minimum width=3cm,
append after command={[/utils/exec=\let\myln\tikzlastnode]
foreach \x in {1,...,6} {(\myln.corner \x)
node[draw,shape=circle,fill=white,opacity=1,minimum size=6mm]{}}
}}}%
\begin{document}
\begin{tikzpicture}[thick,x=1cm,y=1cm]
\path (0,0) node[myhex] (h1) {}
node[myhex,anchor=corner 3] (h2) at (h1.corner 1){}
node[myhex,anchor=corner 3] (h3) at (h1.corner 5){}
node[myhex,anchor=corner 3] (h4) at (h2.corner 5){};
\end{tikzpicture}
\end{document}