2

Is it possible to add a loop to a tree in the forest package? Specifically, how can I typeset the diagram on the right? (Ignore the colors, just usual black on white.)

enter image description here

The code for the left one (without the red X) is:

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

\usepackage{mathtools}
\begin{document}
\begin{forest}
for tree={grow=north}
[$\oplus$
    [$\otimes$
        []
        []
    ]
    [$\otimes$
        []
        []
    ]
]
\end{forest}
\end{document}
2
  • Can you please post complete code people can compile? It makes it much easier to help - no need to guess how to complete examples - and more likely it will help - no danger of completing in an incompatible way.
    – cfr
    Mar 22 '17 at 1:14
  • The problem is that the diagram on the right is not a tree. In a tree, a child has one parent - never two.
    – cfr
    Mar 22 '17 at 1:15
1

The diagram on the right is not a tree since a child has more than one parent. Hence, it cannot be drawn in Forest out-of-the-box. However, you can have Forest draw the loop by manipulating edge path, using tikz or adding TikZ commands directly inside the forest environment.

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

\usepackage{mathtools}
\begin{document}
\begin{forest}
  for tree={
    grow=north,
  },
  where n children=0{tier=terminus}{},
  [$\oplus$
      [$\otimes$
          []
          [, phantom]
      ]
      [, edge path'={(!r1.parent anchor) -- (.child anchor) (!rl.parent anchor) -- (.child anchor)}, child anchor=south]
      [$\otimes$
          [, phantom]
          []
      ]
  ]
\end{forest}
\end{document}

non-tree hack

If you only need the odd non-tree bit, this should be fine. If, however, you need many loops, you will want to use support for generic graphs, avoiding the packages and libraries which focus on trees. Consider the TikZ graphs provision in that case.

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.