11

I want to draw an image like the following:

alt text

I did this using the following Tikz code:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
   [hexagon/.style={regular polygon, regular polygon sides=6,
    draw, minimum size=1cm, anchor=center}]

\foreach \x in {0,...,2}
  \node at (\x, 5) [hexagon]{};
\foreach \x in {0,...,3}
  \node at (\x-0.5, 4.12) [hexagon]{};
\foreach \x in {0,...,4}
  \node at (\x-1, 3.25) [hexagon]{};
\foreach \x in {0,...,3}
  \node at (\x-.5, 2.38) [hexagon]{};
\foreach \x in {0,...,2}
  \node at (\x, 1.52) [hexagon]{};

\end{tikzpicture}
\end{document}

Is there a better way to code this, using fewer manually-calculated approximations in Tikz? Also, as can be seen, the lines overlap in a weird inexact way. Can I avoid that too?

  • 1
    This is very similar to this question, the answers to which you can modify to your liking – Yossi Farjoun Dec 15 '10 at 8:22
18

I realized that this question is slightly different form the drawing hexagons of yore...so, how about this solution:

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepgflibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}
  [hexagon/.style={regular polygon, regular polygon sides=6,
    draw, minimum size=1cm, anchor=center}]
  \foreach[evaluate=\y] \y in {-2,...,2}
  {
  \pgfmathparse{5-abs{ \y }}
\foreach \x in {1,...,\pgfmathresult}
\node at (abs \y /2 +\x, sqrt 3 * \y/2) [hexagon]{};
}
\end{tikzpicture}
\end{document}

which yields: alt text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.