[Tex/LaTex] Drawing hexagonal lattice in LaTex using Cartesian coordinates

pstrickstikz-pgf

I would like to reproduce the hexagonal lattice using Tikz/PSTricks or a similar package in LaTex as shown in the images below.

There're already kinds of answers available here. However, all of them use for-loops whereas I wanted to create the hexagon's arrangement in a more controlled way, i.e. using the Cartesian x,y coordinates one by one.

Is it possible to create in that way, at least first two rows of the lattice?

enter image description here

I am adding a code for a single hexagon below (this needs to be modified as the lattice points are not placed correctly). Can this structure be repeated ensuring that the top leftmost coordinate is (-1,0) and the sides are of unit length?

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.markings}

\begin{document}

%% Create a hexagon:
\begin{tikzpicture}
 % For a hexagons the coordinates for the vertices in a sequence:
 %  (-1,0), (-1/2,sqrt{3}/2), (1/2,sqrt{3}/2), (1,0), (1/2,-sqrt{3}/2), (-1/2,-sqrt{3}/2)
 % sqrt{3}/2 = 0.866 (approx)   
 \draw [*-, color=red] (-1,0) --  (-0.5,0.866);
 \draw [*-, color=red] (-0.5,0.866) -- (0.5,0.866);
 \draw [*-, color=red] (0.5,0.866) -- (1,0);
 \draw [*-, color=red] (1,0) -- (0.5,-0.866);
 \draw [*-, color=red] (0.5,-0.866) -- (-0.5,-0.866);
 \draw [*-, color=red] (-0.5,-0.866) -- (-1,0);
\end{tikzpicture}

\end{document}

Following your suggestion, I have modified this a bit. I want to display the coordinates of one full hexagon:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.markings}

\begin{document}

\begin{tikzpicture}
  \draw [color=red,mark=*] plot[samples at={-180,-120,...,180},variable=\x] 
  (\x:1);
  \node[color=black, left] at (-1,0) {\small (-1,0)};
  \node[color=black, left] at  (-0.5,0.866) {\small (-0.5,0.866)};
  \node[color=black, left] at  (-0.5,-0.866) {\small (-0.5,-0.866)};
  \node[color=black, right] at (1,0) {\small (1,0)};
  \node[color=black, right] at  (0.5,0.866) {\small (0.5,0.866)};
  \node[color=black, right] at  (0.5,-0.866) {\small (0.5,-0.866)};
\end{tikzpicture}


\end{document}

Output:

enter image description here

Can I do that after constructing the entire lattice?

Best Answer

Let me mention that your code can be simplified to

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw [color=red,mark=*] plot[samples at={-180,-120,...,180},variable=\x] 
  (\x:1);
\end{tikzpicture}
\end{document}

enter image description here

As you can see, this solves the problem of the misaligned circles (which come from the fact that you are adding them via arrow heads; you could fix the problem by shortening the paths, but I feel this is simpler).

The problem is that this contains hard coded distances and so on. Luckily the patterns.meta library has found its way to the manual recently, and this allows us to avoid that problem. As @cfr points out, you only need one of them since they are related by rotation, and patterns created with patterns.meta are rotatable, and you can adjust other parameters which are taken to be the line width and size (and you can dial the color, of course).

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{patterns.meta} 
\begin{document}
\tikzdeclarepattern{name=hexa,
  parameters={
      \pgfkeysvalueof{/pgf/pattern keys/size},
      \pgfkeysvalueof{/pgf/pattern keys/angle},
      \pgfkeysvalueof{/pgf/pattern keys/line width},
  },
  bounding box={(-.1pt,-.1pt) and
    (1.5*\pgfkeysvalueof{/pgf/pattern keys/size}+.1pt,
     {sin(60)*\pgfkeysvalueof{/pgf/pattern keys/size}+.1pt})},
  tile size={(1.5*\pgfkeysvalueof{/pgf/pattern keys/size},
              {sin(60)*\pgfkeysvalueof{/pgf/pattern keys/size}})},
  tile transformation={rotate=\pgfkeysvalueof{/pgf/pattern keys/angle}},
  defaults={
    size/.initial=5pt,
    angle/.initial=0,
    line width/.initial=.4pt,
}, code={
\draw[line width=\pgfkeysvalueof{/pgf/pattern keys/line width}] 
(0,{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2}) 
-- ({\pgfkeysvalueof{/pgf/pattern keys/size}*1/4},0) 
-- ({\pgfkeysvalueof{/pgf/pattern keys/size}*3/4},0)
-- (\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2})
(0.75*\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)})
-- (\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2})
 -- (1.5*\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2})
-- (1.75*\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)})
;
} }
\begin{tikzpicture}
\draw[pattern={hexa[size=10pt,line width=.8pt,angle=90]},
pattern color=blue] (0,0) rectangle ++(2,2); 
\draw[pattern={hexa[size=10pt,line width=.8pt,angle=0]},
pattern color=red] (3,0) rectangle ++(2,2); 
\end{tikzpicture}
\end{document}

enter image description here

Or with circles:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns.meta} 
\begin{document}
\tikzdeclarepattern{name=hexa with circles,
  parameters={
      \pgfkeysvalueof{/pgf/pattern keys/size},
      \pgfkeysvalueof{/pgf/pattern keys/angle},
      \pgfkeysvalueof{/pgf/pattern keys/line width},
      \pgfkeysvalueof{/pgf/pattern keys/radius},
  },
  bounding box={(-.1pt,-.1pt) and
    (1.5*\pgfkeysvalueof{/pgf/pattern keys/size}+.1pt,
     {sin(60)*\pgfkeysvalueof{/pgf/pattern keys/size}+.1pt})},
  tile size={(1.5*\pgfkeysvalueof{/pgf/pattern keys/size},
              {sin(60)*\pgfkeysvalueof{/pgf/pattern keys/size}})},
  tile transformation={rotate=\pgfkeysvalueof{/pgf/pattern keys/angle}},
  defaults={
    size/.initial=5pt,
    angle/.initial=0,
    line width/.initial=.4pt,
    radius/.initial=1.2pt,
}, code={
\draw[line width=\pgfkeysvalueof{/pgf/pattern keys/line width}] 
(0,{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2}) 
-- ({\pgfkeysvalueof{/pgf/pattern keys/size}*1/4},0) 
-- ({\pgfkeysvalueof{/pgf/pattern keys/size}*3/4},0)
-- (\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2})
(0.75*\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)})
-- (\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2})
 -- (1.5*\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2})
-- (1.75*\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)});
\fill 
(0,{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2}) circle[radius=\pgfkeysvalueof{/pgf/pattern keys/radius}]
({\pgfkeysvalueof{/pgf/pattern keys/size}*1/4},0) circle[radius=\pgfkeysvalueof{/pgf/pattern keys/radius}]
({\pgfkeysvalueof{/pgf/pattern keys/size}*3/4},0) circle[radius=\pgfkeysvalueof{/pgf/pattern keys/radius}]
(\pgfkeysvalueof{/pgf/pattern keys/size},{\pgfkeysvalueof{/pgf/pattern keys/size}*sin(60)/2}) circle[radius=\pgfkeysvalueof{/pgf/pattern keys/radius}];
} }
\begin{tikzpicture}
\draw[pattern={hexa with circles[size=10pt,line width=.8pt,angle=90]},
pattern color=blue] (0,0) rectangle ++(2,2); 
\draw[pattern={hexa with circles[size=10pt,line width=.8pt,angle=0]},
pattern color=red] (3,0) rectangle ++(2,2); 
\end{tikzpicture}
\end{document}

enter image description here

ADDENDUM: To make @BlackMild a bit happier here is a shorter version. Of course, one could make it much shorter by using hardcoded values for the distances and so on, but this IMHO really defeats the purpose.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns.meta} 
\begin{document}
\def\pk#1{\pgfkeysvalueof{/pgf/pattern keys/#1}}
\tikzdeclarepattern{name=hexa with circles,
  parameters={
      \pk{size},
      \pk{angle},
      \pk{line width},
      \pk{radius},
  },
  bounding box={(-.1pt,-.1pt) and
    (1.5*\pk{size}+.1pt,
     {sin(60)*\pk{size}+.1pt})},
  tile size={(1.5*\pk{size},
              {sin(60)*\pk{size}})},
  tile transformation={rotate=\pk{angle}},
  defaults={
    size/.initial=5pt,
    angle/.initial=0,
    line width/.initial=.4pt,
    radius/.initial=1.2pt,
}, code={
\draw[line width=\pk{line width}] 
(0,{\pk{size}*sin(60)/2}) -- ({\pk{size}*1/4},0) -- ({\pk{size}*3/4},0) -- (\pk{size},{\pk{size}*sin(60)/2})
(0.75*\pk{size},{\pk{size}*sin(60)})-- (\pk{size},{\pk{size}*sin(60)/2}) -- (1.5*\pk{size},{\pk{size}*sin(60)/2})-- (1.75*\pk{size},{\pk{size}*sin(60)});
\fill (0,{\pk{size}*sin(60)/2}) circle[radius=\pk{radius}] ({\pk{size}*1/4},0) circle[radius=\pk{radius}]
({\pk{size}*3/4},0) circle[radius=\pk{radius}] (\pk{size},{\pk{size}*sin(60)/2}) circle[radius=\pk{radius}];
} }
\begin{tikzpicture}
\draw[pattern={hexa with circles[size=10pt,line width=.8pt,angle=90]},
pattern color=blue] (0,0) rectangle ++(2,2); 
\draw[pattern={hexa with circles[size=10pt,line width=.8pt,angle=0]},
pattern color=red] (3,0) rectangle ++(2,2); 
\end{tikzpicture}
\end{document}

ADDENDUM 2: An attempt to address your (updated) question. Apart from rotating the patterns, you can also subject them to other transformations, in particular shifts.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{patterns.meta} 
\begin{document}
\def\pk#1{\pgfkeysvalueof{/pgf/pattern keys/#1}}
\tikzdeclarepattern{name=hexa with circles,
  parameters={
      \pk{size},
      \pk{angle},
      \pk{line width},
      \pk{radius},
      \pk{xshift},
      \pk{yshift},
  },
  bounding box={(-.1pt,-.1pt) and
    (1.5*\pk{size}+.1pt,
     {sin(60)*\pk{size}+.1pt})},
  tile size={(1.5*\pk{size},
              {sin(60)*\pk{size}})},
  tile transformation={xshift=\pk{xshift},yshift=\pk{yshift},rotate=\pk{angle}},
  defaults={
    size/.initial=5pt,
    angle/.initial=0,
    line width/.initial=.4pt,
    radius/.initial=1.2pt,
    xshift/.initial=0pt,
    yshift/.initial=0pt,
}, code={
\draw[line width=\pk{line width}] 
(0,{\pk{size}*sin(60)/2}) -- ({\pk{size}*1/4},0) -- ({\pk{size}*3/4},0) -- (\pk{size},{\pk{size}*sin(60)/2})
(0.75*\pk{size},{\pk{size}*sin(60)})-- (\pk{size},{\pk{size}*sin(60)/2}) -- (1.5*\pk{size},{\pk{size}*sin(60)/2})-- (1.75*\pk{size},{\pk{size}*sin(60)});
\fill (0,{\pk{size}*sin(60)/2}) circle[radius=\pk{radius}] ({\pk{size}*1/4},0) circle[radius=\pk{radius}]
({\pk{size}*3/4},0) circle[radius=\pk{radius}] (\pk{size},{\pk{size}*sin(60)/2}) circle[radius=\pk{radius}];
} }
\begin{tikzpicture}
\draw[pattern={hexa with circles[size=2cm,line width=.8pt,radius=1.4pt,
xshift={-0.42*sin(60)*0.5cm},
yshift={-1.33*cos(60)*0.5cm}]},
pattern color=red] (-8,-8) rectangle ++(16,16); 
 \draw [color=red,mark=*] plot[samples at={-180,-120,...,180},variable=\x] 
  (\x:1);
  \node[color=black, left] at (-1,0) {\small (-1,0)};
  \node[color=black, left] at  (-0.5,0.866) {\small (-0.5,0.866)};
  \node[color=black, left] at  (-0.5,-0.866) {\small (-0.5,-0.866)};
  \node[color=black, right] at (1,0) {\small (1,0)};
  \node[color=black, right] at  (0.5,0.866) {\small (0.5,0.866)};
  \node[color=black, right] at  (0.5,-0.866) {\small (0.5,-0.866)};
\end{tikzpicture}
\end{document}

enter image description here