8

I generated this image in Inkscape because I don't know enough TikZ to write a description of it in that language, but given the amount of examples of TikZ trees available online, it strikes me as comparatively easy. List of trees with probabilities

I know I can generate eg. the 8th of these trees like this:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
    node/.style={circle, fill, minimum size=1em,inner sep=2pt}]
  \node[node] {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}}
  child { node[node] {} 
    child { node[node] {} 
      child { node[node] {} }}};
\end{tikzpicture}
\end{document}

which looks like this

Tree number 8 (output of TikZ standalone above)

How do I generate the entire row, with nice space between them and fractions under them, in a size of \textwidth×5em of a beamer frame? In another question I found mention of the \tikz command and tried the following picture

\begin{tikzpicture}[
    scale=0.2,
    node/.style={circle, fill, minimum size=0.15em,inner sep=1pt}]
  \node (n1) {
    \tikz[scale=0.2]{\node[node] {}
      child { node[node] {}
        child { node[node] {} 
          child { node[node] {} }}};
    }};
  \node[below=1pt of n1] {$\frac{8}{27}$};
  \node[right=1pt of n1] (n2) {
    \tikz[scale=0.2]{\node[node] {}
      child { node[node] {}
        child { node[node] {} 
          child { node[node] {} }
          child { node[node] {} }}};
    }};
  \node[below=1pt of n2] {$\frac{4}{27}$};
  \node[right=1pt of n2] (n8) {
    \tikz[scale=0.2]{\node[node] {}
      child { node[node] {}
        child { node[node] {} 
          child { node[node] {} }
          child { node[node] {} }}}
      child { node[node] {} 
        child { node[node] {} 
          child { node[node] {} }}};
    }};
  \node[below=1pt of n8] {$\frac{8}{243}$};
\end{tikzpicture}

but with that the trees lean to the left: enter image description here

  • \tikz is a shortform for the tikzpicture environment, and nesting tikzpictures doesn't always work that well. I think options from the parent node is passed to the nested \tikz, which causes the skew. – Torbjørn T. Jul 16 '18 at 18:23
  • I completely rewrote my answer by the way, it's bit better now. – Torbjørn T. Jul 16 '18 at 19:01
6

They're trees and there are potentially many of them. Many trees = a forest or, in this case, several forests.

I present a more automated version than the more automated version provided by Torbjørn T. My version uses Forest which makes for very compact trees and very concise tree specifications.

Besides my preference for Forest, the trees are very similar to the problem of drawing 'B series' trees, which have been asked about several times and for which a considerable number of solutions already exist. We need to turn them right way up and to add some code for the fractional labels, but, otherwise, the code is already written.

I amended code from this earlier answer, the result of which is that

\Bseries{8}{27} [[[[]]]]
\Bseries{4}{27} [[[[][]]]]
\Bseries{8}{81} [[[[]][[]]]]
\Bseries{4}{81} [[[[][]][[]]]]
\Bseries{4}{81} [[[[]][[][]]]]
\Bseries{2}{81} [[[[][]][[][]]]]
\Bseries{8}{243} [[[[]]][[[][]]]]
\Bseries{8}{243} [[[[][]]][[[]]]]
\Bseries{4}{243} [[[[][]]][[[][]]]]
$\cdots$
\Bseries{1}{2187} [[[[][]][[][]]][[[][]][[][]]]]

produces

series of upside-down B series trees with fractional labels

That is, \Bseries{<numerator>}{<denominator>} <tree specification> creates a tree of the desired kind, labelled with the applicable fraction, where the <tree specification> is to be given using Forest's bracket syntax.

The nice thing about this, of course, is that the specifications for the trees are relatively concise and do not require the expenditure of excess effort typing :).

Complete code:

% ateb: https://tex.stackexchange.com/a/280059/
% gweler hefyd: forest-bseries.tex forest-bseries2.tex forest-bseries3.tex forest2-1-bseries4.tex
\documentclass[border=10pt]{standalone}
\usepackage{forest,xparse}
\forestset{
  ./.style={
    delay+={append={[]},}
  },
  Bseries down/.style n args=2{
    for tree={
      parent anchor=center,
      child anchor=center,
      s sep'=5pt,
    },
    tikz+={
      \node [anchor=north] at (current bounding box.south -| !r) {$\frac{#1}{#2}$};
    },
    before typesetting nodes={
      for tree={
        circle,
        fill,
        minimum width=3pt,
        inner sep=0pt,
      },
      tempcounta/.max={>O{level}}{tree},
      if={>Rw+P{tempcounta}{isodd(##1)}}{tempcounta'+=1}{},
      for nodewalk={fake=root,until={>ORw+n={level}{tempcounta}{##1/2}}{1}}{baseline}
    },
    before computing xy={
      for tree={
        l'=15pt,
      }
    }
  }
}
\DeclareDocumentCommand\Bseries{mmo}{\Forest{Bseries down={#1}{#2} [#3]}}
\begin{document}

\Bseries{8}{27} [[[[]]]]
\Bseries{4}{27} [[[[][]]]]
\Bseries{8}{81} [[[[]][[]]]]
\Bseries{4}{81} [[[[][]][[]]]]
\Bseries{4}{81} [[[[]][[][]]]]
\Bseries{2}{81} [[[[][]][[][]]]]
\Bseries{8}{243} [[[[]]][[[][]]]]
\Bseries{8}{243} [[[[][]]][[[]]]]
\Bseries{4}{243} [[[[][]]][[[][]]]]
$\cdots$
\Bseries{1}{2187} [[[[][]][[][]]][[[][]][[][]]]]


\end{document}
  • If you wish smaller trees, just adjust the values for l'=15pt, s sep'=5pt and/or minimum width=3pt to whatever values you prefer. I made these a bit bigger than my original style, just for purposes of illustration. But a Beamer slide may well require reversion to the smaller values (which had l'=5pt and s sep'=2.5pt). – cfr Jul 17 '18 at 0:54
  • Your 8/243 examples are wrong; they match the 8/81 exmples, instead of being split at the top. – KRyan Jul 17 '18 at 3:56
  • 1
    @KRyan Yes, but you can easily fix that: \Bseries{8}{243} [[[[]]][[[][]]]] and \Bseries{8}{243} [[[[][]]][[[]]]]. – user121799 Jul 17 '18 at 6:22
  • @KRyan As marmot's comment suggests, that's just a typo on my part. Remember that people are trying to reproduce graphical output without any idea, usually, what it means. So I'm sure it is obvious to you it's wrong, but not to me. I thought I'd been very careful - and I was - but there were a lot of trees to copy! Anyway, you can easily fix that yourself. – cfr Jul 17 '18 at 17:17
  • ...I know? I wasn’t criticizing you or your answer, I was just pointing out a minor error that should be fixed. It doesn’t add anything to the answer to maintain a typo. But I didn’t know how to fix it, and it seemed perhaps not my place to do so? – KRyan Jul 17 '18 at 17:20
5

You could do brute force positioning.

\documentclass[border=10pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{frame}[t]
\frametitle{Some diagrams}
\begin{tikzpicture}[sibling distance=3mm,level distance=3mm,
    node/.style={circle, fill, minimum size=0.3em,scale=0.3}]
  \node[node] (1) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }}};
  \node[below=of 1]   {$\frac{8}{27}$};
  \node[node,right=4mm of 1] (2) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}};
  \node[below=of 2]   {$\frac{4}{27}$};   
  \node[node,right=4mm of 2] (3) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }}
    child { node[node] {} 
      child { node[node] {} }}};
  \node[below=of 3]   {$\frac{8}{81}$};   
  \node[node,right=6mm of 3] (4) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}
    child { node[node] {} 
      child { node[node] {} }}};
  \node[below=of 4]   {$\frac{4}{81}$};   
  \node[node,right=5mm of 4] (5) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }}
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}};
  \node[below=of 5]   {$\frac{4}{81}$};   
  \node[node,right=8mm of 5] (6) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}};
  \node[below=of 6]   {$\frac{2}{81}$};   
  \node[node,right=8mm of 6] (7) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }}}
  child { node[node] {} 
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}};
  \node[below=of 7]   {$\frac{8}{243}$};      
  \node[node,right=8mm of 7] (8) {}
  child { node[node] {}
    child { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}}
  child { node[node] {} 
    child { node[node] {} 
      child { node[node] {} }}};
  \node[below=of 8]   {$\frac{8}{243}$};      
  \node[node,right=8mm of 8,sibling distance=5mm] (9) {}
  child[sibling distance=5mm] { node[node] {}
    child[sibling distance=3mm] { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}}
  child[sibling distance=5mm] { node[node] {} 
    child[sibling distance=3mm] { node[node] {} 
      child { node[node] {} }
      child { node[node] {} }}};
  \node[below=of 9]   {$\frac{4}{243}$};    
  \node[node,right=3cm of 9] (10) {}
  child[sibling distance=10mm] { node[node] {}
    child[sibling distance=5mm] { node[node] {} 
      child[sibling distance=3mm] { node[node] {} }
      child[sibling distance=3mm] { node[node] {} }}
    child[sibling distance=5mm] { node[node] {} 
      child[sibling distance=3mm] { node[node] {} }
      child[sibling distance=3mm] { node[node] {} }}}
  child[sibling distance=10mm] { node[node] {} 
    child[sibling distance=5mm] { node[node] {} 
      child[sibling distance=3mm] { node[node] {} }
      child[sibling distance=3mm] { node[node] {} }}
    child[sibling distance=5mm] { node[node] {} 
      child[sibling distance=3mm] { node[node] {} }
      child[sibling distance=3mm] { node[node] {} }}};
  \node[below=of 10]      {$\frac{1}{2817}$};
  \path([yshift=-5mm]9.east) -- ([yshift=-5mm]10.west)
  node[pos=0.3,node]{} node[pos=0.45,node]{} node[pos=0.6,node]{};    
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

5

Here is a slightly more automated version. Each tree is placed in a separate scope, using the \scoped macro. In that I set the name of a local bounding box, which will encompass the entire tree. I can then add a new node below that local bounding box.

The amount of shifting is done automatically, by issuing the \updatewidth macro. What this does is to calculate the total width of the diagram, with the help of a macro from the tcolorbox package, and add 5pt to that (for some extra padding). That width is saved in a length \diagramwidth, and xshift=\diagramwidth is passed to the scope.

I defined a new style and a macro to make it a bit more convenient to use.

\documentclass{beamer}
\usepackage{tcolorbox}
\tcbuselibrary{skins} % for \tcbsetmacrotowidthofnode
\tikzset{
    node/.style={
          circle,
          fill,
          minimum size=3pt, % reduced this
          inner sep=0pt, % set to zero
    },
    scopestyle/.style={
       every node/.style={node},
       local bounding box=a, 
       xshift=\diagramwidth
    }
}

\newlength\diagramwidth
\newcommand\updatewidth{%
  % set \diagramwidth to width of bounding box
  \tcbsetmacrotowidthofnode\diagramwidth{current bounding box} 
  % add another 5pt for spacing -- adjust that value as you prefer
  \pgfmathsetlengthmacro{\diagramwidth}{\diagramwidth+5pt}
}
\begin{document}
\begin{frame}
\begin{tikzpicture}[
  level distance=15pt, % reduce the level and sibling distance
  sibling distance=10pt
]

\scoped[scopestyle]
  \node {}
  child { node {}
    child { node {} 
      child { node {} }
      child { node {} }}} 
  child { node {} 
    child { node {} 
      child { node {} }}};

% add node below the local bounding box node
\node [below] at (a.south) {$\frac{8}{27}$};

% issue \updatewidth after each tree
\updatewidth


\scoped[scopestyle]
  \node {}
  child { node {}
    child { node {} 
      child { node {} }
      child { node {} }}};

\node [below] at (a.south) {$\frac{8}{27}$};

\updatewidth

\scoped[scopestyle]
  \node (top) {}
  child { node {}
    child { node {} 
      child { node {} }
      child { node {} }}};

\node [below] at (a.south) {$\frac{8}{27}$};

\end{tikzpicture}
\end{frame}


\end{document}

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.