9

I want to draw this: enter image description here

I have managed to do one of the Hexagons using this code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=2,cap=round,>=latex]

  \newdimen\R
  \R=0.8cm
\coordinate (center) at (0,0);
\draw (0:\R)
 \foreach \x in {60,120,...,360} {  -- (\x:\R) }
          -- cycle (300:\R) node[below] {}
          -- cycle (240:\R) node[below] {}
          -- cycle (180:\R) node[left] {}
          -- cycle (120:\R) node[above] {}
          -- cycle (60:\R) node[above] {}
          -- cycle (0:\R) node[right] {};
\draw { (60:\R) -- (30:0.69) -- (center) -- (60:\R) } [fill=gray];
\draw { (90:0.69) -- (120:\R) -- (center) -- (90:0.69) } [fill=gray];
\draw { (180:\R) -- (150:0.69) -- (center) -- (180:\R) } [fill=gray];
\draw { (240:\R) -- (210:0.69) -- (center) -- (240:\R) } [fill=gray];
\draw { (300:\R) -- (270:0.69) -- (center) -- (300:\R) } [fill=gray];
\draw { (0:\R) -- (330:0.69) -- (center) -- (0:\R) } [fill=gray];

\end{tikzpicture}
\end{figure}

\end{document}

And it draw this:

enter image description here

But when I try to do 4 of them, it is really complicated to me, please help with that. Thank you

  • the \newdimen\R should be in the preamble not inside an environment (newdimen allocations are always global) but then can you not simply copy the tikzpicture 4 times? – David Carlisle Apr 19 at 18:36
10

Normally the recommendation is to use pics but here you can just use a slightly extended standard node.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc}
\tikzset{repeating shape/.style={regular polygon,regular polygon sides=#1,alias=curr,draw,append after command={
    [draw,] foreach \XX [remember=\XX as \YY (initially #1)] 
    in {1,...,#1} {
    (curr.corner \XX) -- (curr.center) -- ($(curr.corner \YY)!0.5!(curr.corner
    \XX)$)}}}}

\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=2]
  \draw[fill=gray] foreach \X in {0,2.2} { foreach \Y in {0,2.2}
  {(\X,\Y) node[repeating shape=6,minimum size=3cm]{} }};
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

  • 1
    What kind of sorcery is this? Good job. Can you add comments indicating what each section of your code does in order for other readers to have an idea on how replicate or edit the sections. – azetina Apr 19 at 18:45
  • 1
    @azetina No sorcery, only append after command which can be found on p. 150 of the pgfmanual. – marmot Apr 19 at 18:47
4

No one prevents you from pic, with as many options as possible.

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\newdimen\R
\R=0.8cm
\tikzset{
  pics/hexagon/.style args={#1 and #2}{
    code={
      \node[regular polygon,regular polygon sides=6,minimum size=2*\R] (x) {};
      \coordinate (x.corner 0) at (x.corner 6);
      \foreach \i in {0,...,5} {
        \fill[#1] (x.center) -- (x.{\i*60}) -- (x.{\i*60+30});
        \fill[#2] (x.center) -- (x.{\i*60-30}) -- (x.{\i*60});
      }
    }
  }
}
\begin{document}
\begin{tikzpicture}
\pic at (-1,-1) {hexagon=gray and black};
\pic at (-1, 1) {hexagon=gray and black};
\pic at ( 1,-1) {hexagon=gray and black};
\pic at ( 1, 1) {hexagon=gray and black};
\end{tikzpicture}
\end{document}

enter image description here

3

Here is a simple way!

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\def\a{2}
\def\subpic{
\fill[gray] (0:\a)--(60:\a)--(120:\a)
--(180:\a)--(240:\a)--(300:\a)--cycle;
\foreach \i in {0,1,...,5} 
\fill[rotate=60*\i] (0,0)--(120:\a)-|cycle;
}

\foreach \j in {45,135,-135,-45}{
\begin{scope}[shift={(\j:4)}]
\subpic 
\end{scope}
}
\end{tikzpicture}
\end{document}

enter image description here

  • TikZ has special syntaxes for pics based on pgfkeys. There is no need to use scope and define a new command – user156344 Apr 21 at 3:07

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.