# How can I label TikZ-graphs on four vertices with a loop?

I need to draw all these simple labelled graphs on four vertices. Anyway, can I do this in a quick manner with a loop? And put them in an array? I have already drawn all unlabelled simple graphs on four vertices. Therefore I am hoping that I can just run a loop that labels the edges six different ways for each graph except the complete one and the empty one. Any ideas would be very helpful.

These are the graphs that I have drawn so far. I want to make a loop that permutes the labeling of the edges.

## Code

\documentclass[tikz,convert=false]{standalone}
\begin{document}
\begin{tikzpicture}[scale=.5,auto=left,every node/.style={circle,fill=black!20}]
\node (n1) at (0,0) {1};
\node (n2) at (0,2)  {2};
\node (n3) at (2,2)  {3};
\node (n4) at (2,0) {4};
\foreach \from/\to in {n2/n3}
\draw (\from) -- (\to);
\end{tikzpicture}
\end{document}


## Output

I want to make a loop draw the same picture relabeling the vertices, six times.

• Welcome to TeX.SX. Sorry, but your question is not very clear. We need information about the graphs you want to draw. Four vertices... but what six edges? Please provide the code of the drawings, you have already done and explain further, where they should be labelled. If you need, draw a small sketch to show us. Sep 3, 2013 at 18:30
• Hi, please upload the picture (showing what you want, not your result until now) to dropbox or something and link it here. We will insert it. I put your MWE to your post. It is, however, not compilable. I removed the second documentclass and put a \begin{document}. Please provide something we can copy and compile at once. Sep 3, 2013 at 19:10
• Upload the picture and remove the initial ! then we can add it for you if you don't have enough rep. Sep 3, 2013 at 20:13
• I have no idea where or how to upload this picture? I'm very sorry. Thanks again for the help. Sep 3, 2013 at 20:24
• Another example: texample.net/tikz/examples/complete-graph Sep 4, 2013 at 6:49

You can do that with two loops: one running from 1 to n-1, the other from the current value of the first to n. Here an example for six:

## Code

\documentclass[tikz,convert=false]{standalone}

\begin{document}

\begin{tikzpicture}[scale=.5,auto=left,every node/.style={circle,fill=black!20}]
\foreach \x in {1,...,6}
{   \node (n\x) at (\x*60:3) {\x};
}
\foreach \x in {1,...,5}
{   \pgfmathtruncatemacro{\lowerbound}{\x+1}
\foreach \y in {\lowerbound,...,6}
{   \draw (n\x) -- (n\y);
% for labelled edges
%\draw (n\x) -- (n\y) node[sloped,pos=0.39,fill=white,fill opacity=0.3,above=0.1mm,rectangle, inner sep=0.1mm] {\tiny \x-\y};
}
}

\end{tikzpicture}

\end{document}


## Bonus homework: adapted for 36 nodes

• When I compile your code addapted for 36 nodes the lines cross the nodes above the circles. How to lay the nodes on top of all lines? Sep 4, 2013 at 13:26
• Either use layers (section 82.3, p. 662), or first set the nodes as coordinates, then draw the connections, and finally place the nodes at the coordinates places. Sep 4, 2013 at 21:17

Very similar to Tom Bombadil’s answer, here is an approach with the chains library that takes care of the positioning with the nodes around center style.

## Code

\documentclass[tikz]{standalone}
\usetikzlibrary{chains}
\tikzset{
nodes around center/.style args={#1:#2:#3:#4}{% #1 = start angle, #2 = number of nodes
% #3 = center,      #4 = distance
at={(#3)},shift={({(\tikzchaincount-1)*360/(#2)+#1}:{#4})}}}
\makeatletter
\tikzset{% "edge node" style from CVS
edge node/.code={\expandafter\def\expandafter\tikz@tonodes\expandafter{\tikz@tonodes #1}},
join previous/.code={\ifnum#1=1\relax\else
\tikzset{Join/.list={1,...,\the\numexpr#1-1\relax}}\fi},
Join/.style={join=with \tikz@lib@chain@name-#1
%   by {edge node={node[my edge label/.try] {#1-\cnt}}}
}}
\makeatother
\begin{document}
\begin{tikzpicture}[
start chain=ch placed {nodes around center=60:-12:{0,0}:3},
every on chain/.append style={shape=circle,fill=black!20,outer sep=+0pt,inner sep=+2pt,
text width=\widthof{00}, align=center},
my edge label/.style={font=\scriptsize, auto=right, sloped, pos=.33, inner sep=+0pt,
fill opacity=.5, text opacity=1, fill=white}]
\foreach \cnt in {1,...,12}
\node[on chain=ch, join previous=\cnt] {\cnt};
\end{tikzpicture}
\end{document}