Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to use graph TikZ-library from CVS version (sourceforge or tlcontrib) to draw flowcharts. Something like the one shown in chapter 5 from TikZ manual. In this tutorial, some styles are declared to easily draw loops and corners between nodes. I would like to add some label to this lines but edge label option doesn't work with these kind of edges. It seems to work with straight paths but not with complex ones.

Next example shows the problem:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,graphs,matrix}

\begin{document}
\begin{tikzpicture}[>=stealth',
    font=\sffamily\small,
    every node/.style={align=center},
    skip loop left/.style={to path={-- ++(-#1,0)|- (\tikztotarget)}},
    skip loop right/.style={to path={-- ++(#1,0)|- (\tikztotarget)}},
    hv path/.style ={to path={-| (\tikztotarget)}},
   vh path/.style ={to path={|- (\tikztotarget)}},
]

\matrix[column sep=1cm,row sep=1cm] (mymatrix)  {
\coordinate (A11) {}; & \node[draw] (A12) {A12};\\
\node[draw] (A21) {A21}; & \coordinate (A22) {};\\
};

\graph[use existing nodes] {
 A21->[edge label=A]A12;
 A21->[hv path, edge label=B] A12;  %<- this label is not shown
 A21--[edge label=C]A11->A12; 
};
\end{tikzpicture}
\end{document}

The result shows labels A and C, but not B. Label C is shown because the edge is divided into two straight edges. I can always use something like that, but do you know a better solution?

enter image description here

share|improve this question
    
You should perhaps mention that you're using a CVS version of PGF, and possibly where that can be found, as the graph library isn't yet in the version of PGF that is on CTAN. (Or am I just blind/mistaken?) –  Torbjørn T. Dec 9 '11 at 17:43
    
@TorbjørnT.: Ups! Yes I forgot to mention it. I'll edit my question. –  Ignasi Dec 9 '11 at 18:06
add comment

2 Answers

up vote 2 down vote accepted

Your problem doesn’t have anything to do with graphs or the library. The to paths are missing their nodes. You need to add \tikztonodes after the target coordinate of the path operatore you want to add the nodes to.

hv path/.style ={to path={-| (\tikztotarget) \tikztonodes}},
vh path/.style ={to path={|- (\tikztotarget) \tikztonodes}},

I also switched to the arrows.meta library (stealth becomes Stealth) since the libraries arrows and arrows.spaced are now depecrated (but still available of course).


Though it seem that a new bug has been introduced which can be seen with

A21 -- [edge label=C] A11 -> A12;

as the edge from A21 to A11 gets an arrow tip even if <- is used anywhere on that graph.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta,graphs}
\begin{document}
\begin{tikzpicture}[
  >=Stealth,
  font=\sffamily\small,
  every node/.style={align=center},
  hv path/.style ={to path={-| (\tikztotarget) \tikztonodes}},
  vh path/.style ={to path={|- (\tikztotarget) \tikztonodes}},
]

\matrix[column sep=1cm,row sep=1cm] (mymatrix)  {
  \coordinate (A11);       & \node[draw] (A12) {A12}; \\
  \node[draw] (A21) {A21}; & \coordinate (A22);       \\
};

\graph[use existing nodes] {
 A21 -> [edge label=A]A12;
 A21 -> [hv path, near end, edge label'=B] A12;
 A21 -- [edge label=C] A11 -> A12; 
};
\end{tikzpicture}
\end{document}

Output

enter image description here

share|improve this answer
add comment

If you're only trying to create flow-charts, it would be much easier if you use pure tikz without a library. Here's an example from the tikz example site (slightly modified).

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\begin{document}

  \tikzstyle{decision} = [diamond, draw, fill=gray!20, 
  text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
  \tikzstyle{block} = [rectangle, draw, fill=gray!15, 
  text width=5em, text centered, rounded corners, minimum height=4em]
  \tikzstyle{altblock} = [rectangle, draw, fill=gray!15, 
    text width=5em, text centered, rounded corners=6pt, minimum height=2em]
  \tikzstyle{line} = [draw, -latex']
  \tikzstyle{cloud} = [draw, ellipse,fill=gray!10, node distance=3cm,
  minimum height=2em]
    \begin{tikzpicture}[node distance = 3cm, auto,scale=0.75,transform shape]
    % Place nodes
    \node [altblock] (init) {initialize};
    \node [cloud, left of=init] (expert) {expert};
    \node [cloud, right of=init] (system) {system};
    \node [decision, below of=init] (identify) {Was successful?};
    \node [block, below of=identify] (evaluate) {evaluate candidate models};
    \node [block, left of=evaluate, node distance=3cm] (update) {update model};
    \node [decision, below of=evaluate] (decide) {is best candidate better?};
    \node [block, below of=decide, node distance=3cm] (stop) {stop};
    % Draw edges
    \path [line] (init) -- (identify);
    \path [line] (identify) -- node[auto] {yes} (evaluate);
    \path [line] (identify) -| node[auto,right] {no} (system);
    \path [line] (evaluate) -- (decide);
    \path [line] (decide) -| node [near start] {yes} (update);
    \path [line] (update) |- (identify);
    \path [line] (decide) -- node {no}(stop);
    \path [line,dashed] (expert) -- (init);
    \path [line,dashed] (system) -- (init);
    %\path [line,dashed] (system) |- (evaluate);
    \end{tikzpicture}


\end{document}

Here's the output for this:

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.