5

How do one rotate text inside a diagram as shown below. Below is my MWE modified from here

\documentclass[tikz,12pt]{standalone}
\usetikzlibrary{calc,positioning,backgrounds,arrows.meta}
\usepackage{forest}
\pagestyle{empty}

\begin{document}

\begin{forest}
  for tree={
  child anchor=west,
parent anchor=east,
grow'=east,
draw,
anchor=west,
edge path={
  \noexpand\path[\forestoption{edge}]
    (.child anchor) -| +(-5pt,0) -- +(-5pt,0) |-
    (!u.parent anchor)\forestoption{edge label};
},
 }
 [Root
 [Branch A
  [Branch A1
  ]
  [Branch A2
  ]
]
[Branch B
  [Branch B1
  ]
  [Branch B2
  ]
 ]
]
\end{forest}

\end{document}

enter image description here

Objective: Rotate as shown in the figure below

enter image description here

1
  • I'd recommend using the edges library unless you are stuck and can't update. Makes these kinds of edges much simpler.
    – cfr
    Jun 3, 2018 at 1:02

2 Answers 2

5

The simplest solution is to use the edges library. The rotate option for nodes then works automatically - there is no need to change or specify any anchors at all.

\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}

rotated and non-rotated nodes

If you do need to specify anchors (node, parent or child), try to avoid absolute anchors (e.g. west or north) and instead use relative anchors (e.g. parent or child) because these will be adjusted correctly if a particular node is rotated. That is, if you use these anchors for the tree, you can then rotate your nodes without worrying about them. Similarly, if the direction of growth changes, relative anchors adjust automatically. So code done this way is a lot more flexible than code which depends on absolute anchors.

0
3

You can specify options to each node by appending them after a comma. In order to rotate a node you can use the rotate option. However, you also need to change the child anchor and parent anchor (where the edges connect) because they are rotated as well and you probably want anchor=center for vertical alignment. To simplify things I have combined those in a new style ver.

Also, I have simplified your -| +(-5pt,0) -- +(-5pt,0) to -- +(-10pt,0).

\documentclass[12pt]{standalone}
\usepackage{forest}

\begin{document}

\begin{forest}
    for tree={
        child anchor=west,
        parent anchor=east,
        grow'=east,
        draw,
        anchor=west,
        edge path={
            \noexpand \path[\forestoption{edge}]
            (.child anchor) 
            -- +(-10pt,0) 
            |- (!u.parent anchor)
            \forestoption{edge label};
        },
        ver/.style={rotate=90, child anchor=north, parent anchor=south, anchor=center},
    }
    [Root
        [Branch A, ver
            [Branch A1
            ]
            [Branch A2
            ]
        ]
        [Branch B, ver
            [Branch B1
            ]
            [Branch B2
            ]
        ]
    ]
\end{forest}

\end{document}

enter image description here

2
  • 1
    You don't really need to change the anchors if you specify them correctly in current Forest.
    – cfr
    Jun 3, 2018 at 1:03
  • That is, this is a good answer for version 1 of Forest, but not the best approach for version 2+. Also the page style and the TikZ libraries are all unnecessary here. (I know these are in the OP's code, but they aren't doing anything.)
    – cfr
    Jun 3, 2018 at 1:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .