1

How would I go about drawing something that looks like this (please excuse the extremely lousy Paint image)

lousy tree

with branches continuing to the right.

I am sorry I cannot provide any exact code, but I am basically clueless as to how I'd do this. I hope you have understanding. Thanks in advance.

  • Please at least provide the basic structure of a document. This makes it much easier to help. Nobody expects you to provide the 'exact code' - if you could do that, you wouldn't be asking the question ;) - but the framework for a solution is very helpful. – cfr Mar 22 '15 at 20:17
5

This solution uses forest to create the tree. You enter the tree using standard bracket notation.

The following styles are created:

  • my node which creates an elliptical node with various attributes - this is used for all main nodes of the tree;
  • my label which takes 2 arguments: one specifies position etc. of the label and the other the content - this is used for the small labels on the branches between the main nodes of the tree;
  • my top which takes 1 argument: this specifies the content of the rectangular node on top of a main node;
  • my label node and my top node specify the appearance etc. of the nodes created using my label={}{} and my top={} respectively.

The nice thing about this is that the tree itself can then be specified very simply and concisely:

[A\\10, my top=5
  [B\\15, my top=5, my label={above left}{7}]
  [C\\18, my top=5, my label={above}{8}]
  [D\\20, my top=5, my label={below left}{6}]
]

tree

\documentclass[tikz, border=5pt]{standalone}
\usepackage{forest}
\usetikzlibrary{shapes.geometric,backgrounds}

\begin{document}
  \forestset{
    my label/.style n args=2{
       edge label={node [my label node, #1] {#2}},
    },
    my top/.style={
      tikz={
        \node [my top node] at (.north) {#1};
      }
    },
  }
  \tikzset{
    my node/.style={shape=ellipse,  draw, inner xsep=15pt, inner ysep=1.5pt, fill=white, thick},
    my top node/.style={draw, anchor=south, on background layer, inner ysep=2pt, yshift=-.5pt, thick, fill=white},
    my label node/.style={auto, midway, font=\sffamily\scriptsize, inner sep=1.5pt},
  }
  \begin{forest}
    for tree={
      grow'=0,
      child anchor=west,
      align=center,
      s sep+=5pt,
      l sep+=10pt,
      font=\sffamily,
      edge=thick,
      thick,
      my node,
    }
    [A\\10, my top=5
      [B\\15, my top=5, my label={above left}{7}]
      [C\\18, my top=5, my label={above}{8}]
      [D\\20, my top=5, my label={below left}{6}]
    ]
  \end{forest}
\end{document}
  • This works wonders! I suggest the OP use this code over mine, especially if the overall tree is to be large. – Christopher Mar 22 '15 at 21:27
  • @Christopher Welcome to forest! – cfr Mar 22 '15 at 21:35
5

Like this,

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes}
\begin{document}

\tikzstyle{cloud} = [draw, ellipse, node distance=8em,
minimum height=2.75em, minimum width=5.5em]
\tikzstyle{box} = [draw, rectangle, node distance=2.8em]    
\tikzstyle{line} = [draw, -latex']

\begin{tikzpicture}[thick]
%Main nodes
\node[cloud] (a) {\begin{tabular}{c}A \\ 10 \end{tabular}};
\node[right of=a] (invis) {}; %Invisibile node for extra separation 
\node[cloud,right of=invis] (c) {\begin{tabular}{c} C \\ 18 \end{tabular}};
\node[cloud,above of=c] (b) {\begin{tabular}{c}B  \\15 \end{tabular}};
\node[cloud,below of=c] (d) {\begin{tabular}{c}D  \\20 \end{tabular}};
%Extra nodes
\node [draw, box, above of=a] (x) {5};
\node [draw, box, above of=c] (y) {5};
\node [draw, box, above of=b] (z) {5};
\node [draw, box, above of=d] (q) {5};

% Drawing arrows
\path [line] (a) -- node[above] {8} (c);
\path [line] (a) -- node[above] {7} (b);
\path [line] (a) -- node[below] {6} (d);

\end{tikzpicture}
\end{document}

The output of which looks like:

Tikz diagram output

A good tip to drawing in Tikz is to find an example that looks similar and adapt it to your needs.

Edit: If you want more stuff off to the right then just add more nodes with "right of" to position them.

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.