1

In the following example, I'm trying to "skip" a level in my TikZ tree. On other threads I saw the recommendation to add , tier=text to relevant nodes to assign them manually to another tier. This does not do the job for me, this only changes the alignment - I want to achieve the following. The second option, an empty node (see MWE below) leaves an annoying white space behind.

If the line between 'Tier 1' and 'Tier 3.1' is continuous, I'm happy. But what I'm basically trying to achieve, is to display more 'Tier 2' nodes underneath eachother, because it will not fit on the page otherwise (if I list them out horizontally on Tier 2)

Forest tree

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[edges]{forest}

\forestset{
  direction switch/.style={
    for tree={edge+=thick, font=\sffamily},
    where level>=0{}{draw},
    where level>=2{folder, grow'=0}{for children=forked edge},
  },
}

\begin{document}

{\footnotesize
\begin{forest}
  direction switch
  [Tier 0
  [Tier 1,
    [Tier 2.1
        [Tier 3]
    ]
    [Tier 2.2]
    [Tier 2.3]
    [%the empty node
    [Tier 3.1]
    [Tier 3.2
        [Tier 4]
    ]
    [Tier 3.3
    ]
    ]
    ]
    ]
  ]
  ]
\end{forest}
}

\end{document}
1
  • I also tried: changing the empty node to a phantom node. This results in all lines to the nodes disappearing nested under the empty node. – Nino Apr 18 '20 at 19:50
2

Literally skipping the empty node does not help here, as that node must be assigned the folder style. What does help is to change its shape to coordinate and set folder indent to 0pt. Simplifying a bit: setting coordinate removes the vertical empty space, and setting folder indent=0 aligns the vertical parts of the edges to and from the empty node.

I have changed the direction switch style to do this automatically for empty nodes. Remarks:

  1. The execution of where level must be delayed to correctly identify the empty nodes. Without the delay, all the nodes would appear empty.

  2. folder indent is a register used by the folder style. If we simply set it to 0, it would affect the subsequent calls of folder. We therefore need to "save and restore" it; this would be achieved "manually", via assignment to a temporary register, but Forest actually provides a dedicated key save and restore register.

  3. l sep of the empty node needs to be reduced by the value of the (original) folder indent, otherwise the horizontal part of the edge would be too long for the children of the empty node.

The code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[edges]{forest}

\forestset{
  direction switch/.style={
    for tree={edge+=thick, font=\sffamily},
    delay={
      where level>=2{
        grow'=0,
        if content={}{
          coordinate,
          save and restore register={folder indent}{
            folder indent=0,
            folder,
          },
          l sep-/.register=folder indent,
        }{folder},
      }{
        for children=forked edge
      },
    },
  },
}

\begin{document}

{\footnotesize
\begin{forest}
  direction switch
  [Tier 0
    [Tier 1,
      [Tier 2.1
        [Tier 3]
      ]
      [Tier 2.2]
      [Tier 2.3]
      [
        [Tier 3.1]
        [Tier 3.2
          [Tier 4]
        ]
        [Tier 3.3
        ]
      ]
    ]
  ]
\end{forest}
}

\end{document}

https://imgur.com/CzW4ple.png

2
  • Thank you for solving this and the explanation. Think I understand the concept of the register better now. I wonder how you knew about this coordinate shape, I struggle to find that in the forest documentation. – Nino Apr 19 '20 at 10:26
  • 1
    Part of the problem with Forest is that you need to read the PGF/TikZ documentation too! ;-) Forest nodes are realized as TikZ nodes, so chapter 17 of that documentation is your friend. – Sašo Živanović Apr 19 '20 at 15:05

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.