4

I am trying to build a horizontal tree using package forest, though I don't manage to get the nodes well aligned. Here is an example and MWE:

\documentclass{article}
\usepackage{forest}
\begin{document}
    \begin{forest}
        for tree={
        calign=center,
        grow'=east, % tree direction
        parent anchor=east, child anchor=west, % edge anchors
        rounded corners, draw,
        }
        [A [B [C [D [E[F]]] [D' [E' [F']]] ]]]
    \end{forest}
    \begin{forest}
        for tree={
        calign=center,
        grow'=east, % tree direction
        parent anchor=east, child anchor=west, % edge anchors
        rounded corners, draw, 
        }
        [g [B [g [D [E[F]]] [D' [E' [F']]] ]]]
    \end{forest}
\end{document}

mwe

What I would like to get is something like in the first tree, while as you can see from the second one, the text in the nodes influences their alignment.

4

Set the text height and text depth explicitly such that all nodes have the same height and baseline no matter what the letter looks like.

enter image description here

\documentclass[border=1mm]{standalone}
\usepackage{forest}
\begin{document}
    \begin{forest}
        for tree={
        calign=center,
        grow'=east, % tree direction
        parent anchor=east, child anchor=west, % edge anchors
        rounded corners, draw,
        text height=1.4ex, text depth=0.2ex % <<<<<<<<<<<<<
        }
        [g [B [g [D [E[F]]] [D' [E' [F']]] ]]]
    \end{forest}
\end{document}
  • Where did you get the values (1.4ex and 0.2ex) from? – gtatr Dec 2 '16 at 11:02
  • Trial and error. It very much depends on which signs actually may occur. If you use parentheses, it has to be increased. Using larger values from the outset without needing them will result in nodes that look too big, or in letters off-center. So maybe you have to choose different values for subtrees where the letters are more uniform. – gernot Dec 2 '16 at 11:06
  • I expected some standard measures or constants, something like (an imaginary) \maxfontheight. I tried to google for it but found nothing – gtatr Dec 2 '16 at 12:13
  • 1
4

You can add a \strut to the nodes' contents automatically and reduce the inner ysep a bit so the nodes do not become too tall. You may still want some fine-tuning, but the default result may be better than it might be otherwise.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={
    calign=center,
    grow'=east, % tree direction
    parent anchor=east, child anchor=west, % edge anchors
    rounded corners, draw,
    inner ysep=1pt
  },
  before typesetting nodes={
    for tree={
      content/.wrap value={\strut #1},
    }
  }
  [g [B [g [D [E[F]]] [D' [E' [F']]] ]]]
\end{forest}
\end{document}

with auto-<code>\strut</code>

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.