3

I am learning how to make flow chart in tikz. In this MWE

\documentclass[crop,tikz]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}

\tikzstyle{NODE} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm, draw=black, fill=red!10] \begin{tikzpicture}[node distance=2cm]

\node (A1) [NODE] {my first node}; \node (A2) [NODE,below of=A1] {my second node}; \node (A3) [NODE,below of=A2] {is it true?}; \node (A4) [NODE,left of=A3,yshift=-2cm] {we get here if true}; \node (A5) [NODE,right of=A3,yshift=-2cm] {we get here if false};

\draw [->,thick] (A1.south) -| (A2.north); \draw [->,thick] (A2.south) -| (A3.north); \draw [->,thick] (A3.south) -| node[anchor=south] {YES} (A4.north); \draw [->,thick] (A3.south) -| node[anchor=south] {NO} (A5.north);

\end{tikzpicture}

\end{document}

It gives

enter image description here

I am not sure what is the correct way to produce this instead (which I did using paint.exe for illustration)

enter image description here

Do I need to make a hidden node below A3 first? in order to make the edges as shown? Or is there a better and canonical way to do this? I do not want the edges to be sloping, I like to use straight edges only. I know I could do this

\draw [->,thick] (A1.south) -|   (A2.north);
\draw [->,thick] (A2.south) -| (A3.north);
\draw [->,thick] (A3.south) -- node[anchor=south] {YES} (A4.north);
\draw [->,thick] (A3.south) -- node[anchor=south] {NO} (A5.north);

And get

enter image description here

But I like the straight edges more.

Can one make a node in tikz that has no size and do not show, and use it just as place holder to in order to make edges to it?

Nasser
  • 20,220

2 Answers2

5

Adaptations

  • use relative positioning to move to the point below the node: (A3.south) -- ++(0,-5mm)
  • save that point as coordinate(a) (this is the invisible node without a size you mention)
  • reuse it to draw the other line (a) -| ...
    • I left out the first part ((A3.south) -- (a)) because it would just draw a second line over the already existing one. You would only need this, if you use curved edges or colored arrows.
  • use tikzset instead of tikzstyle (see Should \tikzset or \tikzstyle be used to define TikZ styles?)

See Also

Code

\documentclass[crop,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}

\begin{document}

\begin{tikzpicture}[ node distance=2cm, NODE/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, draw=black, fill=red!10} ] \node (A1) [NODE] {my first node}; \node (A2) [NODE,below of=A1] {my second node}; \node (A3) [NODE,below of=A2] {is it true?}; \node (A4) [NODE,left of=A3,yshift=-2cm] {we get here if true}; \node (A5) [NODE,right of=A3,yshift=-2cm] {we get here if false};

\draw [->,thick] (A1.south) -|   (A2.north);
\draw [->,thick] (A2.south) -| (A3.north);
\draw [->,thick] (A3.south) -- ++(0,-5mm) coordinate(a) -| node[anchor=south] {YES} (A4.north);
\draw [->,thick] (a) -| node[anchor=south] {NO} (A5.north);

\end{tikzpicture}

\end{document}

Result

enter image description here

dexteritas
  • 9,161
4

Here is an alternative using forest.

enter image description here

\documentclass{article}

\usepackage{forest} \useforestlibrary{edges}

\begin{document}

\begin{forest} for tree={rounded corners, minimum width=3cm, minimum height=1cm, draw=black, fill=red!10, l sep=1cm, fork sep=5mm, s sep=1cm, forked edge, edge={thick, ->} } [my first node [my second node [is it true? [we get here if true, edge label={node[midway, above]{YES}}] [we get here if false, edge label={node[midway, above]{NO}}] ] ] ] \end{forest}

\end{document}

Sandy G
  • 42,558