3

My goal is to create a complete bipartite graph with assigning numbers to the vertices. My idea was to use \foreach, which worked well for creating the vertices, but I couldn't add the numbers. What should I try?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains}



\begin{document}
\begin{tikzpicture}[thick,
  snode/.style={draw,fill=black,circle,scale=2},
  every node/.style={inner sep=1pt},
]



\begin{scope}[xshift=0cm,yshift=0cm,start chain=going right,node distance=7mm]
\foreach \i in {1,...,5}
  \node[snode,on chain] (g\i) {};
  \node [above=0.1cm of g\i] (y\i) {1}; %Missing \endcsname, Extra \endcsname
\end{scope}

\begin{scope}[xshift=0cm,yshift=-5cm,start chain=going right,node distance=7mm]
\foreach \i in {1,...,5}
  \node[snode,on chain] (h\i) {};
  \node [below = 0.1cm of h\i] (z\i) {2}; %problem with \endcsname
\end{scope}

\foreach \i in {1,...,5}
    \foreach \j in {1,...,5}
        \draw (g\i) -- (h\j);

\end{tikzpicture}
\end{document}
4

You can add a label instead of the second node.

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains}



\begin{document}
\begin{tikzpicture}[thick,
  snode/.style={draw,fill=black,circle,scale=2},
  every node/.style={inner sep=1pt},
]



\begin{scope}[xshift=0cm,yshift=0cm,start chain=going right,node distance=7mm]
\foreach \i in {1,...,5}
  \node[snode,on chain,label=above:1] (g\i) {};
%  \node [above=0.1cm of g\i] (y\i) {1}; %Missing \endcsname, Extra \endcsname
\end{scope}

\begin{scope}[xshift=0cm,yshift=-5cm,start chain=going right,node distance=7mm]
\foreach \i in {1,...,5}
  \node[snode,on chain,label=below:2] (h\i) {};
%  \node [below = 0.1cm of h\i] (z\i) {2}; %problem with \endcsname
\end{scope}

\foreach \i in {1,...,5}
    \foreach \j in {1,...,5}
        \draw (g\i) -- (h\j);

\end{tikzpicture}
\end{document}

You may be interested in the graphdrawing capabilities of TikZ 3. Below is a simple example. It doesn't reproduce the style of your code though, only the structure.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing,graphs}
\usegdlibrary{layered}
\begin{document}
\begin{tikzpicture}
\graph [layered layout,grow=down,sibling distance=2cm,layer distance=4cm]{
{a,b,c,d,e} --[complete bipartite] {f,g,h,i,j}
};
\end{tikzpicture}
\end{document}
  • Thank you, this graphdrawing package is very interesting. – bmv Oct 1 '15 at 12:18

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.