2

What do I need to change in the following code, in order to bring A at the same level as B?

I need the branches where I have placed them, as I have space problem, and don't want them overlapping, but also cannot expand horizontally either. So practically I want the edge connecting A to its "children" to be elongated and A brought higher.

Any ideas? Thank you in advance!

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{forest}
  for tree={
    align=center,
    parent anchor=south,
    child anchor=north,
    font=\sffamily,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-4pt) -| (.child anchor);}}
   [Head
      [A, for tree={before drawing tree={y-=2.6cm}}
        [1[i]]
        [2[i]]
        [3[i]]
        [4[i]]
        [5[i]]
        [6[i]]]
     [B, for tree={before drawing tree={x-=1cm}}
        [1[i]]
        [2[i]]
        [3]]]
\end{forest}
\end{document}

enter image description here

2

Solution that includes a minor relocation of your branches and a new, hidden node:

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{forest}
  for tree={
    align=center,
    parent anchor=south,
    child anchor=north,
    font=\sffamily,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-4pt) -| (.child anchor);}}
   [Head
      [A
        [{},l*=3,[1[i]]
        [2[i]]
        [3[i]]
        [4[i]]
        [5[i]]
        [6[i]]]]
     [B,
        [1[i]]
        [2[i]]
        [3]]]
\end{forest}

\begin{forest}
  for tree={
    align=center,
    parent anchor=south,
    child anchor=north,
    font=\sffamily,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
    edge path={
      \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(0,-4pt) -| (.child anchor);}}
   [Head
      [A, for tree={before drawing tree={y-=2.6cm}}
        [1[i]]
        [2[i]]
        [3[i]]
        [4[i]]
        [5[i]]
        [6[i]]]
     [B, for tree={before drawing tree={x-=1cm}}
        [1[i]]
        [2[i]]
        [3]]]
\end{forest}
\end{document}

enter image description here


Disclaimer: I'm no forest pro ;)

  • You don't need to change the edge and then change it back in the first solution. (See my example below, although I use a simpler strategy for alignment.) The second solution doesn't put A and B level. – cfr Sep 8 '16 at 21:27
  • If you mean the for tree={edge={thick,-}}: I tried to remove the arrow before the A with that command. – Stefan Braun Sep 8 '16 at 21:33
  • You don't need to remove it, I don't think. At least, I didn't. – cfr Sep 8 '16 at 22:02
  • That is, just ... [A [{},l*=3,[1[i]] [2[i]] ... should work fine. – cfr Sep 8 '16 at 22:06
  • I hope you don't mind. I edited that part so that I could vote this up. If you object, you can roll back, of course. – cfr Sep 8 '16 at 22:14
1

The easiest way to align nodes on a level is to use tier=<name of tier> for the relevant nodes.

I started by removing the manual adjustment for A and B and adding tier=this.

step 1

Now, obviously, this has expanded the tree horizontally, which we don't want. There are various ways of avoiding this. One of the easiest is to use an empty node to help with the alignment.

If we then specify a tier for one of B's terminal nodes e.g. tier=that, then we'd like this to end up above any of A's children. Adding an empty child to A and making its existing children into children of the new child achieves this fairly easily.

step 2

This strategy will adjust automatically in most cases if nodes are multi-line etc. (Manual adjustment might be needed in some cases.) This is because the adjustment in the A branch is a function of the content and shape of the B branch.

For this reason, I prefer this method to something like increasing l or l sep because it is less work if I change something later and requires less trial-and-error.

The code below is also simplified a bit as version 2 of Forest offers several goodies which can do some of your customisation for you. In particular, I use the edges library for the squared-off edges and the parent and children anchors for flexibility. However, if you've only got version 1 and cannot update, you can use the tier= strategy just fine with your original code.

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{forest}
  for tree={
    align=center,
    parent anchor=children,
    child anchor=parent,
    font=\sffamily,
    edge={thick, -{Stealth[]}},
    l sep+=10pt,
  },
  forked edges,
 [Head
   [A, tier=this
      [, tier=that
        [1[i]]
        [2[i]]
        [3[i]]
        [4[i]]
        [5[i]]
        [6[i]]
      ]
   ]
   [B, tier=this
      [1[i, tier=that]]
      [2[i]]
      [3]
   ]
 ]
\end{forest}
\end{document}
1

A simple l sep+ on node A does the trick, actually, but the edge path needs to be defined differently (with a longer part at the parent's side).

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{forest}
  for tree={
    align=center,
    parent anchor=south,
    child anchor=north,
    font=\sffamily,
    edge={thick, {Stealth[]}-},
    l sep+=10pt,
    edge path={\noexpand\path [draw, \forestoption{edge}] (.child anchor) -- +(0,15pt) -| (!u.parent anchor);},
  },
   [Head
      [A, l sep+=4cm,
        [1[i]]
        [2[i]]
        [3[i]]
        [4[i]]
        [5[i]]
        [6[i]]]
     [B
        [1[i]]
        [2[i]]
        [3]]]
\end{forest}
\end{document}
  • 1
    But if B's nodes are multi-line (as they maybe with align=center then you have to change it. Of course, that might be needed using tiers, but there's some change it might not! – cfr Sep 9 '16 at 0:45

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.