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.

Say I have a code to draw a flow chart, I want now a arrow from node 3 to node 1, how can I do that?

1<--|
|   |
2   |
|   |
3----

MWE

\documentclass[class=article,border=0pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

% Define block styles
\tikzset{
    block/.style={rectangle, draw, line width=0.5mm, black, text width=5em, text centered,
                  rounded corners, minimum height=2em},
    line/.style={draw, -latex}
}% <- if you insist in using this in the document add this % here.
\begin{document}
\begin{tikzpicture}[node distance = 1cm, auto]
    % Place nodes
    \node [block] (BLOCK1) {a};
    \node [block, below of=BLOCK1] (BLOCK2) {b};
    \node [block, below of=BLOCK2, node distance=1cm] (BLOCK3) {c};
    % Draw edges
    \path [line] (BLOCK1) -- (BLOCK2);
    \path [line] (BLOCK2) -- (BLOCK3);
\end{tikzpicture}
\end{document}
share|improve this question
    
You could also use Jake's solution to tex.stackexchange.com/questions/55068/… –  Torbjørn T. Mar 14 '13 at 7:40

5 Answers 5

up vote 17 down vote accepted

You can just add a line like:

\path [line] (BLOCK3) --++ (2cm,0cm) |- (BLOCK1);

This path starts at the BLOCK3 node. It then moves (--) to the point which has coordinates (2cm,0cm) relative to this point; hence one of the +s. The second + makes the resulting point the new point of reference. Without it the next part of the path would still be drawn "from" BLOCK3.

The next path description |- draws a right-angle path (vertical first, then horizontal) to BLOCK1. Had we used -| instead it would have been horizontal first, then vertical. Not that you would want that; I'm just including it for clarity.

output

share|improve this answer
    
Someday I'll have to figure out a way to remember this syntax -- not sure why I always have to do trial and error to get it right. :-) –  Peter Grill Mar 14 '13 at 2:02
    
Me too, I am kind of confused about this syntax, tried many times and cant get it right, haha. Thank you Matthew –  Daniel Mar 14 '13 at 2:19
2  
-- ++(2cm,0cm) seems easier to understand than --++ (2cm,0cm). –  Paul Gaborit Mar 14 '13 at 7:29
1  
Ther ei sno accountin gfo rtaste, I guess. :-) –  Matthew Leingang Mar 14 '13 at 17:29
1  
@Nicolás: I would agree it's easier to read than mine, but remembering just comes with a few repetitions. I try to follow DRY when coding TikZ, so if I want to change the name of BLOCK3 I only have to change it in one place. But there is room for both methods, to be sure. –  Matthew Leingang Mar 18 '14 at 16:35

The following answer consists of

Save the .tex files as they are and place them either in your local texmf tree or in the same folder as your main .tex file.

The paths.ortho provides

  • the path operators r-ud, r-rl, r-du and r-lr, they can be used like -- or -| for example;
  • the keys udlr distance as well as for every operator one <operator> distance, which sets the distance between the middle part of the line and the nearest node; and
  • the styles ud, rl, du and lr, they can be used with the path operators to and edge and accept an optional parameter, the distance.

Furthermore, nodes/coordinates can be placed at any position. Similar to the |- and -| operators, the corner points lie at 0.25 and 0.75; this can be set to 1/<n> and (<n>-1)/<n> with udlr/spacing=<n>, i.e. using udlr/spacing=3 sets the corner at 0.3333 and 0.6667. The default is 4.

There’s also the boolean udlr/only middle which sets the corner points at 0 and 1 respectively.

More of this is explained in my answer to Vertical and horizontal lines in pgf-tikz.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning, paths.ortho}
\tikzset{
  block/.style={rectangle,draw,minimum size=1cm},
  line/.style={draw, -latex}
}
\begin{document}
\begin{tikzpicture}[node distance = 1cm]
  % Place nodes
  \node [block]             (a) {a};
  \node [block, below=of a] (b) {b};
  \node [block, right=of a] (c) {c};
  \node [block, below=of c] (d) {d};

  \path[line, very thick] (a) edge[ud] (c)
                          (c) edge[rl] (d)
                          (d) edge[du] (b)
                          (b) edge[lr] (a);

  \path[line, green, thick, udlr/distance=0.25cm] (a) edge[du] (c) 
                                                  (c) edge[lr] (d)
                                                  (d) edge[ud] (b)
                                                  (b) edge[rl] (a);

  \draw[red] (a) r-ud (c) r-rl (d) r-du (b) r-lr (a);
\end{tikzpicture}
\end{document}

Output 1

enter image description here

Examples of the usages of this library …

… can be found in paths.ortho-hvvh.tex and paths.ortho-udlr.tex. Animations created from these examples are hidden behind the following links:

share|improve this answer
    
This is 'fancy' but how has it got anything to do with what the OP asks? :) –  Svend Tveskæg Apr 13 at 0:08
2  
@Svend \path [line] (BLOCK3) r-rl (BLOCK1); It seems I have removed the example of the question in my attempts to clean up my answer. –  Qrrbrbirlbel Apr 13 at 0:13
    
I see! I've never learned TikZ so I didn't knew that there is a easy fix. :) –  Svend Tveskæg Apr 13 at 0:33

One way to do it is to access the east anchor points, applying an xshift and draw form there:

enter image description here

Note:

  • As per Matthew Leingang's answer, there is also the |- syntax, but I always have to either look that up or do trial and error to get it right.

Code:

\documentclass[class=article,border=2pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

% Define block styles
\tikzset{
    block/.style={rectangle, draw, line width=0.5mm, black, text width=5em, text centered,                 rounded corners, minimum height=2em},
    line/.style={draw, -latex}
}% <- if you insist in using this in the document add this % here.
\begin{document}
\begin{tikzpicture}[node distance = 1cm, auto]
    % Place nodes
    \node [block] (BLOCK1) {a};
    \node [block, below of=BLOCK1] (BLOCK2) {b};
    \node [block, below of=BLOCK2, node distance=1cm] (BLOCK3) {c};
    % Draw edges
    \path [line] (BLOCK1) -- (BLOCK2);
    \path [line] (BLOCK2) -- (BLOCK3);

\path [line, red, thick] (BLOCK3.east) --
    ([xshift=0.5cm]BLOCK3.east) --
    ([xshift=0.5cm]BLOCK1.east) --
    (BLOCK1.east);
\end{tikzpicture}
\end{document}
share|improve this answer

A MetaPost solution with the boxes package, combined with the MetaFun format (for the smoothed corners of the bounding boxes).

\documentclass[border=2mm]{standalone}
\usepackage{luamplib}
  \mplibsetformat{metafun}
  \mplibtextextlabel{enable}
\begin{document}
  \begin{mplibcode}
    input boxes;
    numeric len; len := cm;
    beginfig(1); 
      boxjoin(a.s - b.n = (0, .5cm));
      forsuffixes m = a, b, c:
        boxit.m(str m); 
        m.e - m.w = (1.5cm, 0); m.n - m.s = (0, 5mm);
      endfor
      forsuffixes m = a, b, c:
        drawunboxed(m);
        draw (bpath m) cornered 2mm;
      endfor
      drawarrow a.s -- b.n; drawarrow b.s -- c.n;
      drawarrow c.e -- c.e + (len, 0) -- a.e + (len, 0) -- a.e withcolor red;
    endfig; 
  \end{mplibcode}
\end{document}

To be processed with LuaLaTeX.

enter image description here

share|improve this answer

A PSTricks solution:

\documentclass{article}

\usepackage{multido,pstricks}
\usepackage{expl3}

\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

% parameters
\def\boxes{3}
\def\width{3}
\def\height{1}
\def\separation{0.5}
\def\arrow{1}

\begin{document}

\begin{pspicture}(\calc{#1+\arrow},\calc{#3*#2+(#3-1)*#4})
  \multido{\rA = 0+\calc{#2+#4},
           \rB = \calc{#2/2}+\calc{#2+#4},
           \i = #3+-1}{#3}{%
    \psframe[framearc = 0.3](0,\rA)(#1,\calc{\rA+#2})
    \rput(\calc{#1/2},\rB){$\i$}}
  \multido{\r = #2+\calc{#2+#4}}{\calc{#3-1}}{%
    \psline{<-}(\calc{#1/2},\r)(\calc{#1/2},\calc{\r+#4})}
  \psline{->}%
    (#1,\calc{#2/2})%
    (\calc{#1+\arrow},\calc{#2/2})%
    (\calc{#1+\arrow},\calc{(#3-0.5)*#2+(#3-1)*#4})%
    (#1,\calc{(#3-0.5)*#2+(#3-1)*#4})
\end{pspicture}

\end{document}

output

All you have to do is change the values of the parameters and the drawing will be adjusted accordingly.

Note that I've used numbers instead of letters to label the boxes, since that's what the OP asked for (above the MWE).

Update

If you want a macro, here is how it can be done:

\documentclass{article}

\usepackage{multido,pstricks}
\usepackage{expl3}

\ExplSyntaxOn
  \cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff

% horizontal width of ``back to start'' arrow
\def\arrow{1}

% \diagram[<box width>,<box height>](<number of boxes>){<box separation>}
\def\diagram[#1,#2](#3)#4{%
\begin{pspicture}(\calc{#1+\arrow},\calc{#3*#2+(#3-1)*#4})
  \multido{\rA = 0+\calc{#2+#4},
           \rB = \calc{#2/2}+\calc{#2+#4},
           \i = #3+-1}{#3}{%
    \psframe[framearc = 0.3](0,\rA)(#1,\calc{\rA+#2})
    \rput(\calc{#1/2},\rB){$\i$}}
  \multido{\r = #2+\calc{#2+#4}}{\calc{#3-1}}{%
    \psline{<-}(\calc{#1/2},\r)(\calc{#1/2},\calc{\r+#4})}
  \psline{->}%
    (#1,\calc{#2/2})%
    (\calc{#1+\arrow},\calc{#2/2})%
    (\calc{#1+\arrow},\calc{(#3-0.5)*#2+(#3-1)*#4})%
    (#1,\calc{(#3-0.5)*#2+(#3-1)*#4})
\end{pspicture}}

\begin{document}

\diagram[2.2,1](4){0.5} \qquad
\diagram[1.5,2](3){0.3} \qquad
\diagram[2.5,1](5){0.7}

\end{document}

output2

share|improve this answer

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.