3

Is there a way to align nodes by baseline in TikZ graphs?

\documentclass[tikz]{standalone}

\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}

\begin{document}
\begin{tikzpicture}[tree layout]
  \node {aligned?}
    child {%
      node {yes}
    }
    child {%
      node {no}
    };
\end{tikzpicture}
\end{document}

result

In the example above, the yes node is taller because the y, and the nodes seem to be centered vertically, so the baseline of the no node ends up lower than the yes node's baseline. Is there a way to fix this?

1 Answer 1

3

Welcome to TeX-SE! Usually this is done by giving the nodes some text depth.

\documentclass[tikz]{standalone}

\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}

\begin{document}
\begin{tikzpicture}[tree layout,nodes={text depth=0.3ex}]
  \node {aligned?}
    child {%
      node {yes}
    }
    child {%
      node {no}
    };
\end{tikzpicture}
\end{document}

enter image description here

6
  • Ilka had exactly the same problem while working on "The Programming Language Pascal".
    – ScumCoder
    May 26, 2019 at 14:28
  • 2
    @ScumCoder Yes. You can also find it right under Part IV Graph Drawing on p. 409 of pgfmanual v3.1.3.
    – user121799
    May 26, 2019 at 14:31
  • +1: I did not encounter text depth so far. Is this something new? May 26, 2019 at 15:33
  • 1
    @Dr.ManuelKuehner No, it has been there for ages.
    – user121799
    May 26, 2019 at 17:26
  • 1
    @BaltazárRadics I do not know the "perfect way". Apart from text height there is also the baseline option, which is useful for inline nodes. I do not know if there is a simple way to use it here (but I also did not try). Personally I would use forest for such trees which solves most of these issues either automatically or easily.
    – user121799
    May 26, 2019 at 22:45

You must log in to answer this question.

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