How to make an enumerating in a foreach command along with its positioning in TikZ?

Viewed 80

I am using TikZ in LaTeX. I want to make a foreach to enumerate text in several position. Example;

Text 'Statement 1' in coordinate A;
Text 'Statement 2' in coordinate B;
Text 'Statement 3' in coordinate C;
Text 'Statement 4' in coordinate D;

Here is my code

    \documentclass[12pt]{article}
    \usepackage[utf8]{inputenc}
    \usepackage{tikz}
    \usetikzlibrary{intersections, angles, quotes}
    \usepackage{pgffor}
    \usepackage{amsfonts}
    \usepackage{amsmath}
    \usepackage{amssymb}

    \begin{document}
    \begin{center}
    \begin{tikzpicture}
    \draw[very thick] (7,5) rectangle (11,6.5);
    \coordinate (A) at (9,5.75);
    \node at (A) {IF-CONDITIONAL};
    \draw[very thick, ->] (9,5)--(9,3);
    \foreach \s in {3, -2.5, -8, -13.5} \draw[very thick]
    (9,\s)--(6,{\s-2})--(9,{\s-4})--(12,{\s-2})--cycle;
    \node at (9,1) {Condition 1 (\textit{true?})};
    \node at (9,-4.5) {Condition 2 (\textit{true?})};
    \node at (9,-10){Condition ... (\textit{true?})};
    \node at (9,-15.5){Else (\textit{true?})};
    \foreach \s in {-1, -6.5, -12} \draw[very thick, ->] (9,\s)--(9,{\s-1.5}) 
    node[fill=white, midway]{NO};
    \foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (12,\s)--(14,\s) 
    node[fill=white, midway]{YES};
    \foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (19, \s)--(20, \s);
    \foreach \x in {0,-5.5, -11, -16.5} \draw[very thick] (14,\x) rectangle (19,{\x+2});
    \draw[very thick, ->] (9, -17.5)--(9,-19)--(20, -19);
    \node[fill=white] at (9, -18.25){NO};
    \draw[very thick, ->] (20, 1)--(20, -21);
    \node at (16.5,1){Statement 1};
    \end{tikzpicture}
    \end{center}
    \end{document}

This code produce the output

A chartflow of conditional-algorithm

How to write the text 'Statement 1/2/3/4' in the blank boxes using foreach ?

1 Answers

To answer the question you ask: You can use the count option of \foreach

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{intersections, angles, quotes}
\usepackage{pgffor}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
\begin{center}
\begin{tikzpicture}
\draw[very thick] (7,5) rectangle (11,6.5);
\coordinate (A) at (9,5.75);
\node at (A) {IF-CONDITIONAL};
\draw[very thick, ->] (9,5)--(9,3);
\foreach \s in {3, -2.5, -8, -13.5} \draw[very thick]
(9,\s)--(6,{\s-2})--(9,{\s-4})--(12,{\s-2})--cycle;
\node at (9,1) {Condition 1 (\textit{true?})};
\node at (9,-4.5) {Condition 2 (\textit{true?})};
\node at (9,-10){Condition ... (\textit{true?})};
\node at (9,-15.5){Else (\textit{true?})};
\foreach \s in {-1, -6.5, -12} \draw[very thick, ->] (9,\s)--(9,{\s-1.5}) 
node[fill=white, midway]{NO};
\foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (12,\s)--(14,\s) 
node[fill=white, midway]{YES};
\foreach \s in {1, -4.5, -10, -15.5} \draw[very thick, ->] (19, \s)--(20, \s);
\foreach[count=\xi] \x in {0,-5.5, -11, -16.5}{ 
  \node[draw,very thick,minimum width=5cm,minimum height=2cm] at (16.5,\x+1) {Statement \xi};
}
\draw[very thick, ->] (9, -17.5)--(9,-19)--(20, -19);
\node[fill=white] at (9, -18.25){NO};
\draw[very thick, ->] (20, 1)--(20, -21);
\end{tikzpicture}
\end{center}
\end{document}

However instead of manually messing around with absolute coordinates, you could simply let tikz do the positioning for you:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{intersections, angles, quotes,positioning,shapes.geometric}
\usepackage{pgffor}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
\begin{center}
\begin{tikzpicture}[
  scale=0.6,
  transform shape,
  very thick,
  node distance=1.6cm and 3cm,
  dia/.style={draw,diamond,shape aspect=1.3,minimum width=6cm,minimum height=4cm}
]

\node[draw,minimum height=1.5cm,minimum width=4cm] (A) {IF-CONDITIONAL};

\node[dia] (B1) [below=of A] {Condition 1 (\textit{true?})};
\node[dia] (B2) [below=of B1] {Condition 2 (\textit{true?})};
\node[dia] (B3) [below=of B2] {Condition ... (\textit{true?})};
\node[dia] (B4) [below=of B3] {Else (\textit{true?})};

\draw[ ->] (A)  -- (B1);
\draw[ ->] (B1) -- (B2) node[fill=white,midway] {NO};
\draw[ ->] (B2) -- (B3) node[fill=white,midway] {NO};
\draw[ ->] (B3) -- (B4) node[fill=white,midway] {NO};

\foreach \x in {1,...,4}{ 
  \node[draw,minimum width=5cm,minimum height=2cm] (C\x) [right=of B\x] {Statement \x};
  \draw[ ->] (B\x) -- (C\x) node[fill=white,midway] {YES};
  \draw[ ->] (C\x.east) -- ++(1cm,0);
}

\draw[ ->] (B4.south) |- ++(12cm,-1.5cm) node[fill=white,near start] {NO};
\draw[->] (C1) -| ([xshift=12cm,yshift=-3cm]B4.south);
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Related