\documentclass[border=2mm, tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
x=7.5mm,y=4.34mm,
box/.style={
regular polygon,
regular polygon sides=6,
minimum size=10mm,
inner sep=0mm, outer sep=0mm,
draw}
]
\node [box] (innerbox) {};
\foreach \x [evaluate=\x as \y using {int(ifthenelse(\x+2>6,\x-4,\x+2))}] in {1,...,6}
\node [box,anchor=corner \y] at (innerbox.corner \x) {};
\end{tikzpicture}
\end{document}
答案1
您的代码无法按原样编译。除非问题在于无法编译,否则请在发布示例之前检查代码是否编译。在这种情况下,请确保它会产生您询问的错误。
这段代码不可能对你有用。它不可能产生没有错误的输出,因为它在各种随机奇怪的地方包含段落中断,这会导致一个又一个错误。
至于这个问题,为什么不能在循环中使用第二个变量?
\documentclass[border=5pt, tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
x=7.5mm,y=4.34mm,
box/.style={
regular polygon,
regular polygon sides=6,
minimum size=10mm,
inner sep=0mm, outer sep=0mm,
draw}
]
\node [box] (innerbox) {g};
\foreach \i/\j [evaluate=\i as \k using {int(ifthenelse(\i+2>6,\i-4,\i+2))}] in {1/a,2/b,3/c,4/d,5/e,6/f}
\node [box,anchor=corner \k] at (innerbox.corner \i) {\j};
\end{tikzpicture}
\end{document}
编辑
您还可以简化操作,以便只需指定节点的名称。例如:
\documentclass[border=5pt, tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
box/.style={
regular polygon,
regular polygon sides=6,
minimum size=10mm,
inner sep=0mm,
outer sep=0mm,
inner color=orange!75!red!50!yellow,
outer color=orange!75!red!75!yellow,
draw=orange,
text=red,
},
]
\node [box] (innerbox) {7};
\foreach \i [count=\k from 0, evaluate=\k as \j using {int(60*\k+30)}, evaluate=\j as \l using {(int(\j+180))} ] in {10,6,4,3,1,9}
\node (h\k) [box, anchor=\l] at (innerbox.\j) {\i};
\end{tikzpicture}
\end{document}
生产以下内容(对于蜜蜂而言):