I just started doing things with TikZ today and I run into a problem: there is just no example code snippets for typesetting directed, weighted graphs. Can anyone supply one simple example in an answer?
[Tex/LaTex] Typesetting a directed, weighted graph with TikZ
graphstikz-pgf
Related Solutions
At http://latex-community.org/know-how/427-stemma-tikz last Thursday an interesting document was just translated from French to English. It is for manuscript analysis showing relationships between different manuscripts, authors, and texts. It is in the form a tutorial and I think this may be exactly what you want.
A second attempt, this time making some assumptions on the layout.
I assume you want the hyper-edge to always be aligned to the "lonely" node of the triplets (e.g. c
in ({a,b},{c})
and ({c},{a,b})
).
Since you seem to need fine control on the positioning of the nodes I suggest the following: first you place named nodes with manual positioning (using whichever method you prefer, from absolute coordinates to relative positioning); then you specify the hyperedges and the corresponding arcs get created. Here's a macro that can help you with the second step:
\newcommand{\hyperedge}[4][180]{
\draw (#2.#1) ++(#1:.5) edge (#2) edge (#3) edge (#4);
}
It takes 4 arguments (first optional): \hyperedge[angle]{c}{a}{b}
draws the edges assuming that c
is the lonely node and requires you to specify the angle at which the hyperedge should stick out of it.
The definition of the macro works by moving to the right/left border of the lonely node (this is accomplished by (#2.#1)
which moves to the border of the node #2
at angle #1
), then move right/left by .5
which is the coordinate of the "centre" of the edge; then this "centre" is connected with the nodes using the edge
action.
Here's your second example using tikz and my macro:
\documentclass{standalone}
\usepackage{tikz}
\newcommand{\hyperedge}[4][180]{
\draw (#2.#1) ++(#1:.5) edge (#2) edge (#3) edge (#4);
}
\begin{document}
\begin{tikzpicture}[
y=.7cm, x=1cm,
every edge/.append style={thick}
]
\node (a) at (-1,1) {a};
\node (b) at (-1,-1) {b};
\node (c) at (0,0) {c};
\node (d) at (1,1) {d};
\node (e) at (1,-1) {e};
\node (g) at (2,-2) {g};
\hyperedge{c}{b}{a}
\hyperedge[0]{c}{d}{e}
\hyperedge{g}{b}{e}
\end{tikzpicture}
\end{document}
Which is rather readable and easy to extend.
You can elaborate on this by using other means to specify the angle (other derived macros) or enriching it with custom styles.
Another option for the placement of the "centre" of the hyperedge is barycentric coordinates:
\newcommand{\hyperedge}[3]{
\draw (barycentric cs:#1=1,#2=1,#3=1) edge (#1) edge (#2) edge (#3);
}
Note that this does not require the angle
argument.
Best Answer
I made
tkz-graph
andtkz-berge
to help beginners to draw some graphs.tkz-berge
is used for specials graphs (named graphs in graph theory)You can use only
tikz
to draw graphs. Version with tkz-graphWith arrows on edges
update : Version with
tikz
andautomata