5

I'm working on some flowcharts for a programming course and have an alignment issue with one of them. Usually lines in a flowchart are directed from one symbol to another. However, I want the line to intersect with another line.

My existing TeX file:

\documentclass{article}

\usepackage[margin=1.0in]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}

\begin{document}
\pagestyle{empty}

\tikzstyle{decision}=[diamond, draw, fill=yellow!20,
  text width=7em, text badly centered, node distance=3cm,
  inner sep=0pt]
\tikzstyle{block}=[rectangle, draw, fill=blue!20, 
  text width=10em, text badly centered, rounded corners,
  minimum height=4em]
\tikzstyle{line}=[draw, very thick, color=black!75, -latex']

\begin{center}
\begin{tikzpicture}[node distance=2cm, auto]
  % Place nodes
  \node [block] (read_first_integer) {Read first integer};
  \node [block, below of=read_first_integer] (read_second_integer)
        {Read second integer};
  \node [block, below of=read_second_integer] (read_third_integer)
        {Read third integer};
  \node [block, below of=read_third_integer] (calculate)
        {Calculate sum, product, difference};
  \node [decision, below of=calculate] (q_first_integer_smallest)
        {First integer less than second and third?};
  \node [block, on grid=false, below of=q_first_integer_smallest,
         node distance=3cm] (first_integer_smallest)
        {First integer is smallest};
  \node [decision, left of=q_first_integer_smallest,
         node distance=4.5cm] (q_second_integer_smallest)
        {Second integer less than second and third?};
  \node [block, below of=q_second_integer_smallest,
         node distance=3cm]
        (second_integer_smallest) {Second integer is smallest};
  \node [block, left of=q_second_integer_smallest,
         node distance=4.5cm] (third_integer_smallest)
        {Third integer is smallest};
  \node [decision, below of=first_integer_smallest,
         node distance=3.5cm] (q_first_integer_largest)
        {First integer larger than second and third?};

  % Draw edges
  \path [line] (read_first_integer) -- (read_second_integer);
  \path [line] (read_second_integer) -- (read_third_integer);
  \path [line] (read_third_integer) -- (calculate);
  \path [line] (calculate) -- (q_first_integer_smallest);
  \path [line] (q_first_integer_smallest) -- node [near start]
               {no} (q_second_integer_smallest);
  \path [line] (q_first_integer_smallest) -- node [near start]
               {yes} (first_integer_smallest);
  \path [line] (q_second_integer_smallest) -- node [near start]
               {no} (third_integer_smallest);
  \path [line] (q_second_integer_smallest) -- node [near start]
               {yes} (second_integer_smallest);
  \path [line] (first_integer_smallest) -- (q_first_integer_largest);
  \path [line] (second_integer_smallest) |- (q_first_integer_largest);
  \path [line] (third_integer_smallest) |- (q_first_integer_largest);
\end{tikzpicture}
\end{center}

\end{document}

The result:enter image description here

I've done the following as a solution and am wondering if there is a simpler way:

\documentclass{article}

\usepackage[margin=0.5in]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}

\begin{document}
\pagestyle{empty}

\tikzstyle{decision}=[diamond, draw, fill=yellow!20,
  text width=7em, text badly centered, node distance=3cm,
  inner sep=0pt]
\tikzstyle{block}=[rectangle, draw, fill=blue!20, 
  text width=10em, text badly centered, rounded corners,
  minimum height=4em]
\tikzstyle{line}=[draw, very thick, color=black!75, -latex']
\tikzstyle{empty}=[]

\begin{center}
\begin{tikzpicture}[node distance=2cm, auto]
  % Place nodes
  \node [block] (read_first_integer) {Read first integer};
  \node [block, below of=read_first_integer] (read_second_integer)
        {Read second integer};
  \node [block, below of=read_second_integer] (read_third_integer)
        {Read third integer};
  \node [block, below of=read_third_integer] (calculate)
        {Calculate sum, product, difference};
  \node [decision, below of=calculate] (q_first_integer_smallest)
        {First integer less than second and third?};
  \node [block, on grid=false, below of=q_first_integer_smallest,
         node distance=3cm] (first_integer_smallest)
        {First integer is smallest};
  \node [decision, left of=q_first_integer_smallest,
         node distance=4.5cm] (q_second_integer_smallest)
        {Second integer less than second and third?};
  \node [block, below of=q_second_integer_smallest,
         node distance=3cm]
        (second_integer_smallest) {Second integer is smallest};
  \node [empty, below of=second_integer_smallest, node distance=1.25cm,
         inner sep=0pt] (e_second_integer_smallest) {};
  \node [block, left of=q_second_integer_smallest,
         node distance=4.5cm] (third_integer_smallest)
        {Third integer is smallest};
  \node [empty, below of=first_integer_smallest, node distance=1.25cm,
         inner sep=0pt] (e_first_integer_smallest) {};

  \node [decision, below of=first_integer_smallest,
         node distance=4cm] (q_first_integer_largest)
        {First integer larger than second and third?};
  \node [block, below of=q_first_integer_largest,
         node distance=3cm] (first_integer_largest)
        {First integer is largest};
  \node [decision, left of=q_first_integer_largest,
         node distance=4.5cm] (q_second_integer_largest)
        {Second integer larger than second and third?};
  \node [block, below of=q_second_integer_largest,
         node distance=3cm] (second_integer_largest)
        {Second integer is largest};
  \node [empty, below of=second_integer_largest, node distance=2cm,
         inner sep=0pt] (e_second_integer_largest) {};
  \node [block, left of=q_second_integer_largest,
         node distance=4.5cm] (third_integer_largest)
        {Third integer is largest};
  \node [block, below of=first_integer_largest,
         node distance=2cm] (output) {Output results};

  % Draw edges
  \path [line] (read_first_integer) -- (read_second_integer);
  \path [line] (read_second_integer) -- (read_third_integer);
  \path [line] (read_third_integer) -- (calculate);
  \path [line] (calculate) -- (q_first_integer_smallest);

  \path [line] (q_first_integer_smallest) -- node [near start]
               {no} (q_second_integer_smallest);
  \path [line] (q_first_integer_smallest) -- node [near start]
               {yes} (first_integer_smallest);
  \path [line] (q_second_integer_smallest) -- node [near start]
               {no} (third_integer_smallest);
  \path [line] (q_second_integer_smallest) -- node [near start]
               {yes} (second_integer_smallest);
  \path [line] (first_integer_smallest) -- (q_first_integer_largest);
  \path [line] (second_integer_smallest) -- (e_second_integer_smallest);
  \path [line] (third_integer_smallest) |- (e_first_integer_smallest);

  \path [line] (q_first_integer_largest) -- node [near start]
               {no} (q_second_integer_largest);
  \path [line] (q_first_integer_largest) -- node [near start]
               {yes} (first_integer_largest);
  \path [line] (q_second_integer_largest) -- node [near start]
               {no} (third_integer_largest);
  \path [line] (q_second_integer_largest) -- node [near start]
               {yes} (second_integer_largest);
  \path [line] (first_integer_largest) -- (output);
  \path [line] (second_integer_largest) -- (e_second_integer_largest);
  \path [line] (third_integer_largest) |- (output);
\end{tikzpicture}
\end{center}

\end{document}

The result:enter image description here

And, why is the line between "First integer larger than second and third?" and "first integer is largest" so small?

3

No need of defining a new node e_second_integer_largest. You can use orthogonal coordinates like

(second_integer_smallest |- q_first_integer_largest)

which means that x coordinate same as second_integer_smallest and y coordinate same as q_first_integer_largest in

\path [line] (second_integer_smallest) -- (second_integer_smallest |- q_first_integer_largest);

The line between "First integer larger than second and third?" and "first integer is largest" so small because of an extra line of text. You can adjust the text width in the definition of decision node style like text width=8em,

\documentclass{article}

\usepackage[margin=0.5in]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}

\begin{document}
\pagestyle{empty}

\tikzstyle{decision}=[diamond, draw, fill=yellow!20,
  text width=8em, text badly centered, node distance=3cm,
  inner sep=0pt]
\tikzstyle{block}=[rectangle, draw, fill=blue!20,
  text width=10em, text badly centered, rounded corners,
  minimum height=4em]
\tikzstyle{line}=[draw, very thick, color=black!75, -latex']
\tikzstyle{empty}=[]

\begin{center}
\begin{tikzpicture}[node distance=2cm, auto]
  % Place nodes
  \node [block] (read_first_integer) {Read first integer};
  \node [block, below of=read_first_integer] (read_second_integer)
        {Read second integer};
  \node [block, below of=read_second_integer] (read_third_integer)
        {Read third integer};
  \node [block, below of=read_third_integer] (calculate)
        {Calculate sum, product, difference};
  \node [decision, below of=calculate] (q_first_integer_smallest)
        {First integer less than second and third?};
  \node [block, on grid=false, below of=q_first_integer_smallest,
         node distance=3cm] (first_integer_smallest)
        {First integer is smallest};
  \node [decision, left of=q_first_integer_smallest,
         node distance=4.5cm] (q_second_integer_smallest)
        {Second integer less than second and third?};
  \node [block, below of=q_second_integer_smallest,
         node distance=3cm]
        (second_integer_smallest) {Second integer is smallest};
%  \node [empty, below of=second_integer_smallest, node distance=1.25cm,
%         inner sep=0pt] (e_second_integer_smallest) {};
  \node [block, left of=q_second_integer_smallest,
         node distance=4.5cm] (third_integer_smallest)
        {Third integer is smallest};
  \node [empty, below of=first_integer_smallest, node distance=1.25cm,
         inner sep=0pt] (e_first_integer_smallest) {};

  \node [decision, below of=first_integer_smallest,
         node distance=4cm] (q_first_integer_largest)
        {First integer larger than second and third?};
  \node [block, below of=q_first_integer_largest,
         node distance=3cm] (first_integer_largest)
        {First integer is largest};
  \node [decision, left of=q_first_integer_largest,
         node distance=4.5cm] (q_second_integer_largest)
        {Second integer larger than second and third?};
  \node [block, below of=q_second_integer_largest,
         node distance=3cm] (second_integer_largest)
        {Second integer is largest};
%  \node [empty, below of=second_integer_largest, node distance=2cm,
%         inner sep=0pt] (e_second_integer_largest) {};
  \node [block, left of=q_second_integer_largest,
         node distance=4.5cm] (third_integer_largest)
        {Third integer is largest};
  \node [block, below of=first_integer_largest,
         node distance=2cm] (output) {Output results};

  % Draw edges
  \path [line] (read_first_integer) -- (read_second_integer);
  \path [line] (read_second_integer) -- (read_third_integer);
  \path [line] (read_third_integer) -- (calculate);
  \path [line] (calculate) -- (q_first_integer_smallest);

  \path [line] (q_first_integer_smallest) -- node [near start]
               {no} (q_second_integer_smallest);
  \path [line] (q_first_integer_smallest) -- node [near start]
               {yes} (first_integer_smallest);
  \path [line] (q_second_integer_smallest) -- node [near start]
               {no} (third_integer_smallest);
  \path [line] (q_second_integer_smallest) -- node [near start]
               {yes} (second_integer_smallest);
  \path [line] (first_integer_smallest) -- (q_first_integer_largest);
  \path [line] (second_integer_smallest) -- (second_integer_smallest |- e_first_integer_smallest);
  \path [line] (third_integer_smallest) |- (e_first_integer_smallest);

  \path [line] (q_first_integer_largest) -- node [near start]
               {no} (q_second_integer_largest);
  \path [line] (q_first_integer_largest) -- node [near start]
               {yes} (first_integer_largest);
  \path [line] (q_second_integer_largest) -- node [near start]
               {no} (third_integer_largest);
  \path [line] (q_second_integer_largest) -- node [near start]
               {yes} (second_integer_largest);
  \path [line] (first_integer_largest) -- (output);
  %\path [line] (second_integer_largest) -- (e_second_integer_largest);
  \path [line] (second_integer_largest) -- (second_integer_largest |- output);
  \path [line] (third_integer_largest) |- (output);
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

| improve this answer | |
2

Loading the positioning library you can use below = of <node name> instead of below of = <node name>. Then node distance=<length> means the distance between the node borders. So there is no more need to change the node distance depending on the node size.

I have also replaced \tikzstyle by \tikzset.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning}
\tikzset{
  decision/.style={diamond,draw,fill=yellow!20,text width=6em,
    inner sep=.1em,text badly centered},
  block/.style={draw, fill=blue!20,text width=10em,
    text badly centered, rounded corners,minimum height=4em},
  line/.style={draw, very thick, color=black!75, -latex'},
  empty/.style={inner sep=0pt}
}
\begin{document}
\pagestyle{empty}
\begin{center}
\begin{tikzpicture}[node distance=.6cm, auto]
  % Place nodes
  \node [block] (read_first_integer) {Read first integer};
  \node [block, below = of read_first_integer] (read_second_integer)
        {Read second integer};
  \node [block, below = of read_second_integer] (read_third_integer)
        {Read third integer};
  \node [block, below = of read_third_integer] (calculate)
        {Calculate sum, product, difference};
  \node [decision, below = of calculate] (q_first_integer_smallest)
        {First integer less than second and third?};
  \node [block, below = of q_first_integer_smallest] (first_integer_smallest)
        {First integer is smallest};
  \node [decision, left = of q_first_integer_smallest] (q_second_integer_smallest)
        {Second integer less than second and third?};
  \node [block, below = of q_second_integer_smallest]
        (second_integer_smallest) {Second integer is smallest};
  \node [block, left = of q_second_integer_smallest] (third_integer_smallest)
        {Third integer is smallest};
  \node [empty, below = of first_integer_smallest] (e_first_integer_smallest) {};
  \node [decision, below = of e_first_integer_smallest] (q_first_integer_largest)
        {First integer larger than second and third?};
  \node [block, below = of q_first_integer_largest] (first_integer_largest)
        {First integer is largest};
  \node [decision, left = of q_first_integer_largest] (q_second_integer_largest)
        {Second integer larger than second and third?};
  \node [block, below = of q_second_integer_largest] (second_integer_largest)
        {Second integer is largest};
  \node [block, left = of q_second_integer_largest] (third_integer_largest)
        {Third integer is largest};
  \node [block, below = of first_integer_largest] (output) {Output results};
  % Draw edges
  \begin{scope}[every path/.style=line]
    \path (read_first_integer) -- (read_second_integer);
    \path (read_second_integer) -- (read_third_integer);
    \path (read_third_integer) -- (calculate);
    \path (calculate) -- (q_first_integer_smallest);
    \path (q_first_integer_smallest)
          -- node [near start]{no} (q_second_integer_smallest);
    \path (q_first_integer_smallest)
          -- node [near start]{yes} (first_integer_smallest);
    \path (q_second_integer_smallest)
          -- node [near start]{no} (third_integer_smallest);
    \path (q_second_integer_smallest)
          -- node [near start]{yes} (second_integer_smallest);
    \path (first_integer_smallest) -- (q_first_integer_largest);
    \path (second_integer_smallest)
          -- (second_integer_smallest |- e_first_integer_smallest);
    \path (third_integer_smallest) |- (e_first_integer_smallest);
    \path (q_first_integer_largest)
          -- node [near start]{no} (q_second_integer_largest);
    \path (q_first_integer_largest)
          -- node [near start]{yes} (first_integer_largest);
    \path (q_second_integer_largest)
          -- node [near start]{no} (third_integer_largest);
    \path (q_second_integer_largest)
          -- node [near start]{yes} (second_integer_largest);
    \path (first_integer_largest) -- (output);
    \path (second_integer_largest) -- (second_integer_largest |- output);
    \path (third_integer_largest) |- (output);
  \end{scope}    
\end{tikzpicture}
\end{center}
\end{document}

Result:

enter image description here


Another possibility is using a matrix to position the nodes. The column sep and the row sep are the minimum distance between the borders of the nodes.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,matrix}
\tikzset{
  decision/.style={diamond,draw,fill=yellow!20,text width=6em,
    text badly centered,inner sep=.1em},
  block/.style={draw, fill=blue!20,text width=10em,
    text badly centered, rounded corners,minimum height=4em},
  line/.style={draw, very thick, color=black!75, -latex'},
  empty/.style={inner sep=0pt}
}
\begin{document}
\pagestyle{empty}
\begin{center}
\begin{tikzpicture}[auto]
  % Place nodes
  \matrix[matrix of nodes,row sep=6mm,column sep=6mm,nodes={anchor=center}]{
    &&|[block](read_first_integer)|Read first integer\\
    &&|[block](read_second_integer)|Read second integer\\
    &&|[block](read_third_integer)|Read third integer\\
    &&|[block](calculate)|Calculate sum, product, difference\\
    |[block](third_integer_smallest)|Third integer is smallest&
      |[decision](q_second_integer_smallest)|Second integer less than second and third?&
      |[decision](q_first_integer_smallest)|First integer less than second and third?\\
    &|[block](second_integer_smallest)|Second integer is smallest&
      |[block](first_integer_smallest)|First integer is smallest\\
    &&|[empty](e_first_integer_smallest)|\\
    |[block](third_integer_largest)|Third integer is largest?&
      |[decision](q_second_integer_largest)|Second integer larger than second and third?&
      |[decision](q_first_integer_largest)|First integer larger than second and third?\\
    &|[block](second_integer_largest)|Second integer is largest&
      |[block](first_integer_largest)|First integer is largest\\
    &&|[block](output)|Output results\\
  };
  % Draw edges
  \begin{scope}[every path/.style=line]
    \path (read_first_integer) -- (read_second_integer);
    \path (read_second_integer) -- (read_third_integer);
    \path (read_third_integer) -- (calculate);
    \path (calculate) -- (q_first_integer_smallest);
    \path (q_first_integer_smallest)
          -- node [near start]{no} (q_second_integer_smallest);
    \path (q_first_integer_smallest)
          -- node [near start]{yes} (first_integer_smallest);
    \path (q_second_integer_smallest)
          -- node [near start]{no} (third_integer_smallest);
    \path (q_second_integer_smallest)
          -- node [near start]{yes} (second_integer_smallest);
    \path (first_integer_smallest) -- (q_first_integer_largest);
    \path (second_integer_smallest)
          -- (second_integer_smallest |- e_first_integer_smallest);
    \path (third_integer_smallest) |- (e_first_integer_smallest);
    \path (q_first_integer_largest)
          -- node [near start]{no} (q_second_integer_largest);
    \path (q_first_integer_largest)
          -- node [near start]{yes} (first_integer_largest);
    \path (q_second_integer_largest)
          -- node [near start]{no} (third_integer_largest);
    \path (q_second_integer_largest)
          -- node [near start]{yes} (second_integer_largest);
    \path (first_integer_largest) -- (output);
    \path (second_integer_largest) -- (second_integer_largest |- output);
    \path (third_integer_largest) |- (output);
  \end{scope}
\end{tikzpicture}
\end{center}
\end{document}

Result:

enter image description here

| improve this answer | |

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.