Seite 1 von 1
Graph mit tikz: edge-Label
Verfasst: Fr 20. Jan 2017, 09:19
von Kappa
Hallo zusammen,
ich möchte mit tikz einen Graphen darstellen. Die Knoten und Kanten habe ich bereits, nun möchte ich an jede Kante ein label setzen. Das Label soll für jede Kante
mittig und einen kleinen Abstand
rechtwinklig zur Kante haben. Also für jede Kante soll vom Kantenmittelpunkt rechtwinklig in eine Distanz gegangen werden und dort das Label positioniert werden (in welche der beiden Richtungen möchte ich für jede Kante selber angeben). Bisher hab ich folgendes:
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture} [scale=0.75]
\begin{scope}[every node/.style={circle,draw,fill=white}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\begin{scope}[>=latex,
every node/.style={midway},
every edge/.style={draw=black,thick}]
\path [-] (5) edge[right] node {\footnotesize $1$} (6);
\path [-] (6) edge[right] node {\footnotesize $1$} (7);
\path [-] (6) edge[right] node {\footnotesize $1$} (8);
\end{scope}
\end{tikzpicture}
\end{document}
Kann mir einer helfen? Danke schonmal :)
Verfasst: Fr 20. Jan 2017, 13:35
von esdd
Ich bin mir nicht sicher, ob ich verstanden habe, wie die Label positioniert sein sollen. Eventuell suchst Du slopend. Dann kannst Du für jeden Node angeben, ob er oberhalb oder unterhalb der Verbindung stehen soll oder für alle eine Position vorgeben und nur im Einzelfall ändern:
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture} [scale=0.75]
\begin{scope}[nodes={circle,draw,fill=white}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\begin{scope}[>=latex,
nodes={font=\footnotesize,sloped,above},
every edge/.style={draw=black,thick}]
\path [-] (5) edge node[below]{ $1$} (6);
\path [-] (6) edge node { $1$} (7);
\path [-] (6) edge node { $1$} (8);
\end{scope}
\end{tikzpicture}
\end{document}
Verfasst: Fr 20. Jan 2017, 14:08
von Kappa
Hallo,
die Positionen sind genau richtig, nur ich möchte die Zahlen trotzdem nicht "sloped" haben, sondern "normal" ausgerichtet. Also quasi wie dein Beispiel, nur die Zahlen an sich noch rotiert, sodass sie grade sind.
Danke
Verfasst: Fr 20. Jan 2017, 14:18
von esdd
Also eventuell etwas wie auto=right. Mit swap kann man die Richtung für einen einzelnen Node umkehren.
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture} [scale=0.75]
\begin{scope}[nodes={circle,draw,fill=white}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\begin{scope}[>=latex,
nodes={font=\footnotesize,circle,inner sep=2pt},
auto=right,
every edge/.style={draw=black,thick}]
\path [-] (5) edge node { $1$} (6);
\path [-] (6) edge node { $1$} (7);
\path [-] (6) edge node[swap] { $1$} (8);
\end{scope}
\end{tikzpicture}
\end{document}
Verfasst: Sa 21. Jan 2017, 17:20
von Kappa
Super, das sieht viel besser aus als meine bisherigen Versuche.
Falls ein Label mal vereinzelt nicht exakt positioniert ist, ändere ich es noch mit node[pos=...]. Danke für die super Antwort
Verfasst: So 22. Jan 2017, 13:42
von Bartman
Diese Abbildung könnte auch eine Gelegenheit für den Einsatz der TikZ-Bibliothek graphs sein:
\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{
graphs,% für die zweite und dritte Zeichnung
quotes% nur für die dritte Zeichnung
}
\begin{document}
\begin{tikzpicture}[scale=0.75]
\begin{scope}[nodes={circle,draw}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\path [
nodes={font=\footnotesize,inner sep=2pt},
auto=right,
every edge/.style={draw,thick}
]
(5) edge node {1} (6)
(6) edge node {1} (7)
(6) edge node[swap] {1} (8);
\end{tikzpicture}
%
% Graph Variante 1
%
\begin{tikzpicture}[scale=0.75]
\begin{scope}[nodes={circle,draw}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\begin{scope}[
nodes={font=\footnotesize,inner sep=2pt},
every edge/.style={draw,thick}
]
\graph [use existing nodes, edge label'=1] {
5 -- 6 -- 7
};
\graph [use existing nodes, edge label=1] {
6 -- 8
};
\end{scope}
\end{tikzpicture}
%
% Graph Variante 2
%
\begin{tikzpicture}[scale=0.75]
\begin{scope}[nodes={circle,draw}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\begin{scope}[
nodes={font=\footnotesize,inner sep=2pt},
every edge/.style={draw,thick}
]
\graph[use existing nodes] {
5 -- ["1"'] 6 -- {
7 [> "1"'],
8 [> "1"]
}
};
\end{scope}
\end{tikzpicture}
\end{document}