19

I created a npm package to write easily a TIKZ flowchart. Flowtex facilitates maintenance of a flowchart. Such as the insertion of new nodes or the positioning of the nodes as a function of others. I hope it will be as useful to you as for me.

https://www.npmjs.com/package/flowtex

Using flowtex.sty on github based on Creating Flowcharts with TikZ

tutorial & doc

Flowchart flowchart

Flowtex Code

flowchart.offsetX("3");
  N('Start').belowGoto(
    P('k <- 0').belowGoto(
      loop = IO('Loop ?').belowGoto(
        D('Yes or No ?').offsetY(-1) // use offsetY with D
         .rightGoto(kp1 = P('k <- k + 1')).topLabel('yes')
         .belowGoto( // use offsetY for this node because is immediatly below of D
          P('Print k').offsetY(-1).belowGoto(
            P('Stop')
          ).leftLabel('exit')
        ).leftLabel('no')
      ).leftLabel('Looping')
    ).leftLabel('Start loop')
  ).leftLabel('Init');
  kp1.goto(loop).brokenArrow();

LaTeX code using TIKZ

\begin{center}
  \begin{tikzpicture}[node distance=2cm]
    \node (node0) [startstop] {Start};
    \node (node1) [process, below of=node0] {k $\leftarrow$ 0};
    \node (node2) [io, below of=node1] {Loop ?};
    \node (node3) [decision, below of=node2, yshift=-1cm] {Yes or No ?};
    \node (node4) [process, right of=node3, xshift=3cm] {k $\leftarrow$ k + 1};
    \node (node5) [process, below of=node3, yshift=-1cm] {Print k};
    \node (node6) [process, below of=node5] {Stop};
    \draw [arrow] (node3) --node[anchor=south] {yes} (node4);
    \draw [arrow] (node3) --node[anchor=east] {no} (node5);
    \draw [arrow] (node5) --node[anchor=east] {exit} (node6);
    \draw [arrow] (node2) --node[anchor=east] {Looping} (node3);
    \draw [arrow] (node1) --node[anchor=east] {Start loop} (node2);
    \draw [arrow] (node0) --node[anchor=east] {Init} (node1);
    \draw [arrow] (node4) |- (node2);
  \end{tikzpicture}
\end{center}

Currently I use \input to insert the generated LaTeX code. But, I would like create a LaTeX command like this:

\begin{flowtex}
Here, the Flowtex code.
\end{flowtex}

Would you know how to do?

2 Answers2

14

An alternative to the @A.Ellet answer (both consider the first version of your question):

 \documentclass[border=3mm,
                tikz]{standalone}
\usetikzlibrary{arrows.meta,
                calc, chains,
                quotes,
                positioning,
                shapes.geometric}

\begin{document} \begin{tikzpicture}[ node distance = 8mm and 16mm, start chain = A going below, base/.style = {draw, minimum width=32mm, minimum height=8mm, align=center, on chain=A}, startstop/.style = {base, rounded corners, fill=red!30}, process/.style = {base, fill=orange!30}, io/.style = {base, trapezium, trapezium left angle=70, trapezium right angle=110, fill=blue!30}, decision/.style = {base, diamond, fill=green!30}, every edge quotes/.style = {auto=right}] ] \node [startstop] {Start}; % <-- A-1 \node [process] {k $\gets$ 0}; \node [io] {Loop ?}; \node [decision] {Yes or No ?}; \node [process] {Print k}; \node [process] {Stop}; % <-- A-6 % \node [process, % <-- A-7 right=of A-4] {k $\gets$ k + 1}; %% \draw [arrows=-Stealth] (A-1) edge["init"] (A-2) (A-2) edge["start stop"] (A-3) (A-3) edge["looping"] (A-4) (A-4) edge["no"] (A-5) (A-5) edge["exit"] (A-6) (A-4) edge["yes"'] (A-7) % <-- by ' is swapped label position (A-7) |- ($(A-2.south east)!0.5!(A-3.north east)$) -| ([xshift=7mm] A-3.north); \end{tikzpicture} \end{document}

From comparison of both solution you can observe the following main differences:

  • nodes in main branch are set as chain (this significantly simplify code)
  • for node names are used names which is defined by chain
  • for nodes' common parameters is defined new style named base
  • for drawing edges between nodes are used quotes package by which syntax for writing edges labels significantly shorter needed code.

The compilation of above MWE (Minimal Working Example) produce similar picture as is shown in @A.Ellett answer with important differences: the feedback edge is drawn more correct (according to my opinion).

enter image description here

Zarko
  • 296,517
  • What's the syntax for |-["yes"]? Google is bad at searching for pipe/line/|-/right angle/etc.. – John Moser May 24 '20 at 03:01
  • @JohnMoser, in my code |- ["yes"] is not present (since it is erroneous, it gives error : tikz cannot parse this coordinate). Correct is to ["yes"] (or edge ["yes"]) means that arrow (from <coordinate 1> to >coordinate 2>) has label "yes". For this syntax you need to have loaded TikZ library quotes. Symbol |- designate perpendicular arrow system, where arrow first go from <coordinate 1> up (or down) and then right to <coordinate 2>. See TikZ & PGF Manual, 13.3.1 Intersections of Perpendicular Lines, page 144 – Zarko May 24 '20 at 07:40
7

There's quite a bit missing from your MWE: which libraries you're using etc. Nevertheless, I haven't really done anything here. I copied your code and then plagiarized the rest from this webpage.

Except for color, the following code should do what you want.

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{arrows.meta}
\begin{document}

  \begin{tikzpicture}[
    node distance=2cm,
    startstop/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30},
    process/.style={rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30},
    io/.style={trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30},
    decision/.style={diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30},
    ]

    \node (node0) [startstop]                             {Start};
    \node (node1) [process, below of=node0]               {k $\leftarrow$ 0};
    \node (node2) [io, below of=node1]                    {Loop ?};     
    \node (node3) [decision, below of=node2, yshift=-1cm] {Yes or No ?};
    \node (node4) [process, right of=node3, xshift=3cm]   {k $\leftarrow$ k + 1};
    \node (node5) [process, below of=node3, yshift=-1cm]  {Print k};
    \node (node6) [process, below of=node5]               {Stop};

    \draw [arrows=-Stealth] (node3) --node[anchor=south]            {yes}        (node4);
    \draw [arrows=-Stealth] (node3) --node[anchor=east]             {no}         (node5);
    \draw [arrows=-Stealth] (node5) --node[anchor=east]             {exit}       (node6);
    \draw [arrows=-Stealth] (node2) --node[anchor=east]             {Looping}    (node3);
    \draw [arrows=-Stealth] (node1) --node[anchor=east]             {Start loop} (node2);
    \draw [arrows=-Stealth] (node0) --node[anchor=east]             {Init}       (node1);
    \draw [arrows=-Stealth] (node4) -- (node2);

  \end{tikzpicture}

\end{document}

enter image description here

I'll leave it to you to tweak there rest to get the colors you want (afterall, I'm colorblind).

A.Ellett
  • 50,533