2

I am trying to draw a diagram in LaTeX but there is a problem which is I needed to made many lines to go to a specific output (Result), but they do a nested output..

look at the diagram that I have

enter image description here

The code that I was writing is this:

\documentclass[conference]{IEEEtran}
\usepackage{tikz}
\usetikzlibrary{shapes, shadows, arrows}
\ifCLASSINFOpdf
\hyphenation{op-tical net-works semi-conduc-tor}



\begin{document}


\newpage

\tikzstyle{decision} = [diamond, draw, fill=blue!50, text width=8em, text centered, minimum height=15mm, node distance=5em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{elli} = [draw, ellipse, fill=red!50, text width=8em, text centered, minimum height=15mm, node distance=5em]
\tikzstyle{block} = [draw, rectangle, fill=blue!50, text width=10em, text centered, minimum height=10mm, node distance=5em]
\begin{tikzpicture}
\node[block] (start) {1, 2};
\node[decision, below of=start, yshift=-5em](dec1) {...?};
\node[decision, below of=dec1, yshift=-8em](dec2) { 'Yes'?};
\node[decision, right of=dec2, xshift= 8em](dec3) { 'No'?};
\node[elli, below of=dec3, yshift=-5em, yshift=-1em](look) {look};
\node[decision, below of=look, yshift= -5em](dec4) {found?};
\node[elli, right of=dec4, xshift=10em](AddToYes) {Add to 'Yes'};
\node[elli, left of=dec4, xshift=-10em](AddToNo) {Add to 'No'};
\node[block, below of= AddToYes, yshift=-5em](Res) {Result};
%arrows
\path [line] (start) -- (dec1);
\path [line] (dec1) -- (dec2);
\path [line] (dec2) -- (dec3);
\path [line] (dec3) -- (look);
\path [line] (look) -- (dec4);
\path [line] (dec4) -- (AddToNo);
\path [line] (dec4) -- (AddToYes);
\path [line] (AddToYes) -- (Res);
\path [line] (AddToNo) |- (Res);
\path [line] (dec3) |- (Res);
\path [line] (dec2) |- (Res);
\path [line] (dec1) -| node[yshift=0.5em, xshift=-17em]{No} (Res);



\end{tikzpicture}


\end{document}

migrated from stackoverflow.com Oct 25 '15 at 13:20

This question came from our site for professional and enthusiast programmers.

1

I would use something like the chains library for this or a matrix of nodes or both. I'm not entirely sure how the diagram should look, though.

Something like this??

chained flow

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{shapes.geometric, shadows, arrows, chains, scopes, positioning}
\hyphenation{op-tical net-works semi-conduc-tor}
\begin{document}
\tikzset{
  decision/.style = {diamond, draw, fill=blue!50, text width=8em, text centered, minimum height=15mm},
  line/.style = {draw, -latex'},
  elli/.style = {draw, ellipse, fill=red!50, text width=8em, text centered, minimum height=15mm},
  block/.style = {draw, rectangle, fill=blue!50, text width=10em, text centered, minimum height=10mm},
}
\begin{tikzpicture}[start chain=first going below, node distance=5em, every join/.style={line}]
  \node [on chain, block] (start) {1, 2};
  \node [on chain, decision, join] (dec1) {...?};
  {[start branch=helper going left]
    \node [on chain, shape=coordinate] (helper) {};
  }
  \node [on chain, decision, join] (dec2) { 'Yes'?};
  {[start branch=no going right]
    \node [on chain, decision, join] (dec3) { 'No'?};
    {[continue chain=going below]
      \node [on chain, elli, join] (look) {look};
      \node [on chain, decision, join] (dec4)  {found?};
      {[start branch=add to no going right]
        \node [on chain, elli, join] (AddToYes) {Add to 'Yes'};
      }
      \node [on chain, elli, join] (AddToNo) {Add to 'No'};
    }
  }
  \node [on chain, below=of dec2 |- AddToNo, block, join] (res) {Result};
  \draw [line] (AddToNo) |- (res);
  \draw [line] (dec1) -- (helper) |- (res);
  \draw [line] (AddToYes) |- (res);
  \draw [line] (dec1) -| (AddToYes);
\end{tikzpicture}
\end{document}

EDIT

Another possibility is, as I mentioned, to use the matrix library. For example, the following is based on your scanned diagram (so has no elli type nodes but you can easily adapt):

matrix option

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{shapes.geometric, arrows, matrix, quotes}
\begin{document}
\tikzset{
  decision/.style = {diamond, draw, fill=blue!50, text width=8em, text centered, minimum height=15mm},
  line/.style = {draw, -latex'},
  block/.style = {draw, rectangle, fill=blue!50, text width=10em, text centered, minimum height=10mm},
}
\begin{tikzpicture}[every edge quotes/.append style={midway, auto}]
  \matrix [matrix of nodes, column sep=5em, row sep=5em]
  {
    |(start) [block]| 1, 2 & & \\
    |(dec1) [decision]| \dots? & & \\
    |(yes) [decision]| `Yes' & & \\
    |(no) [decision]| `No' & & |(look) [block]| Look\dots \\
    & |(add2no) [block]| Add to `No' & |[decision] (found)| Found? \\
    & & |(add2yes) [block]| Add to `Yes' \\
    |(res) [block]| Result & & \\
  };
  \foreach \i/\j/\k in {start/dec1/,dec1/yes/Yes,yes/no/No,no/look/No,no/res/Yes,found/add2no/No,found/add2yes/Yes,look/found/} \draw [line] (\i) edge ["\k"] (\j);
  \draw [line] (dec1.west) -- +(-5em,0) node [midway, above] {No}  |- (res);
  \draw [line] (add2yes) |- (res);
  \draw [line] (add2no) |- (res);
  \draw [line] (yes.west) -- +(-2.5em,0) node [midway, above] {Yes} |- (res);
\end{tikzpicture}
\end{document}
  • Nice answer, it works, but I amend something. I really would like to vote up, but I need to get more point to be able to do so. Thanks cfr – aamls Oct 25 '15 at 14:59
  • @aamls Please see edit. May a matrix would work better in your case? – cfr Oct 25 '15 at 16:09
  • REALLY GREAT ANSWER .. THANK YOU VERY MUCH. I COULD NOT UPVOTE IT BECAUSE I SHOULD HAVE A BETTER REPUTATION, BUT IT IS EXACTLY WHAT I NEED. – aamls Oct 25 '15 at 16:17
  • @aamls Glad it helps! If you wish (you don't have to), you can accept the answer by clicking on the greyed out tick at the top left of it. This is entirely up to you: you can wait for a better answer, accept this one and then change your mind, or accept no answer at all. But you don't need any reputation to accept answers - just you can only accept one per question, of course. (That is, if you accept one then accept another, you unaccept the first....) – cfr Oct 25 '15 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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