1

I'm trying to reproduce a graph from a paper using Tikz, the graph that I'm trying to reproduce is the following:

What i want

But without the horizontal curved line and brackets on the left subtree. So far, this is what I get:

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}

\tikzset{
    treenode/.style = {align=center, inner sep=0pt, text centered,
        font=\sffamily\Large\bfseries, text width=5cm},
    burned/.style = {treenode, circle, white, draw=red, fill=red, text width=1.5em, very thick},
    saved/.style = {treenode, circle, white, draw=green, fill=green, text width=1.5em, very thick},
    untouch/.style = {treenode, circle, white, draw=gray, fill=gray, text width=1.5em, very thick}
}

\begin{document}

\begin{tikzpicture}[auto, node distance=2cm, thick]
% nodes
\node[burned](1){F};
    \node[saved](2) [below left of=1] {S};
    \node[untouch](3) [below left of=2] {};
    \node[untouch](4) [below right of=2] {};
    \node[burned](5) [below of=1] {B};
    \node[burned](6) [below right of=1] {B};
    \node[saved](7) [below of=5] {S};
    \node[burned](8) [below of=6] {B};
    \node[untouch](9) [below of=7] {};
    \node[saved](10) [below of=8] {S};
    \node[untouch](11) [below of=9] {};
    \node[untouch](12) [below of=11] {};
    \node[untouch](13) [below of=10] {};
    \node[untouch](14) [below of=13] {};

%edges
\path
    (1) edge node [right] {} (2)
        edge node [below] {} (5)
        edge node [left] {} (6)
    (2) edge node [right] {} (3)
        edge node [right] {} (4)
    (5) edge node [below] {} (7)
        edge node [left] {} (8)
    (7) edge node [below] {} (9)
    (9) edge node [below] {} (11)
    (11) edge node [below] {} (12)
    (6) edge node [right] {} (7)
        edge node [below] {} (8)
    (8) edge node [below] {} (10)
    (10) edge node [below] {} (13)
    (13) edge node [below] {} (14);
\end{tikzpicture}

\end{document}

Which produces the following graph:

What I get

So, basically I have three questions:

  • Is there any way to avoid the overlapping between the edges and nodes?
  • Also, there is any way to get the nodes from the same level to be aligned? (Like in the graph of the paper)
  • There is another LaTex package or tikz library with a more intuitive syntax to draw graphs (i.e. graphs with edges/nodes)? Maybe something similar do Graphviz dot syntax.
  • The sibling distance doesn't do anything here, because you're not actually using the tree syntax of TikZ. See chapter 21 Making Trees Grow in the manual for a description of how it works. – Torbjørn T. May 12 '16 at 19:56
  • Thanks @TorbjørnT. I was using the tree syntax before and I forgot to delete the sibling distance attribute. I edited my question for further reference. – Natanael Ramos May 12 '16 at 22:23
2

Probably someone will suggest using forest for this kind of graph. It will be a good suggestion. But while waiting for it, you can start using positioning library and its =of (instead of of=) syntax.

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
    treenode/.style = {align=center, inner sep=0pt, text centered,
        font=\sffamily\Large\bfseries, text width=5cm},
    burned/.style = {treenode, circle, white, draw=red, fill=red, text width=1.5em, very thick,sibling distance=30mm},
    saved/.style = {treenode, circle, white, draw=green, fill=green, text width=1.5em, very thick,sibling distance=15mm},
    untouch/.style = {treenode, circle, white, draw=gray, fill=gray, text width=1.5em, very thick,sibling distance=7.5mm}
}

\begin{document}

\begin{tikzpicture}[auto, thick, on grid, ]
% nodes
\node[burned](1){F};
    \node[saved](2) [below left = 1 and 2 of 1] {S};
    \node[untouch](3) [below left = 1 and 1 of 2] {};
    \node[untouch](4) [below right = 1 and 1 of 2] {};
    \node[burned](5) [below = 1 of 1] {B};
    \node[burned](6) [below right = 1 and 2 of 1] {B};
    \node[saved](7) [below = of 5] {S};
    \node[burned](8) [below = of 6] {B};
    \node[untouch](9) [below = of 7] {};
    \node[saved](10) [below = of 8] {S};
    \node[untouch](11) [below = of 9] {};
    \node[untouch](12) [below =of 11] {};
    \node[untouch](13) [below = of 10] {};
    \node[untouch](14) [below = of 13] {};

%edges
\path
    (1) edge node [right] {} (2)
        edge node [below] {} (5)
        edge node [left] {} (6)
    (2) edge node [right] {} (3)
        edge node [right] {} (4)
    (5) edge node [below] {} (7)
        edge node [left] {} (8)
    (7) edge node [below] {} (9)
    (9) edge node [below] {} (11)
    (11) edge node [below] {} (12)
    (6) edge node [right] {} (7)
        edge node [below] {} (8)
    (8) edge node [below] {} (10)
    (10) edge node [below] {} (13)
    (13) edge node [below] {} (14);
\end{tikzpicture}

\end{document}

enter image description here

Update: forest version.

\documentclass[a4paper,12pt]{article}
\usepackage{forest}

\tikzset{
    treenode/.style = {align=center, inner sep=0pt, text centered,
        font=\sffamily\Large\bfseries, text width=5cm},
    burned/.style = {treenode, circle, white, draw=red, fill=red, text width=1.5em, very thick,sibling distance=30mm},
    saved/.style = {treenode, circle, white, draw=green, fill=green, text width=1.5em, very thick,sibling distance=15mm},
    untouch/.style = {treenode, circle, white, draw=gray, fill=gray, text width=1.5em, very thick,sibling distance=7.5mm}
}

\begin{document}

\begin{forest}
[F,burned
    [S, saved, , for descendants={untouch}
        [][]]
    [B, burned, name=B1
        [S, saved, name=S1, for descendants={untouch} [[[]]]]]
    [B, burned, name=B2
        [B, burned, name=B3 [S, saved, , for descendants={untouch} [[]]]]]]
    \draw (B1)--(B3) (B2)--(S1);
\end{forest}
\end{document}

enter image description here

  • Awesome! Thanks for the answer @Ignasi. I will take a look at the forest package. – Natanael Ramos May 12 '16 at 22:26

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.