5

I'm trying to copy this image:

enter image description here

And this is what I have so far (spacing is different but it's ok):

enter image description here

All I need to do is add the horizontal line at the middle of the figure, at depth 2, behind all nodes. Here's my code:

\begin{tikzpicture}
\begin{scope}[auto, every node/.style={draw,circle,minimum size=2em,inner sep=1, font=\footnotesize}]

\node (1) [circle, draw, fill=lightgray] at (0,0) {};

\node (2) [circle, draw, fill=lightgray, below left=of 1, shift={(-1,0)}] {};
\draw[->](1)--(2);

    \node (4) [circle, draw, fill=lightgray, below left=of 2, shift={(0,0)}] {};
    \draw[->](2)--(4);

        \node (3) [circle, draw, fill=lightgray, below left=of 4, shift={(-.5,-1)}]  {0};
        \draw[dashed, ->](4)--(3);

        \node (6) [circle, draw, fill=lightgray, right=of 3, shift={(-1,0)}]  {100};
        \draw[dashed, ->](4)--(6);

        \node (7) [circle, draw, fill=lightgray, right=of 6, shift={(-1,0)}]  {100};
        \draw[dashed, ->](4)--(7);

        \node (8) [circle, draw, fill=lightgray, right=of 7, shift={(-1,0)}]  {0};
        \draw[dashed, ->](4)--(8);

    \node (5) [circle, draw, fill=lightgray, below right=of 2, shift={(0,0)}] {};
    \draw[->](2)--(5);
         \node (9) [circle, draw, fill=lightgray, below left=of 5, shift={(-.5,-1)}]  {100};
         \draw[dashed, ->](5)--(9);

        \node (10) [circle, draw, fill=lightgray, right=of 9, shift={(-1,0)}]  {0};
        \draw[dashed, ->](5)--(10);

        \node (11) [circle, draw, fill=lightgray, right=of 10, shift={(-1,0)}]  {0};
        \draw[dashed, ->](5)--(11);

        \node (12) [circle, draw, fill=lightgray, right=of 11, shift={(-1,0)}]  {0};
        \draw[dashed, ->](5)--(12);

\node (13) [circle, draw, fill=lightgray, below right=of 1, shift={(1,0)}] {};
\draw[->](1)--(13);

    \node (14) [circle, draw, fill=lightgray, below left=of 13, shift={(0,0)}] {};
    \draw[->](13)--(14);

        \node (16) [circle, draw, fill=lightgray, below right=of 14, shift={(.5,-1)}]  {0};
        \draw[dashed, ->](14)--(16);

        \node (17) [circle, draw, fill=lightgray, left=of 16, shift={(1,0)}]  {0};
        \draw[dashed, ->](14)--(17);

        \node (18) [circle, draw, fill=lightgray, left=of 17, shift={(1,0)}]  {0};
        \draw[dashed, ->](14)--(18);

        \node (19) [circle, draw, fill=lightgray, left=of 18, shift={(1,0)}]  {0};
        \draw[dashed, ->](14)--(19);

    \node (15) [circle, draw, fill=lightgray, below right=of 13, shift={(0,0)}] {};
    \draw[->](13)--(15);

        \node (20) [circle, draw, fill=lightgray, below right=of 15, shift={(.5,-1)}]  {100};
         \draw[dashed, ->](15)--(20);

        \node (21) [circle, draw, fill=lightgray, left=of 20, shift={(1,0)}]  {0};
        \draw[dashed, ->](15)--(21);

        \node (22) [circle, draw, fill=lightgray, left=of 21, shift={(1,0)}]  {100};
        \draw[dashed, ->](15)--(22);

        \node (23) [circle, draw, fill=lightgray, left=of 22, shift={(1,0)}]  {100};
        \draw[dashed, ->](15)--(23);
\end{scope}
\end{tikzpicture}

How can I add that line?

1
  • Please: that is not your code as that cannot possibly be compiled without somebody sitting down and completing it for you, guessing as best they may what's needed and which decisions you made. Help people to help you: make your code compilable before posting it, test it to ensure it really does compile and post something people can copy-paste-compile to reproduce the problem you want help with.
    – cfr
    Jun 9, 2016 at 22:03

1 Answer 1

2

In the preamble

\usetikzlibrary{backgrounds}

At the end of the picture

\scoped[on background layer]{\draw (current bounding box.west) -- (current bounding box.east);}

for example.

In the case of a tree, for example, I would do something like this:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{forest}
  for tree={
    circle,
    minimum size=25pt,
    draw,
    fill=lightgray,
    font=\small\sffamily,
    l sep+=10pt,
    edge={->}
  },
  where n children=0{
    tier=terminus,
    +edge=densely dashed,
  }{
    if level=2{
      tikz+={
        \scoped[on background layer]{\path [line width=1pt, draw=lightgray, line cap=round] (current bounding box.west |- .center) -- (current bounding box.east |- .center);}
      }
    }{}
  }
  [
    [
      [
        [0]
        [100]
        [100]
        [0]
      ]
      [
        [100]
        [0]
        [0]
        [0]
      ]
    ]
    [
      [
        [0]
        [0]
        [0]
        [0]
      ]
      [
        [100]
        [100]
        [0]
        [100]
      ]
    ]
  ]
\end{forest}
\end{document}

tree example

You don't necessarily wish to use forest, but using some tree-drawing facility will greatly simplify the task. Even if you just use TikZ's built-in tree stuff or the trees library, while your tree specification will be much less concise than mine is in Forest's syntax, it will still be less verbose and easier to maintain than placing everything manually.

6
  • I added a tree for you just because Forest is so nice ;).
    – cfr
    Jun 9, 2016 at 22:16
  • wow, this looks so good, thank you! I'm not too familiar with tikz yet, so I didn't know you could make trees like this
    – Tuma
    Jun 9, 2016 at 22:18
  • Well, this is Forest. TikZ has more basic tree-drawing facilities. Forest is TikZ-based and does a lot of specialist tree-drawing wizardry. It takes a little getting used to, but it can produce some very nice trees quite easily. (I'm a fan, though.) ;)
    – cfr
    Jun 9, 2016 at 22:22
  • Hey, just a note, in your code you have where n children=0{ tier=terminus, +edge=densely dashed,. I had to remove the + from +edge for it to work. Maybe youre using another package?
    – Tuma
    Jun 10, 2016 at 3:02
  • @Marituma You are probably using an older version of Forest. I'm using 2.03. Are you using version 1 or, maybe, an earlier version of 2?
    – cfr
    Jun 10, 2016 at 3:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .