7

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:

enter image description here

1
  • 3
    Hi and welcome to TeX.sx. In its current form, your question might not receive many answers. Please take a look at the How to Ask-page and try to improve your question according to the guidance found there. This may require you to show some effort on your part in terms of attempting a solution. If you search for graphs on this site you can find many similar questions.
    – percusse
    Oct 14, 2012 at 22:37

3 Answers 3

5

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: a graph with weights above the nodes

7

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:

enter image description here

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.

3

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:

enter image description here

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .