11

I have a problem trying to create a simple causal chain, as it can be seen on the attached file: List of available TikZ libraries with a short introduction.

I'm having two main issues:

  1. I would like to remove the "clouds" from X and Y.
  2. The arrow from Z should hit perpendicularly the arrow from X to Y.

I have spent hours trying to solve them, but without luck.

Here is my MWE so far:

\documentclass{article} 
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows}
%
\begin{document} 
%   
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse, node distance=3cm, minimum height=2em]
 \begin{tikzpicture}[node distance = 3cm, auto]
  \node [cloud] (init) {Z1};      
  \node [blank, below of=init] (sup) {};
  \node [cloud, left of=X1] (X1) {X1};
  \node [cloud, right of=Y1] (Y1) {Y1};    
  \path [line] (X1) -- (Y1);
  \path [line] (init) -- (sup);
\end{tikzpicture}
%
\end{document}

Here is the output:

enter image description here

The wanted image should appear like this:

hand drawn image

A. F.
  • 1,227
  • 1
  • 13
  • 21
  • Can you show us some of the code of your attempts so we can see where the problem is? – doncherry Nov 24 '13 at 22:40
  • I am sorry. I have now edited my post, so you can see how it looks like in my document. – A. F. Nov 24 '13 at 22:51
  • I have now added an extra photo. Maybe it helps. – A. F. Nov 24 '13 at 22:53
  • 1
    It should be there now. – A. F. Nov 24 '13 at 22:57
  • Your MWE defines a node, while simultaneously trying to place that node relative to itself. Also, the position of = syntax has been deprecated. See http://tex.stackexchange.com/questions/9386/difference-between-right-of-and-right-of-in-pgf-tikz Instead, use position = of node. – Adam Liter Nov 24 '13 at 23:18
  • 1
    @Jake: Sure, done … thanks for the reminder :-) I deleted my comment too. – Tobi Nov 25 '13 at 08:55

3 Answers3

12

You have some mistakes.

  1. There is no blank option for nodes.
  2. In \node [cloud, left of=X1] (X1) {X1};, left of=X1 is wrong as X1 is unknown at this point. Further, you are saying left of hence the X1 comes to the left at the same height. It should be below left = of init. Same is true for \node [cloud, right of=Y1] (Y1) {Y1}; You may refer to this question.
  3. With calc library, you can find the mid point of the line joing X1 and Y1. Hence you won't need \node [coordinate,below of=init] (sup) {};.
  4. Don't use \tikstyle, use \tikzset instead. Refer to this question.

Full code with correction:

\documentclass[a4paper,12pt,oneside]{article}
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows,positioning,calc}  %% Added last two libraries
\begin{document}
%% Use tikzset instead of tikzstyle which is deprecated
\tikzset{line/.style = {draw, -latex'},
        cloud/.style = {draw, ellipse, node distance=3cm,
                        minimum height=2em}
}
 \begin{tikzpicture}[node distance = 3cm, auto]
    \node [cloud] (init) {Z1};
    %\node [coordinate,below of=init] (sup) {}; not needed
    \node [cloud, below left = of init] (X1) {X1};  %% note below left syntax from positioning library 
    \node [cloud, below right = of init] (Y1) {Y1};

     \path [line] (X1) -- (Y1);   %% (($(X1)!0.5!(Y1)$) is mdpoint of this line
     \path [line] (init) -- ($(X1)!0.5!(Y1)$);  %% $expression$ possible with calc library
\end{tikzpicture}
\end{document}

enter image description here

If you don't want ellipse surrounding X1 and Y1, remove cloud option from them:

\node [below left = of init] (X1) {X1};  
\node [below right = of init] (Y1) {Y1};

to get

enter image description here

Also, if X1 and Y1 are math symbols/variables, don't forget to surround them with ${$X1$};

As Marc notes in the comments, you can avoid calc library by using \path[line] (init) -- (init -| X1);.

  • A helpful reference for point 2 is: http://tex.stackexchange.com/questions/9386/difference-between-right-of-and-right-of-in-pgf-tikz – Adam Liter Nov 24 '13 at 23:21
  • 1
    @Adam Thanks for the link. I added it in the answer. :-) –  Nov 24 '13 at 23:27
  • 1
    You can also draw the last arrow without the calc library: (init) -- (init -| X1). –  Nov 25 '13 at 10:26
  • @MarcvanDongen You are right :-) –  Nov 25 '13 at 12:23
  • Hi Harish Kumar. Thank you for a very helpful answer. If I would like to change names of Z1, X1 and Y1, should I just replace the names? Or? Because I can see the line from Z1 wouldn't be straight ? – A. F. Nov 25 '13 at 17:55
  • @A.F. Hi. Yes just change the names like \node [cloud] (init) {M}. What do you mean by not straight? –  Nov 25 '13 at 22:08
9

Your code is quite confusing. Is this what you wanted?

\documentclass[tikz]{standalone} 
\usepackage{tikz}
\usepackage{pgf}
\begin{document}
\usetikzlibrary{shapes, calc, positioning}


\begin{tikzpicture}
    %create X and Y node
    \node (X) {X}; 
    \node [right = 1.5 of X] (Y) {Y};
    %create a coordinate in the middle of (X) and (Y)
    \coordinate (middle) at ($(X)!0.5!(Y)$);
    %draw Z above this middle
    %add "\," before and after Z to add some space, so that you get a nice ellipse
    \node[ellipse, draw, above = .5 of middle] (Z) {\,Z\,};
    %draw arrow between (X) and (Y)
    \draw (X) edge[->] (Y);
    %draw arrow from (Z) to 1pt above (middle)
    \draw(Z) edge[->] ($(middle)+(0,1pt)$);
\end{tikzpicture}

\end{document}

results in:

result

someonr
  • 8,531
8

Other solution, also using TikZ.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
    \begin{tikzpicture}[%
        >=latex',
        circ/.style={draw, shape=circle, node distance=1cm, line width=1pt}]%Define the arrow type and style for circled nodes
        \draw[->] (0,0) node[left] (X) {X} -- (2,0) node[right] (Y) {Y}; %Line between X and Y
        \path (X) -- coordinate (middle) (Y);%Defining the middle between X and Y
        \node[above of=middle,circ] (Z) {Z};%Draw Z, a circled node
        \draw[->] (Z) -- (middle);%Line between Z and X-Y
    \end{tikzpicture}
\end{document}

Result

enter image description here

If you want to use a ellipse or another shape:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\begin{document}

    %With a ellipse
    \begin{tikzpicture}[%
        >=latex',
        cloud/.style={draw, thick, shape=ellipse, node distance=1cm, minimum width=0.8cm}]%Define the arrow type and style for ellipse nodes
        \draw[->,thick] (0,0) node[left] (X) {X} -- (2,0) node[right] (Y) {Y}; %Line between X and Y
        \path (X) -- coordinate (middle) (Y);%Defining the middle between X and Y
        \node[above of=middle,cloud] (Z) {Z};%Draw Z, a circled node
        \draw[->,thick] (Z) -- (middle);%Line between Z and X-Y
    \end{tikzpicture}

    %With a rectangle
    \begin{tikzpicture}[%
        >=latex',
        block/.style={rectangle,draw,thick,node distance=1cm,rounded corners}]%Define the arrow type and style for retangular nodes
        \draw[->,thick] (0,0) node[left] (X) {X} -- (2,0) node[right] (Y) {Y}; %Line between X and Y
        \path (X) -- coordinate (middle) (Y);%Defining the middle between X and Y
        \node[above of=middle,block] (Z) {Z};%Draw Z, a circled node
        \draw[->,thick] (Z) -- (middle);%Line between Z and X-Y
    \end{tikzpicture}
\end{document}

enter image description here

osjerick
  • 6,970