Hy,
I would like to know if it is possible in Tikz graphs put the weight above the vertice (this way, all edges to the destination node has the same weight) instead above the edge. Here is an example:
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityHy,
I would like to know if it is possible in Tikz graphs put the weight above the vertice (this way, all edges to the destination node has the same weight) instead above the edge. Here is an example:
A simple solution is to place the weight at the north of your node, using the above style option :
\documentclass[crop]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}
\begin{document}
\tikzset{mynode/.style={ellipse,minimum height=20pt,minimum width=30pt,draw},}
\begin{tikzpicture}
\draw
(-1,-2) node[mynode] (ini) {ini}
(0,0) node[mynode] (0) {0}
(3,0) node[mynode] (1) {1}
(0.north) node[above] {4}
(1.north) node[above] {6} ;
\draw[-latex]
(ini)->(0) ;
\draw[-latex]
(0)->(1) ;
\end{tikzpicture}
\end{document}
which gives the result:
There's a simple way to tag on a node relative to the position of another node. To do that for the top left corner of your graph, take a look at the following code:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes}
\tikzset{mynode/.style={ellipse,minimum height=20pt,minimum width=30pt,draw},}
\tikzstyle{every picture}=[->,>=latex]
\begin{document}
\begin{tikzpicture}
\node[mynode] (0) at (0,0) {0};
\node[mynode] (1) at (3,0) {1};
\node[mynode] (ini) at (-1,-2) {ini};
\node (0top) at ($(0) + (90:.6)$) {4};
\node (1top) at ($(1) + (90:.6)$) {6};
\draw (ini) to (0);
\draw (0) to (1);
\end{tikzpicture}
\end{document}
The result will be this:
I've used .6
as the distance between the main node and the node holding the number above it, but you can adjust that as desired. Also, the degree for placement I've used is 90
, but that can be changed if you want the number to appear below the node - just use -90
.
Here is a bit more automated way to do so (in order to avoid the deferred definition of labels). The bases are taken from How to modify nodes in TikZ to automatically add a line on their top? and the beauty of this approach is that one can decide to position the label or not immediately when the node is defined. Also some customization of the aspect are possible.
The example (structure stolen from JimboBimbo):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\pgfmathtruncatemacro\labelabove{4}% <= change this value to modify the weight distance from the node
\tikzset{my node/.style 2 args={
ellipse,minimum height=20pt,minimum width=30pt,draw,
append after command={
\pgfextra{%
\begin{pgfinterruptpath}% just to be safe
\node[#2,above= \labelabove pt of \tikzlastnode] {#1};
\end{pgfinterruptpath}%
}
}
}
}
\begin{document}
\begin{tikzpicture}
\node[my node={4}{red,circle,draw}] (0) at (0,0) {0};
\node[my node=6] (1) at (3,0) {1};
\node[my node] (ini) at (-1,-2) {ini};
\draw[-stealth] (ini) to (0);
\draw[-stealth] (0) to (1);
\end{tikzpicture}
\end{document}
The result:
The first argument of my node
accepts as label the weight and it could be omitted (as per ini
); for particular needs, it is possible to customize the aspect of the weight by means of the second argument of my node
, that is optional.
Notice that the previous approach is not the only possible. In a simpler manner:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,positioning}
\tikzset{my node/.style={
ellipse,minimum height=20pt,minimum width=30pt,draw,
}
}
\begin{document}
\begin{tikzpicture}
\node[my node, label={[label distance=5pt,red,circle,draw]90:4}] (0) at (0,0) {0};
\node[my node,label={[label distance=5pt]90:6}] (1) at (3,0) {1};
\node[my node] (ini) at (-1,-2) {ini};
\draw[-stealth] (ini) to (0);
\draw[-stealth] (0) to (1);
\end{tikzpicture}
\end{document}
will lead to the same picture reported above.