11

The problem of drawing hexagonal grids has been discussed here: Drawing Hexagons

I want the shape of the grid to be hexagonal and furthermore, I want to be able to add numbers in the hexagonal cells. Any idea?

3
  • I am basically trying to create a honey sudoku puzzle.
    – Carl
    Jun 27, 2012 at 15:15
  • 2
    Please add which drawing package you are using, how far you got (your current code) and where you are stuck.
    – Caramdir
    Jun 27, 2012 at 15:19
  • I have not implemented any code. I am a very beginner in latex. I would like the shape of the board to be hexagonal, like in this picture: img73.imageshack.us/img73/14/minesperfectportablenj3.png
    – Carl
    Jun 27, 2012 at 16:19

2 Answers 2

15

I'm not sure I understood correctly the question. I forgot to add name at each cell.

Update

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\begin{tikzpicture} [hexa/.style= {shape=regular polygon,regular polygon sides=6,minimum size=1cm, draw,inner sep=0,anchor=south,fill=lightgray!85!blue,rotate=30}]
\foreach \j in {0,...,5}{%
\pgfmathsetmacro\end{5+\j} 
  \foreach \i in {0,...,\end}{%
  \node[hexa] (h\i;\j) at ({(\i-\j/2)*sin(60)},{\j*0.75}) {};}  }      
\foreach \j in {0,...,4}{%
  \pgfmathsetmacro\end{9-\j} 
  \foreach \i in {0,...,\end}{%
  \pgfmathtruncatemacro\k{\j+6}  
  \node[hexa] (h\i;\k) at ({(\i+\j/2-2)*sin(60)},{4.5+\j*0.75}) {};}  } 

  \foreach \k in {0,...,10}  {\node [circle,red,minimum size=1cm] at (h3;\k) {3;\k};} 
   \foreach \k in {0,...,10}  {\node [circle,blue,minimum size=1cm] at (h1;\k) {1;\k};}   
\end{tikzpicture}
\end{document}   

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}
\begin{tikzpicture} [hexa/.style= {shape=regular polygon,
                                   regular polygon sides=6,
                                   minimum size=1cm, draw,
                                   inner sep=0,anchor=south,
                                   fill=lightgray!85!blue}]

\foreach \j in {0,...,9}{% 
     \ifodd\j 
         \foreach \i in {0,...,9}{\node[hexa] (h\j;\i) at ({\j/2+\j/4},{(\i+1/2)*sin(60)}) {\j;\i};}        
    \else
         \foreach \i in {0,...,9}{\node[hexa] (h\j;\i) at ({\j/2+\j/4},{\i*sin(60)}) {\j;\i};}
    \fi}
\node [circle,draw,red,minimum size=1cm] at (h3;4){};
\end{tikzpicture}
\end{document}      

enter image description here

5
  • Maybe better to state that this requires the CVS version.
    – percusse
    Jun 27, 2012 at 15:52
  • @percusse Why ? I used \ifodd and not \isodd ! Jun 27, 2012 at 15:56
  • Ah sorry. My bad. You are right.
    – percusse
    Jun 27, 2012 at 16:09
  • Great! thanx alot!
    – Carl
    Jun 27, 2012 at 17:20
  • I would like to change the numbering of hexagons, how do I do that?
    – benedito
    Nov 11, 2015 at 21:52
4

Not very pretty, but with the code in this answer, numbers can be added if you calculate the coordinates:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[x=1cm,y=-1cm]
  \foreach \i in {0,...,3} 
  \foreach \j in {0,...,3} {
  \foreach \a in {0,120,-120} \draw (3*\i,2*sin{60}*\j) -- +(\a:1);
  \foreach \a in {0,120,-120} \draw (3*\i+3*cos{60},2*sin{60}*\j+sin{60}) -- +(\a:1);}

  \foreach \i in {0,...,3}
  \foreach \j in {0,...,3} {
  \node at (3*\i+2,2*sin{60}*\j) {\small $\i^+,\j$};
  \node at (3*\i+0.5,2*sin{60}*\j+sin{60}) {\small $\i,\j$};}
\end{tikzpicture}

\end{document}

enter image description here

So, if you just want to number some of the hexagons, that may be good enough.

4
  • Thanks that is very helpful. However, I am getting the following error when I run the code:"Package PGF Math Error: Unknown function `sin60'"
    – Carl
    Jun 27, 2012 at 16:35
  • @Carl Probably a version mismatch, I guess. It worked for me with tikz version 1.76 (2010/10/13)
    – Jellby
    Jun 27, 2012 at 16:47
  • @Jellby How would you reduce the size of the hexagons?
    – V-Red
    Sep 29, 2017 at 21:17
  • @V-Red See the [x=1cm,y=-1cm]? Use smaller dimensions (I guess, I haven't tried).
    – Jellby
    Oct 2, 2017 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .