編輯

編輯
\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}

產生以下結果(對於蜜蜂):

蜜蜂的蜂窩

相關內容