2

I have a graph drawn in tikz, in sperate tex file, that I call upon with an input, and a call to \TikzExampleLargeKtn{6}{1}{1} (for instance). I'm not using tkz-graph but I'm open to it.

\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc}

\tikzstyle{vertex}=[circle, draw, fill=black, inner sep=0pt, minimum size=3pt] %style of nodes.
\newcommand{\vertex}{\node[vertex]} %shorthand in place of \node[vertex]

\newcounter{Angle}

\newcommand{\TikzExampleLargeKtn}[3]{
    \begin{tikzpicture}[x=#2 cm, y=#3 cm]
        %creating a complete graph on #1 vertices
        \foreach \i in {1, 2,..., #1} {
            \setcounter{Angle}{ (\i-1) * 360 / #1}
            \vertex (c\i) at (\theAngle:1) [label=\theAngle:]{};
        }
        %drawing the edges, I known every edge is defined twice but okay
        \path 
            \foreach \i in {1, 2,..., #1} {
                \foreach \j in {1, 2,..., #1} {
                    (c\i) edge[color=blue] (c\j)
                }
            };
        %Adding an ellipse shape
        \draw[fill=gray, opacity=0.2] (-2,0) ellipse (3 and 2);
    \end{tikzpicture}
}

The output is a desired :

enter image description here

But now I would like to "attach" this construction at every vertices of another complete graph. The general output should be as follow, but the "gray/blue blob" being at each one of the 6 vertices of the red graph, and at the vertex touching the ellipse. enter image description here

For info, I create the red graphs in a similar fashion, e.g.

\begin{tikzpicture}[x=4 cm, y=4 cm]
    \foreach \i in {1, 2,..., 6} {
        \setcounter{Angle}{ (\i-1) * 360 / 6}
        \vertex (c\i) at (\theAngle:1) [label=\theAngle:]{};
    }

    \path %I known every edge is defined twice but okay
    \foreach \i in {1, 2,..., 6} {
        \foreach \j in {1, 2,..., 6} {
            (c\i) edge[color=red,line width=\widthedge pt] (c\j)}};
    
\end{tikzpicture} 

Is there any simplish way to do it ? I might have to review the way I draw these graphs entirely I guess. I based them from a simple rotation around (0,0) and it's not possible anymore.

3
  • 2
    First, avoid nesting tikzpicture into a tikzpicture, strange/bad behaviours could happen. In this case, I think the proper thing to do is to create a pic that draws your graph, with all the arguments you need (including the coulour), and then to call it where and when needed.
    – SebGlav
    Commented Aug 11, 2021 at 9:27
  • The other alternative is to put the tikzpicture into a savebox and put \usebox into a node. Commented Aug 11, 2021 at 15:57
  • thanks all I had never used pics before. good find! Commented Aug 11, 2021 at 23:03

2 Answers 2

4

I think the same as SebGlav, and I'd go for a pic solution here. You can make a pic for the repeated graph (hex in my example) and reuse it as you need.

For example:

\documentclass[tikz,border=2mm]{standalone}

\tikzset
{%
  pics/hex/.style={% #1 = edge lenght = radius
    code={%
      \foreach\i in {1,...,6}
        \coordinate (-\i) at (60*\i-60:#1);
      \foreach\i in {1,...,6}
      {%
        \foreach\j in {\i,...,6}
          \draw (-\i) -- (-\j);
        \fill[draw=none] (-\i) circle (0.05*#1);
      }
    }},
}

\begin{document}
\begin{tikzpicture}
  \foreach\i in {1,...,6}
  {%
    \pgfmathsetmacro\a{60*\i-60}
    \begin{scope}[shift={(\a:3.5)},rotate=\a]
      \draw[gray,fill=gray!20] (0,0) ellipse (1.5 and 1);
      \pic[draw=blue] at (-1,0) {hex=0.5};
    \end{scope}
  }
  \pic[draw=red,thick] {hex=2};
\end{tikzpicture}
\end{document}

enter image description here

3

Similar to Juan Cataño's solution but using two pics

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{shapes.geometric, backgrounds}

\tikzset{
    pics/hexagon/.style n args={3}{
        code= {
            \node[regular polygon, regular polygon sides=6, minimum size=#2, shape border rotate=-60, anchor=east] (#1) {};
            \foreach \i in {1,...,6} {
                \foreach  \j in {\i,...,6}{
                    \draw[#3] (#1.corner \i)--(#1.corner \j);}
                \fill (#1.corner \i) circle (.5pt);
            }
        }
    },
    pics/cloud/.style n args={3}{
        code= {
            \node[regular polygon, regular polygon sides=6, minimum size=#2, shape border rotate=-60, anchor=east] (#1) {};
            \foreach \i in {1,...,6} {
                \foreach  \j in {\i,...,6}{
                    \draw[#3] (#1.corner \i)--(#1.corner \j);}
                \fill (#1.corner \i) circle (.5pt);
            }
        \begin{scope}[on background layer]
            \node[ellipse, fill=gray, opacity=.2, anchor=east, minimum size=1.5cm, minimum height=1cm] at (#1.corner 1) {};
        \end{scope}
        }
    },
}

\begin{document}
\begin{tikzpicture}
\pic (central) {hexagon={c}{2cm}{red}};
\pic[scale=-1, transform shape] (right) at (centralc.corner 1) {cloud={r}{.5cm}{blue}};
\pic[scale=-1, rotate=60, transform shape] (topright) at (centralc.corner 2) {cloud={tr}{.5cm}{blue}};
\pic[rotate=-60,transform shape] (topleft) at (centralc.corner 3) {cloud={tl}{.5cm}{blue}};
\pic (left) at (centralc.corner 4) {cloud={l}{.5cm}{blue}};
\pic[rotate=60, transform shape] (bottomleft) at (centralc.corner 5) {cloud={bl}{.5cm}{blue}};
\pic[scale=-1, rotate=-60, transform shape] (bottomright) at (centralc.corner 6) {cloud={br}{.5cm}{blue}};
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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