3

I'm new to Tikz and trying to understand (and modify) this 16-node complete graph example from texample.net

% A complete graph
% Author: Quintin Jean-Noël
% <http://moais.imag.fr/membres/jean-noel.quintin/>
\documentclass{article}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage{verbatim} 
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\begin{comment}
:Title: A complete graph
:Tags: Foreach;Graphs;To paths
:Author: Jean-Noël Quintin
:Slug: complete-graph
\end{comment}
\usetikzlibrary[topaths]
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\begin{document}
\begin{tikzpicture}[transform shape]
  %the multiplication with floats is not possible. Thus I split the loop in two.
  \foreach \number in {1,...,8}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 45
        \advance\mycount by 0
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {9,...,16}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 45
        \advance\mycount by 22.5
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {1,...,15}{
        \mycount=\number
        \advance\mycount by 1
  \foreach \numbera in {\the\mycount,...,16}{
    \path (N-\number) edge[->,bend right=3] (N-\numbera)  edge[<-,bend
      left=3] (N-\numbera);
  }
}
\end{tikzpicture}
\end{document}

The code generates the following picture enter image description here

I'm trying to understand how the node and edge positions are computed in the picture, so I can modify it to have less or more nodes. I tried doubling the angles to reduce the number of nodes by half, but that gave me a very ugly picture.

Edit: Added what I've changed. Maybe I did something wrong before, but turns out the picture was not so ugly after all

% A complete graph
% Author: Quintin Jean-Noël
% <http://moais.imag.fr/membres/jean-noel.quintin/>
\documentclass{article}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\begin{comment}
:Title: A complete graph
:Tags: Foreach;Graphs;To paths
:Author: Jean-Noël Quintin
:Slug: complete-graph
\end{comment}
\usetikzlibrary[topaths]
% A counter, since TikZ is not clever enough (yet) to handle
% arbitrary angle systems.
\newcount\mycount
\begin{document}
\begin{tikzpicture}[transform shape]
  %the multiplication with floats is not possible. Thus I split the loop in two.
  \foreach \number in {1,...,4}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 90
        \advance\mycount by 0
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {5,...,8}{
      % Computer angle:
        \mycount=\number
        \advance\mycount by -1
  \multiply\mycount by 90
        \advance\mycount by 45
      \node[draw,circle,inner sep=0.25cm] (N-\number) at (\the\mycount:5.4cm) {};
    }
  \foreach \number in {1,...,7}{
        \mycount=\number
        \advance\mycount by 1
  \foreach \numbera in {\the\mycount,...,8}{
    \path (N-\number) edge[->,bend right=3] (N-\numbera)  edge[<-,bend
      left=3] (N-\numbera);
  }
}
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Please show us what you have changed ... – Mensch Sep 9 '17 at 23:41
  • Welcome! Wouldn't it be easier to start with a somewhat simpler graph if you're new to TikZ? One where you can visually match the edges drawn to the code, for example. – cfr Sep 9 '17 at 23:48
2

Can't say I understood the rule but I guess it's simpler to just draw from scratch

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\def\n{16}% how many nodes
\node[circle,minimum size=10 cm] (b) {};
\foreach\x in{1,...,\n}{
  \node[minimum size=0.75cm,draw,circle] (n-\x) at (b.{360/\n*\x}){\x};
}
\foreach\x in{1,...,\n}{
  \foreach\y in{1,...,\n}{
    \ifnum\x=\y\relax\else
      \draw (n-\x) edge[->,bend right=3] (n-\y);
    \fi
  }
}
\end{tikzpicture}
\end{document}

enter image description here

  • This is much easier to understand than the example I put in. – Asterix Sep 9 '17 at 23:55
  • What does the b stand for in b.{360/\n*\x}? – Asterix Sep 9 '17 at 23:57
  • @Asterix I've put a big circle node to place the smaller ones around it. It is not necessary you can directly place them. I had a similar example by coincidence so worked from it out of laziness. – percusse Sep 10 '17 at 0:02
  • @Asterix You might consider drawing the lines on a background layer so that the circular nodes at the bottom are on top of the lines. – cfr Sep 10 '17 at 1:32

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.