2

Sorry for the poor title. I'm working with tikz on a graph, which I began with a circle, and I have two problems:

  1. The labels on the edges don't seem to be center on the edges. They all seem nearer v3 and v5 than they should.
  2. I can align v1 left of v4, but v6 is not "right" of v2, is below. Why is that? Why can't it be just right of v2, as v4 is left of v6?

My code:

\documentclass[10pt,a4paper,twoside]{report}
%---------------------------------- tikz ---------------------------------------
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc,arrows,patterns,external,shapes.callouts,graphs}
\begin{document}

\begin{tikzpicture} [node distance=3cm,]

    \graph [clockwise=4,radius=1.5cm] {
        v2/$v_2$ ,
        v3/$v_3$,
        v4/$v_4$,
        v5/$v_5$,
    };

    \graph {
        v1/$v_1$ [left of=v4],
        v6/$v_6$ [right of=v2],
    };

    \graph {
        (v5) -> [sloped,edge label=$-4$] (v2) ->[sloped,edge label=$2$] (v3) ->[sloped,edge label=$-5$] (v4) -> [sloped,edge label=$1$] (v5),
    };

\end{tikzpicture}
\end{document}

Image: enter image description here

1 Answers1

3

I would recommend using \graph to establish the nodes, then variants of \path to connect and reference them. As noted by @GonzaloMedina, the alignment issue for the edge labels appears to be related to sloped. As for the second issue, \graph is intended for defining nodes in terms of node chains and chain groups, not relative to existing nodes. I will also point out that right of and left of are deprecated.

Here is a working version using \node and \draw:

\documentclass[10pt,a4paper,twoside]{report}
%---------------------------------- tikz ---------------------------------------
\usepackage{tikz}
\usetikzlibrary{positioning,chains,fit,shapes,calc,arrows,patterns,external,shapes.callouts,graphs}
\begin{document}

\begin{tikzpicture} [node distance=3cm,]
    \graph [clockwise=4,radius=1.5cm] {
        v2/$v_2$,
        v3/$v_3$,
        v4/$v_4$,
        v5/$v_5$,
    };

%    \graph {
%        v1/$v_1$ [left= of v4],
%        v6/$v_6$ [right= of v2]
%    };
    \node[left= of v4] (v1) {$v_1$};
    \node[right= of v2] (v6) {$v_6$};

%   \graph {
%        v5 -> [sloped,$4$] v2 ->[edge label=$2$,sloped,inner sep=0pt,outer sep=0pt,anchor=south,midway] (v3) ->[sloped,edge label=$-5$] (v4) -> [edge label=$1$,sloped,inner sep=0pt,outer sep=0pt,anchor=south,midway] (v5),
%   };
    \draw[->] (v5)--(v2) node[midway,sloped,above]{$-4$};
    \draw[->] (v2)--(v3) node[midway,sloped,above]{$2$};
    \draw[->] (v3)--(v4) node[midway,sloped,below]{$-5$};
    \draw[->] (v4)--(v5) node[midway,sloped,below]{$1$};
\end{tikzpicture}
\end{document}

corrected image

Guho
  • 6,115
  • Why does anchor=north locates labels below and south above? Shouldn't it be the other way around? – Fernando César Oct 14 '15 at 07:00
  • @FernandoCésar: anchor specifies how the label should be situated relative to the coordinate given. In this case, the midway option specifies that the label is to be placed midway along the path between the two points. anchor indicates if the label should be anchored through the top (north), bottom (south), left (west), or right (east) of the label to that point. Note as well that the corners are also available (e.g., north east) and alignment relative to the baseline of the contained text (base). – Guho Oct 14 '15 at 16:37
  • Right. So, for example, the label -4, between v5 -- v2, is above the edge, on top, and should be anchored north, however it is south! – Fernando César Oct 14 '15 at 20:23
  • @FernandoCésar: It is helpful to think of the node placement as a series of steps. Considering your example, the first is selecting a coordinate at which the label will be located (i.e., the midpoint of the path, given by midway). Second, the rotation of the node is set (a product of the sloped option). Finally, the oriented label is placed at the coordinate such that the bottom of the label is located at the coordinate (the south option). – Guho Oct 14 '15 at 21:34
  • @FernandoCésar: Part of the confusion might stem from the orientation of the node resulting from sloped. Internally, sloped sets the rotation of the node to be perpendicular to the path at the indicated location (midway). Additionally, the angle is selected such that the label will never be upside down (see page 238 of the manual). Accordingly, with a label oriented right-side up, for it to appear the line, the anchor will have to be at the bottom. An alternative, which may make more sense, would be to use above in pace of anchor=south and below in place of `anchor=north. – Guho Oct 14 '15 at 21:45
  • 1
    @FernandoCésar: I've edited the answer to reflect this alternative approach. Also, I noticed that the arrows were not between the nodes as you had requested. This was the result of placing all of the nodes in the same \draw command (one arrow was drawn through all of the points). The arrows are now drawn individually. – Guho Oct 14 '15 at 22:03