4

I know it's a bit tedious, but could someone please write me the latex code for below tree?

Here's how I started

\documentclass{article}
\usepackage{amsmath}
\usepackage{forest}
\tikzset{
  dot/.style={circle,draw,inner sep=1.2,fill=black},
  }
 \begin{document}
 \begin{forest}
 [ ,name=a,for tree={s sep=60pt,l sep=2cm,dot,grow=0},
   [ ,name=b,edge 
      [ ,name=c, ]
      [,name=d,]    
   ]
  [ ,name=e, edge
     [ ,name=f,]
     [,name=g, ]
     [ ,name=h,]
  ]
]
\end{forest}
\end{document}

I've tried several times to build up on this to get the tree I wanted but I can't get the branches right. Thank you!enter image description here

  • 1
    Please show us what you have for now. Typing the whole thing for you would be tedious, indeed. At least, we will need an MWE showing your setup. – LaRiFaRi Jun 16 '15 at 8:41
  • See Tikz/Forest: highlighting level background. You can copy that one and modify it according to your needs. – Alenanno Jun 16 '15 at 9:29
5

Indeed the forest branches are a bit tricky to get right, I suggest the next time you try building the lower part first, then copy that and paste it to make the upper (applying the changes to fit your desired output).

Since you use a node for the dots, you cannot enter the text as you would normally. If you do this is the result:

figure 1

So to work around this, you can use the label command, which works like this (\cdots is the command for the dots):

[ ,name=g, label={right:$\cdots$}] 

So the label is placed at the right, since this forest grows east.

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{amsmath}
\usepackage{forest}
\tikzset{
  dot/.style={circle,draw,inner sep=1.2,fill=black},
  }
 \begin{document}
 \begin{forest}
    for tree={s sep=60pt,l sep=2cm,dot,grow=0}
%
[ ,name=root
    [ ,name=below
    [ ,name=a
        [ ,name=c, label={right:$\cdots$}] 
        [ ,name=d, label={right:$\cdots$}] 
        [ ,name=e, label={right:$\cdots$}] 
    ]
    [ ,name=f
        [ ,name=g, label={right:$\cdots$}] 
        [ ,name=h, label={right:$\cdots$}] 
    ] ]
    [ ,name=above
    [ ,name=i
        [ ,name=k, label={right:$\cdots$}] 
        [ ,name=l, label={right:$\cdots$}] 
    ] 
    [ ,name=m
        [ ,name=n, label={right:$\cdots$}] 
        [ ,name=o, label={right:$\cdots$}] 
        [ ,name=p, label={right:$\cdots$}] 
    ] ]
]
\end{forest}
\end{document}
  • Is it possible to make it smaller? Thanks! – Selena Jun 16 '15 at 11:33
  • 1
    @Selena Sure. Wrap it inside of a scale box. Like \scalebox{.5}{<forest>} where .5 will make it half the size. Choose a value between 0 and 1. – Alenanno Jun 16 '15 at 11:58
  • 1
    @Selena, you can also make it smaller by decreasing the s sep and/or l sep values. – Jason Zentz Jun 16 '15 at 12:49
2

If you prefer less typing ;), you can have forest automatically label the final nodes and automatically name all nodes. To make it easier to figure out which name matches which node, you can temporarily label them using a TikZ foreach loop.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest}
\tikzset{
  dot/.style={circle,draw,inner sep=1.2,fill=black},
}
\begin{document}
  \newcounter{mynode}
  \setcounter{mynode}{0}
  \renewcommand*\themynode{\alph{mynode}}
  \begin{forest}
    for tree={
      if n children=0{label=right:$\dots$}{},
      grow=0,
      dot,
      fit=band,
      TeX={\stepcounter{mynode}},
      name/.expanded={\themynode},
      s sep+=5pt
    }
    [
      [
        [
          [ ]
          [ ]
          [ ]
        ]
        [
            [ ]
            [ ]
        ]
      ]
      [
        [
            [ ]
            [ ]
        ]
        [
          [ ]
          [ ]
          [ ]
        ]
      ]
    ]
    \foreach \i in {a,...,q} {\node [text=red, text opacity=.25, font=\tiny, yshift=-5pt] at (\i) {\i};};
  \end{forest}
\end{document}

auto-tree

Once you've finalised things, you can remove or comment out the labelling line.

de-labelled tree

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.