6

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.

2 Answers 2

5

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}
5
  • Where did you get the values (1.4ex and 0.2ex) from?
    – gtatr
    Commented Dec 2, 2016 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
    Commented Dec 2, 2016 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
    Commented Dec 2, 2016 at 12:13
  • 1
    @g.tataranni See Determine height and depth of letters relative to the font size
    – gernot
    Commented Dec 2, 2016 at 12:19
  • @gtatr Alternatively, you could set a fixed height for your font by useing \setlength{\ttlhght}{\totalheightof{characters in your tree}}. I used this length to place a zero-width parbox (\newcommand{\hghtbx}{\parbox[c][\ttlhght]{0pt}{}}) in every node.
    – Jopie
    Commented Mar 27, 2020 at 9:26
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>

You must log in to answer this question.

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