6

I'm trying to create the following diagram:

          |Welcome|
|Low|                   |High|

         |Truth|

          |Lie|

So far, this is what I've come up with:

enter image description here

The idea is that the node Welcome is in the center of the page, with Low and High on either sides of it. Then, the nodes Truth and Lie should be in the center of the page again.

This is my MWE:

\documentclass[11pt]{report}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usepackage{pgfplots}

\begin{document}

\tikzstyle{comp1} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm, fill=gray!50]
\tikzstyle{comp2} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm]
\tikzstyle{comp3} = [draw, ellipse, minimum height=1cm, minimum width=4cm, fill=gray!10, text centered]

\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{figure}
\centering

\begin{tikzpicture}[node distance=2cm]
%%% NODES %%%
\node (welcome)     [comp1]                             {Welcome};

\node (low)         [comp2, below of=welcome]           {Low};
\node (high)        [comp2, right of=low, xshift=3cm]   {High};
\node (truth)       [comp3, below of=low]               {Truth};
\node (lie)         [comp3, below of=truth]             {Lie};

%%% ARROWS %%%
\draw [arrow] (welcome) -- (low);
\draw [arrow] (welcome) -- (high);
\draw [arrow] (low) -- (truth);
\draw [arrow] (high) -- (truth);
\draw [arrow] (truth) -- (lie);
\end{tikzpicture}
\end{figure}

\end{document}

I'm aware that this might have a simple solution, but I can't seem to find it, even after googling around. So, can anyone help?

  • Add a helper-node \node[below of=welcome, shape=coordinate](tmp){}; \node[comp2, left of=tmp](low){Low};\node[comp2, right of=tmp](high){High};\node[comp3, below left of=tmp](truth){Truth}; I'll add an Answer when I'm at a computer. – Richard Aug 9 '15 at 16:16
9

You can add an coordinate below of=welcome and then use it as reference:

enter image description here

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{shapes.geometric, arrows}

\begin{document}

\tikzstyle{comp1} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm, fill=gray!50]
\tikzstyle{comp2} = [draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm]
\tikzstyle{comp3} = [draw, ellipse, minimum height=1cm, minimum width=4cm, fill=gray!10, text centered]

\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{figure}
\centering

\begin{tikzpicture}[node distance=2.2cm]
%%% NODES %%%
\node (welcome)     [comp1]                 {Welcome};
\coordinate[below of=welcome] (c);
\node (low)         [comp2, left of=c]      {Low};
\node (high)        [comp2, right of=c]     {High};
\node (truth)       [comp3, below of=c]     {Truth};
\node (lie)         [comp3, below of=truth] {Lie};

%%% ARROWS %%%
\draw [arrow] (welcome) -- (low);
\draw [arrow] (welcome) -- (high);
\draw [arrow] (low) -- (truth);
\draw [arrow] (high) -- (truth);
\draw [arrow] (truth) -- (lie);
\end{tikzpicture}
\end{figure}

\end{document}

Update

Some "standard" improvements as suggested by Gonzalo Medina:

  • below=of welcome instead of below of=welcome
  • or (if you like) more precise below=2.4cm of welcome
  • \tikzset (new syntax) instead of \tikzstyle (old syntax)

Related: Should \tikzset or \tikzstyle be used to define TikZ styles?

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric, arrows}

\begin{document}

\tikzset{
    comp1/.style={draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm, fill=gray!50},
    comp2/.style={draw, rectangle, rounded corners, minimum height=1cm, minimum width=4cm},
    comp3/.style={draw, ellipse, minimum height=1cm, minimum width=4cm, fill=gray!10, text centered},
    arrow/.style={thick,->,>=stealth}
}

\begin{figure}
\centering

\begin{tikzpicture}[node distance=2cm and 1cm]
%%% NODES %%%
\node [comp1]                 (welcome) {Welcome};
\coordinate[below=of welcome] (c);
\node [comp2, left=of c]      (low)     {Low};
\node [comp2, right=of c]     (high)    {High};
\node [comp3, below=of c]     (truth)   {Truth};
\node [comp3, below=of truth] (lie)     {Lie};

%%% ARROWS %%%
\draw [arrow] (welcome) -- (low);
\draw [arrow] (welcome) -- (high);
\draw [arrow] (low)     -- (truth);
\draw [arrow] (high)    -- (truth);
\draw [arrow] (truth)   -- (lie);
\end{tikzpicture}
\end{figure}

\end{document}
  • 1
    You could also suggest "standard" improvements: 1) Instead of the old of= syntax, one should use the positioning library and the more versatile and precise =of syntax. 2) Instead of \tikzstyle, use \tikzset. – Gonzalo Medina Aug 9 '15 at 16:32
  • @GonzaloMedina Thanks for your comment, I have added an improved version. – sergej Aug 9 '15 at 17:03

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.