編集

編集
\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

コードはそのままではコンパイルできません。コンパイルできないことが問題でない限り、例を投稿する前にコードがコンパイルされるかどうかを確認してください。その場合は、質問しているエラーが発生することを確認してください。

このコードはおそらく機能しません。さまざまなランダムな奇妙な場所に段落区切りが含まれているため、エラーなしで表示された出力を生成することは不可能であり、エラーが次から次へと発生します。

質問ですが、ループ内で 2 番目の変数を使用できないのはなぜですか?

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

以下を生成します(ミツバチの場合):

ミツバチの巣

関連情報