5

I want to make various copies of a graph in tikz. The graph I want has 10 vertices. But only has the edges between the vertices a_i,a_(i+1),t,b.

I would like to automatically create 8 copies of this graph. and draw them. I just don't know how to code it. Here is my current code, but it adds all of the edges to the same copy of the graph:

\begin{tikzpicture}
[main_node/.style={circle,fill=blue!20,draw,minimum size=2em,inner sep=3pt]}]
\node[main_node] (8) at (0:0.5) {b};
\node[main_node] (9) at (180:0.5) {t};
\foreach \x in {0,1,...,7}
    \node[main_node] (\x) at (360/8*\x:2) {$a_{\x}$};
\foreach \x in {0,1,...,7}{
    \pgfmathsetmacro{\y}{int(mod(\x+1,8))}
    \path[draw,thick] (\x) edge node {} (8);
    \path[draw,thick] (\x) edge node {} (9);
    \path[draw,thick] (\x) edge node {} (\y);
    \path[draw,thick] (8) edge node {} (9);
    \path[draw,thick] (8) edge node {} (\y);
    \path[draw,thick] (9) edge node {} (\y);



}

I want each of the edge sets to be drawn seperately.

5
  • In this case the copies of the graph are just rotations of the same thing, but I need a similar case in which the construction is not soo similiar (but is still arithmetically expressable in terms of an integer x that ranges between 0 and 8). – Jorge Feb 12 '17 at 17:01
  • thank you! Sorry if this question is not up to quality standards, I started learning tikz yesterday, but I couldn't find any resources on this. – Jorge Feb 12 '17 at 17:11
  • You can enclose the whole tikzpicture inside the body of a foreach loop. – JLDiaz Feb 12 '17 at 17:35
  • But how can I do it so that a new graph is created in each cycle? – Jorge Feb 12 '17 at 17:44
  • @JorgeFernándezHidalgo If \begin{picture}/\end{picture} is inside the loop, each iteration of it would produce a new figure. See my anser. – JLDiaz Feb 12 '17 at 17:49
5

Elaborating the method proposed in my comment:

\documentclass[tikz]{standalone}
\usepackage{tikz}

\begin{document}
\foreach \n [remember=\n as \previous (initially 0)] in {1,...,7,0} {
    \begin{tikzpicture}[main_node/.style={circle,fill=blue!20,draw,minimum size=2em,inner sep=3pt]}]
        \node[main_node] (8) at (0:0.5) {b};
        \node[main_node] (9) at (180:0.5) {t};
        \foreach \x in {0,1,...,7}
            \node[main_node] (\x) at (360/8*\x:2) {$a_{\x}$};
        \path[draw,thick] (\n) edge (8);
        \path[draw,thick] (\n) edge (9);
        \path[draw,thick] (\previous) edge (8);
        \path[draw,thick] (\previous) edge (9);
    \path[draw,thick] (\n) edge (\previous);
    \end{tikzpicture}
}
\end{document}

Result:

Result

(Not sure if this is the intended result, but at least it can illustrate the proposed method)

Update: A little improvement to avoid overlapping edges (and to add the missing t--b edge):

\documentclass[tikz]{standalone}
\usepackage{tikz}

\begin{document}
\foreach \n [remember=\n as \previous (initially 0)] in {1,...,7,0} {
    \begin{tikzpicture}[main_node/.style={circle,fill=blue!20,draw,minimum size=2em,inner sep=3pt]}]
        \node[main_node] (8) at (0:0.5) {b};
        \node[main_node] (9) at (180:0.5) {t};
        \foreach \x in {0,1,...,7}
            \node[main_node] (\x) at (360/8*\x:2) {$a_{\x}$};
        \path[draw,thick] (\n) edge[bend left] (8);
        \path[draw,thick] (\n) edge[bend right] (9);
        \path[draw,thick] (8) edge (9);
        \path[draw,thick] (\previous) edge[bend left] (8);
        \path[draw,thick] (\previous) edge[bend right] (9);
    \path[draw,thick] (\n) edge (\previous);
    \end{tikzpicture}
}
\end{document}

Result:

Updated result

Second update

Using a conditional expression, we can change the bending direction of the edges for the cases of the nodes a4..a7, which produces a nicer result.

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

\begin{document}
\foreach \n [remember=\n as \previous (initially 7)] in {0,...,7} {
    \begin{tikzpicture}[main_node/.style={circle,fill=blue!20,draw,minimum size=2em,inner sep=3pt]}]
        \node[main_node] (8) at (0:0.5) {b};
        \node[main_node] (9) at (180:0.5) {t};
        \foreach \x in {0,1,...,7}
            \node[main_node] (\x) at (360/8*\x:2) {$a_{\x}$};
        % Here is the conditional which defines \up and \down
        % appropiately, depending on the iteration of the loop
        \pgfmathsetmacro{\up}{\previous < 4 ? "left" : "right"}
        \pgfmathsetmacro{\down}{\previous < 4 ? "right" : "left"}
        \path[draw,thick] (\n) edge[bend \up] (8);
        \path[draw,thick] (\n) edge[bend \down] (9);
        \path[draw,thick] (8) edge (9);
        \path[draw,thick] (\previous) edge[bend \up] (8);
        \path[draw,thick] (\previous) edge[bend \down] (9);
    \path[draw,thick] (\n) edge (\previous);
    \end{tikzpicture}
}
\end{document}

Result:

Nicer result

3
  • yes, that is exactly what I wanted. Thank you very much! – Jorge Feb 12 '17 at 17:52
  • Any hints as to how I can make my article position the graphs in an ordered fashion? currently it packs them all really close. – Jorge Feb 12 '17 at 18:03
  • @JorgeFernándezHidalgo Not sure of what you mean. You can insert other commands inside the loop, even a figure environment with a caption whose text uses \n variable. But I think the easiest approach is to use an external tex file which uses standalone class, like the one in my answer, to generate a pdf with only figures, and then insert these in your main document via \includegraphics[page=1]{figs.pdf} at your convenience (changing 1 by 2, and so on for the different figures). – JLDiaz Feb 12 '17 at 21:36

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.