I want to draw graphs with white and black vertices, such as square-octagon graph. Someone knows the easy way to draw in Latex? Thanks a lot!
[Tex/LaTex] Drawing bipartite graph
graphs
Related Solutions
I'm the author of this package. The good news are the new versions of tkz-graph and tkz-berge will be on the ctan servers this week (i'm working on the docs actually), the bad news are that I modified some macros. I'm not sure to understand exactly what you want but an answer is that you can put TikZ's code inside you code.
Vertices are nodes so you can add multiple labels if you want with the option label=
, if you want adding a text you can use the option text width
something like :
\SO(P){X1}
\node[text width=2cm,left=1cm] (X1){the protocol starts here};
Another possibility :
\begin{tikzpicture}
{ \SetVertexNoLabel
\Vertex[style={label=60:A}]{A}}
\SO[Lpos=180,
LabelOut,
L= {the protocol starts here},
style={text width=2cm}](A){B}
\end{tikzpicture}
But there are other ways to do this. You can use label
. In your code you used "position" , it's not very fine because it's a problem to use "scale".
In 12 hours, I will upload the new version of tkz-graph. I the new version node distance
is obsolete, now I added an option unit
and a macro \tkzGraphUnit=6
instead of `\tikzset{node distance=6cm}.
One option would be to use PGF/TikZ; the package is very well documented, and you'll find many examples in the documentation. Another source of examples can be found at TeXample.net
Here's a little example:
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc}
\begin{document}
\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}
\begin{tikzpicture}[thick,
every node/.style={draw,circle},
fsnode/.style={fill=myblue},
ssnode/.style={fill=mygreen},
every fit/.style={ellipse,draw,inner sep=-2pt,text width=2cm},
->,shorten >= 3pt,shorten <= 3pt
]
% the vertices of U
\begin{scope}[start chain=going below,node distance=7mm]
\foreach \i in {1,2,...,5}
\node[fsnode,on chain] (f\i) [label=left: \i] {};
\end{scope}
% the vertices of V
\begin{scope}[xshift=4cm,yshift=-0.5cm,start chain=going below,node distance=7mm]
\foreach \i in {6,7,...,9}
\node[ssnode,on chain] (s\i) [label=right: \i] {};
\end{scope}
% the set U
\node [myblue,fit=(f1) (f5),label=above:$U$] {};
% the set V
\node [mygreen,fit=(s6) (s9),label=above:$V$] {};
% the edges
\draw (f1) -- (s6);
\draw (s6) -- (f2);
\draw (f2) -- (s7);
\draw (s7) -- (f3);
\draw (s8) -- (f3);
\draw (f3) -- (s9);
\draw (s9) -- (f5);
\draw (f5) -- (s6);
\end{tikzpicture}
\end{document}
EDIT: I add a variation of the previous graph (requested in a comment); each vertex will now be a labelled ordered pair. The idea is to use multiple variables in each \foreach
construct; the first variable will be used to add a name to each node and to create the labels; the second and third variables will give the first and second coordinates of each ordered pair. The code:
\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc}
\begin{document}
\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}
\begin{tikzpicture}[thick,
fsnode/.style={},
ssnode/.style={},
every fit/.style={ellipse,draw,inner sep=5pt,text width=2cm},
->,shorten >= 3pt,shorten <= 3pt
]
% the vertices of U
\begin{scope}[start chain=going below,node distance=7mm]
\foreach \i/\xcoord/\ycoord in {1/6/8,2/5/1,3/-4/7,4/6/9,5/0/-3}
\node[fsnode,on chain,label=left:$t_{\i}$] (f\i) {$(\xcoord,\ycoord)$};
\end{scope}
% the vertices of V
\begin{scope}[xshift=4cm,yshift=-0.5cm,start chain=going below,node distance=7mm]
\foreach \i/\xcoord/\ycoord in {6/0/3,7/1/4,8/-2/1,9/5/9}
\node[ssnode,on chain,label=right:$t_{\i}$] (s\i) {$(\xcoord,\ycoord)$};
\end{scope}
% the set U
\node [myblue,fit=(f1) (f5),label=above:$U$] {};
% the set V
\node [mygreen,fit=(s6) (s9),label=above:$V$] {};
% the edges
\draw (f1) -- (s6);
\draw (s6) -- (f2);
\draw (f2) -- (s7);
\draw (s7) -- (f3);
\draw (s8) -- (f3);
\draw (f3) -- (s9);
\draw (s9) -- (f5);
\draw (f5) -- (s6);
\end{tikzpicture}
\end{document}
Best Answer
The answer depends in part on how much time and effort you're willing to put into it. Definitely start by looking at tikz along with the
tkz-graph
package here and thetkz-berge
package here. This site and this page are a good place to start looking at for examples which you like best. Make sure you read Altermundus' "Gallery of Named Graphs"; you can download it here. Note that the computer algebra system Sage supports tikz and Altermundus' packages as well as LaTeX. The relevant page is here. Robert Beezer has used Sage combined with LaTeX to produce graphs; see here for example.