4

How do I go about drawing arrows from the exposed right most nodes to my aligned values on the right.

\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{forest}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}

\begin{document}
$$
  \begin{array}{lr}
    \begin{forest}
    for tree={
        l sep=2.5em
    }
     [$cn^2$
      [$c\left(\frac{n}{4}\right)^2$
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
      ]
      [$c\left(\frac{n}{4}\right)^2$
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
      ]
      [$c\left(\frac{n}{4}\right)^2$
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
      ]
     ]
    \end{forest} &
    \quad\begin{aligned}
        &cn^2 \\ \\
        &\frac{3}{16}cn^2 \\ \\
        &\left(\frac{3}{16}\right)^2cn^2 \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\
    \end{aligned}
  \end{array}
$$
\end{document}

This makes the following diagram:

enter image description here

But I would like to have something like:

enter image description here

I would also like to have the 3 equations on the right line up with the levels on the tree they correspond to.

Essentially I am trying to recreate diagram d) from my book:

enter image description here

  • Don't use $$ for display maths in LaTeX. – cfr Dec 4 '17 at 23:34
  • Why not use $$ for display math nodes? – Drew Dec 4 '17 at 23:36
  • Most of the time, it will give you worse spacing than \[...\], which was introduced a few decades ago with LaTeX 2e. There are some very unusual cases in which it might do worse, but the vast majority of the time, it will do better. There's a question about it somewhere if you search. (Of course, there's a question about most everything!) – cfr Dec 5 '17 at 0:45
6

like this?

enter image description here

i named the most right forest nodes and right of them positioned equations. then the draw of connection lines is simple:

\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}

\usepackage{forest}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}

\begin{document}
\[
    \begin{forest}
    for tree={
        l sep=2.5em
    }
     [$cn^2$,name=L1
      [$c\left(\frac{n}{4}\right)^2$
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
      ]
      [$c\left(\frac{n}{4}\right)^2$
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
      ]
      [$c\left(\frac{n}{4}\right)^2$, name=L2
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$
        []
        []
        []
       ]
       [$c\left(\frac{n}{16}\right)^2$, name=L3
        []
        []
        []
       ]
      ]
     ]
    \node (a) [right=of L1 -| L3.east] {$cn^2$};
    \node (b) [right=of L2 -| L3.east] {$\frac{3}{16}cn^2$};
    \node (c) [right=of L3.east]       {$\left(\frac{3}{16}\right)^2 cn^2$};
    \draw[dashed,->] (L1) -- (a);
    \draw[dashed,->] (L2) -- (b);
    \draw[dashed,->] (L3) -- (c);
    \end{forest}
\]
\end{document}
  • You can also set a coordinate at current bounding box.east so it doesn't matter if the rightmost node changes (e.g. if the content of a node gets larger or smaller or whatever). – cfr Dec 4 '17 at 23:33
  • @cfr, i try this, but then the equation was not horizontally left aligned (i didn't manage to do this) . – Zarko Dec 4 '17 at 23:49
  • Yes, you need to specify the anchor in this case, as you're using a coordinate for placement. At least, I think that's why ;). – cfr Dec 5 '17 at 0:42
  • 1
    Yeah, that's just a typo on my part it should be 16 not 15 – Drew Dec 5 '17 at 1:27
  • 1
    Yes there should be a c – Drew Dec 5 '17 at 5:08
3

This code defines a level markers style for trees where you wish to mark levels and a level mark style for defining the mark. level mark can be added to any node on the relevant level. The code expects the value to require maths mode. (This would be easy to change if you need text.)

A new nodewalk step, tier end moves to the last node on the current tier. The level markers style sets up tiers to correspond to levels of the tree.

\documentclass[border=10pt]{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\forestset{
  define long step={tier end}{}{
    group={
      while nodewalk valid={next on tier}{next on tier},
      current
    }
  },
  level markers/.style={
    tikz+={
      \coordinate (tree marker) at ([xshift=10pt]current bounding box.east);
    },    
    before typesetting nodes={
      for tree={
        tier/.option=level,
      },
    },
  },
  level mark/.style={
    before drawing tree={
      for tier end={
        tikz+={
          \node (b) [anchor=mid west] at (tree marker |- .mid) {$#1$};
          \draw [densely dashed, -LaTeX] (.east) -- (b.west |- .center);
        },
      },
    },
  }
}
\begin{document}
\begin{forest}
  for tree={
    l sep=2.5em,
    math content,
  },
  level markers,
  [cn^2, level mark=cn^2
    [c\left(\frac{n}{4}\right)^2, level mark=\frac{3}{16}cn^2
      [c\left(\frac{n}{16}\right)^2, level mark=\left(\frac{3}{15}\right)^2n^2
        []
        []
        []
      ]
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
    ]
    [c\left(\frac{n}{4}\right)^2
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
    ]
    [c\left(\frac{n}{4}\right)^2
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
      [c\left(\frac{n}{16}\right)^2
        []
        []
        []
      ]
    ]
  ]
\end{forest} 
\end{document}

resulting tree

EDIT [In light of corrected level markers]

This code shows how to draw the tree automatically. It defines a style, auto level markers which takes 3 arguments: the number of children each parent has / numberator, the denominator and the number of levels to be filled and marked. For example, asking for 3 levels will yield a 4-level tree, where the fourth level consists of nodes without content and no level marker.

\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\forestset{
  define long step={tier end}{}{
    group={
      while nodewalk valid={next on tier}{next on tier},
      current
    }
  },
  level markers/.style={
    tikz+={
      \coordinate (tree marker) at ([xshift=10pt]current bounding box.east);
    },    
    before typesetting nodes={
      for tree={
        tier/.option=level,
      },
    },
  },
  level mark/.style={
    before drawing tree={
      for tier end={
        tikz+={
          \node (b) [anchor=mid west] at (tree marker |- .mid) {$#1$};
          \draw [densely dashed, -LaTeX] (.east) -- (b.west |- .center);
        },
      },
    },
  },
  declare count register={auto numerator},
  declare count register={auto denominator},
  declare count register={auto levels},
  declare count register={auto denominator squared},
  Autoforward register={auto denominator}{
    auto denominator squared=#1*#1,
  },
  auto numerator'=3,
  auto denominator'=4,
  auto levels'=3,
  auto level markers/.style n args=3{
    level markers,
    auto numerator=#1,
    auto denominator=#2,
    auto levels=#3,
    delay={
      tempcounta'=0,
      while={
        > RR< {tempcounta}{auto levels}
      }{
        where n children=0{
          repeat={
            >R{auto numerator}
          }{
            append={[]}
          }
        }{},
        do dynamics,
        tempcounta'+=1,
      }
    },
    before typesetting nodes={
      where={
        > OR<  {level} {auto levels}
      }{
        if level=0{
          content=cn^2,
          math content
        }{
          content/.process={
            OR w2+P w {level} {auto denominator} {int(##2^##1)} { c\left(\frac{n}{##1}\right)^2 }
          },
          math content
        }
      }{},
      for nodewalk={
        r,
        while={
          > ORw+n< {level} {auto levels} {##1-1}
        }{l}
      }{
        if level>=2{
          level mark/.process={ O R R w3 {level} {auto numerator} {auto denominator squared} {\left(\frac{##2}{##3}\right)^{##1}cn^2} }
        }{
          if level=1{
            level mark/.process={ R R w2 {auto numerator} {auto denominator squared} {\frac{##1}{##2}cn^2} }
          }{
            level mark=cn^2
          }
        }
      },
    },
    before packing={
      for tree={
        l sep'=2.5em
      },
    },
  },
}
\begin{document}
\begin{forest}
  auto level markers={3}{4}{3},
  []
\end{forest}
\begin{forest}
  auto level markers={2}{6}{4},
  []
\end{forest}
\end{document}

The first tree is the corrected version of that drawn above:

automated & corrected version

The second randomly varies the values passed to the style, which may or may not make sense, given whatever the tree represents:

variation

  • Yeah, the missing c and 15 are typos, I'll fix EM. But, I am curious, how would I automate the structure of the tree? Since I am doing recurrences I will be doing this frequently. – Drew Dec 5 '17 at 1:29
  • @Drew See edit above. I guess you could simplify it further, by making a macro, but \begin{forest}auto level markers={}{}{}[]\end{forest} is the Forest bit. You can use \newcommand to create a convenience wrapper, if you want. – cfr Dec 5 '17 at 3:24
  • @Drew Basically, if there's a rule or pattern, Forest can apply the rule or pattern for you: whether it is the structure of the tree, the content of the nodes or additional annotation. This is handy if you need to do a lot of variations-on-a-theme :-). – cfr Dec 5 '17 at 3:25

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.