3

How do I force the middle child of a tikz tree layout to be vertically aligned with its parent? For example, in the following code, the middle child of the root node is noticeably to the left of the center of the root node.

\documentclass[english]{article}
\usepackage{tikz}
\usetikzlibrary{graphdrawing,graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[tree layout]
\tikzset{sibling distance=0em}
\tikzset{sibling sep=0em}
\node {$+$}
 child {node{$+$}
  child {node{$Exp$}}
  child {node{$+$}}
  child {node{$Exp$}}
 }
 child {node{$+$}}
 child {node{$Exp$}}
;
\end{tikzpicture}
\end{document}

Note that the problem goes away if I don't set sibling distance and sibling sep to 0em, but I need those to get some of the larger diagrams to fit in the context of my document.

3

If you are prepared to use the specialist tree drawing package, Forest, then you can achieve a high degree of flexibility and control over the output.

In addition to allowing a high degree of control and supporting powerful automation options, Forest also permits very concise specifications of trees. In this case, the tree itself is given by

 [+[+[Exp][+][Exp]][+][Exp]]

although

  [+
    [+
      [Exp]
      [+]
      [Exp]
    ]
    [+]
    [Exp]
  ]

is a lot easier for a human to read since it reflects the tree's structure spatially.

For example,

\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
  for tree={%
    math content,
    s sep=0pt,
    text height/.pgfmath={height("Xpy")},
    text depth/.pgfmath={depth("Xpy")},
  },
  where level=0{}{
    if={n()==((n_children("!u")+1)/2)}{calign with current}{},
  },
  [+
    [+
      [Exp]
      [+]
      [Exp]
    ]
    [+]
    [Exp]
  ]
\end{forest}
\end{document}

Forest solution

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.