5

I am trying to draw a diagram as given below in latex. I have little information about TikZ package? Is there any simple way to draw this diagram in latex?

I would be grateful if you could help on this matter.

enter image description here

2
  • Welcome to TeX.SX! I am sorry, but the package can not handle so far such diagrams. You should definitely go for TikZ and on this site you will find lot of examples to start with. Jul 13, 2015 at 14:06
  • Read the documentation of the package forest and see here (tex.stackexchange.com/a/254926) for a start.
    – LaRiFaRi
    Jul 13, 2015 at 14:13

2 Answers 2

10
% arara: pdflatex

\documentclass{article}
\usepackage{forest}

\begin{document}    
\begin{forest} for tree={%
        draw=gray, minimum width=1cm, minimum height=.5cm, rounded corners=3,
        text height=1.5ex, text depth=0ex,
        grow=east,reversed,
        edge={gray},
        parent anchor=east,
        child anchor=west,
        if n children=0{tier=last}{}
    }
[A
    [b 
        [l]]
    [c 
        [e[m]][f[n]][g[o]][h[p]]]
    [d
        [j[r]][k[s]]]
]
\end{forest}
\end{document}

enter image description here

5

In case you would like something slightly fancier, here is a variation on LaRiFaRi's answer:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}

\begin{document}
\tikzset{
  my node/.style={
    draw=gray,
    inner color=gray!5,
    outer color=gray!10,
    thick,
    minimum width=1cm,
    rounded corners=3,
    text height=1.5ex,
    text depth=0ex,
    font=\sffamily,
    drop shadow,
  }
}
\begin{forest}
  for tree={%
    my node,
    l sep+=5pt,
    grow'=east,
    edge={gray, thick},
    parent anchor=east,
    child anchor=west,
    if n children=0{tier=last}{},
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label};
    },
    if={isodd(n_children())}{
      for children={
        if={equal(n,(n_children("!u")+1)/2)}{calign with current}{}
      }
    }{}
  }
  [A
    [b
      [l]]
    [c
      [e[m]][f[n]][g[o]][h[p]]]
    [d
      [j[r]][k[s]]]
  ]
\end{forest}
\end{document}

jazzed up slightly

Just for fun, here's a version which shows that forest doesn't use the same alphabet as the rest of us ;)...

\newcounter{mynode}
\setcounter{mynode}{0}
\begin{forest}
  for tree={%
    my node,
    l sep+=5pt,
    grow'=east,
    edge={gray, thick},
    parent anchor=east,
    child anchor=west,
    if n children=0{tier=last}{},
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label};
    },
    if={isodd(n_children())}{
      for children={
        if={equal(n,(n_children("!u")+1)/2)}{calign with current}{}
      }
    }{},
    delay={content={\stepcounter{mynode}\alph{mynode}}}
  },
  [
    [
      []]
    [
      [[]][[]][[]][[]]]
    [
      [[]][[]]]
  ]
\end{forest}

confused alphabet

Or, if you wanted a splash of colour, you could try this code:

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
% xcolor manual: 34
\definecolorseries{colours}{hsb}{grad}[hsb]{.575,1,1}{.987,-.234,0}
\resetcolorseries[12]{colours}

\begin{document}
\tikzset{
  my node/.style={
    draw=gray,
    inner color=gray!5,
    outer color=gray!10,
    thick,
    minimum width=12mm,
    rounded corners=3,
    text height=1.5ex,
    text depth=0ex,
    font=\sffamily,
    drop shadow,
    minimum height=6mm,
  }
}
\forestset{
  my tree/.style={
      my node,
      l sep+=5pt,
      grow'=east,
      edge={gray, thick},
      parent anchor=east,
      child anchor=west,
      edge path={
        \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label};
      },
      if={isodd(n_children())}{
        for children={
          if={equal(n,(n_children("!u")+1)/2)}{calign with current}{}
        }
      }{},
  }
}
\newcounter{mynode}
\setcounter{mynode}{0}
\begin{forest}
  for tree={
    my tree
  },
  before typesetting nodes={
    for tree={
      my tree,
      content={\color{colours!!+}\stepcounter{mynode}\alph{mynode}},
      font=\bfseries\sffamily,
    }
  },
  before packing={
    for tree={
      if n children=0{tier=last}{},
    }
  }
  [
    [[]]
    [, delay={repeat=4{append={[[]]}}}
    ]
    [, delay={repeat=2{append={[[]]}}}
    ]
  ]
\end{forest}
\end{document}

colour

You must log in to answer this question.

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