3

I would like to extend the three tier in the answer here to a four-tier tree. To something as shown in the figure below. This is the MWE provided here. The question appears quite simple, but I have tried almost all I could before posting this question

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\begin{document}
\begin{forest}
for tree={
draw,
grow'=0,
},
forked edges,
where n children=0{}{rotate=90},
[Root
[Branch A
[Branch A1
]
[Branch A2
]
]
[Branch B
[Branch B1
]
[Branch B2
]
]
]
\end{forest}
\end{document}
3

like this?

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}

\begin{document}
\begin{forest}
    for tree={
        grow'=east,
        draw,
        anchor=west,
    forked edges,
    where n children=2{rotate=90, anchor=center}{}, % <---
    }
    [Root
        [Branch A
            [Branch A1
                [Sub-branch a1]
            ]
            [Branch A2
                [Sub-branch a2]
            ]
        ]
        [Branch B
            [Branch B1
                [Sub-branch b1]
            ]
            [Branch B2
                [Sub-branch b2]
            ]
        ]
    ]
\end{forest}
\end{document}

in code is (in comparison to yours) where n children=0{}{rotate=90} replaced with where n children=2{rotate=90, anchor=center}{},

  • Yes! You are absolutely Right! – Abdulhameed Jun 3 '18 at 10:04
3

The reason my original answer rotates all but the last nodes is because it uses the

where n children=0{}{}

conditional to decide whether to rotate or not. Basically, if a node has children, it gets rotated; if it has no children (is a leaf), it doesn't.

There are several ways to alter this for you tree. One is to rotate nodes with 2 or more children, as Zarko suggested.

Another is to rotate nodes in the first two levels (levels 0 and 1).

where level<=1{}{}

A third is to create a style which you can apply to just those nodes you want rotated.

, <style name> % add to each node you want to apply <style name> to

In my example, rot is defined to do the rotation, so

, rot

is added to each of the three to-be-rotated nodes. This is the simplest solution if you want to be able to extend the tree and have fine-grained control over what gets rotated.

Here is code illustrating each of these two alternative approaches:

\documentclass[border=10pt]{standalone}
\usepackage[edges]{forest}
\forestset{
  rot/.style={rotate=90},
}
\begin{document}
\begin{forest}
  for tree={
    draw,
    grow'=0,
  },
  forked edges,
  where level<=1{rot}{},
  [Root
  [Branch A
    [Branch A1 [Sub-branch A11]
    ]
    [Branch A2 [Sub-branch A21]
    ]
  ]
  [Branch B
    [Branch B1 [Sub-branch B11]
    ]
    [Branch B2 [Sub-branch B21]
    ]
  ]
  ]
\end{forest}
\begin{forest}
  for tree={
    draw,
    grow'=0,
  },
  forked edges,
  [Root, rot
  [Branch A, rot
    [Branch A1 [Sub-branch A11]
    ]
    [Branch A2 [Sub-branch A21]
    ]
  ]
  [Branch B, rot
    [Branch B1 [Sub-branch B11]
    ]
    [Branch B2 [Sub-branch B21]
    ]
  ]
  ]
\end{forest}
\end{document}

The output is the same in each case:

identical trees

  • nice explanation (which is missing in my answer) +1! – Zarko Jun 3 '18 at 22:16
  • @Zarko Your answer does the job perfectly well. (At least, it got my vote.) I just thought this might be useful lest we get a new question if the OP need to add another node :-). Give an OP a fish vs. teach an OP to fish ... ? – cfr Jun 3 '18 at 22:29
  • @cfr the OP keeps learning to fish infact he has an edit giving more details on one of the answers i.e extending heterogeneous number of sub-branches which was meant to undergo review . However, the OP really appreciate your answers and explanation – Abdulhameed Jun 3 '18 at 22:32
  • @Abdulhameed ;) Forest takes getting used to. At least, so I found. I only 'got' it by pestering somebody here for help/explanations. – cfr Jun 3 '18 at 22:35
  • Thanks for your answers! Hoping I will get more used to it. I had to create a new question as it took me much time trying to extend it but couldn't get through! – Abdulhameed Jun 3 '18 at 22:37

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.