1

The tree below is produced by the code beneath it. In the tree, the parent node is slightly out of alignment. You will notice that the edges to its children are asymmetrical: the one on the left is slightly shorter than the one on the right.

I cannot figure out what is causing this—perhaps it is something to do with the use of nodeparts. How can I ensure that the parent node in this tree will be properly positioned to ensure symmetrical edges?

A simple computation tree with asymmetrical edges

EDIT: Schrödinger's cat provided an answer which reminds me that I would like the solution to preserve the spaces between different levels of the tree in more complex trees, and to keep the separation between a parent and its child constant even if more branches are added.

For example, in the slightly more complex tree below the spacing between parent and children and between siblings is good, but the same asymmetry can be noticed between the root node and its children, and additionally the lowest node on the tree is connected to its parent by a slanted line when I would like it to be a straight vertical line.

A slightly more complex computation tree

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.multipart, positioning}

\usepackage{xinttools}

\usepackage{forest}

\def\numname#1{%
    \ifcase#1 \or one\or two\or three\or four\or five\or six\or seven\or eight\or nine\or ten\or eleven \or twelve\or thirteen\or fourteen\or fifteen\or sixteen\or seventeen\or eighteen\or nineteen\or twenty\fi%
}

\newcounter{cellnum}
\newcommand{\tape}[1]{%
    \setcounter{cellnum}{1}
    \xintFor* ##1 in {#1} \do {%
        \nodepart[text width=1.5ex, align=center]{\numname{\value{cellnum}}} $##1$%
        \stepcounter{cellnum}%
    }
    \addtocounter{cellnum}{-1}%
}

\begin{document}

\begin{forest}
    for tree={
        delay={
            content={\tape{#1}}
        },
        rectangle split,
        rectangle split horizontal,
        rectangle split parts=\value{cellnum},
        rectangle split part align=base,
        draw,
        l sep=6ex,
        s sep=2em,
        edge path={
            \noexpand\path[draw] 
            ([yshift=-2.5ex]!u.children) -- ([yshift=1ex].parent);
        },
        head/.style n args=2{
            tikz={\node[below=0cm of .\numname{#1} south, font=\scriptsize]{$#2$};}
        }
    }
    [111, head={1}{1}
        [0111, head={1}{2}]
        [111, head={2}{1}]
    ]
\end{forest}

\end{document}
2
  • Adding anchor=parent to for tree solves this problem, making this probably a duplicate of tex.stackexchange.com/questions/508172/…. I should have searched more thoroughly before asking.
    – solisoc
    Feb 21, 2020 at 6:59
  • @dexteritas Yes—that's the question I linked to in my comment above yours.
    – solisoc
    Feb 21, 2020 at 9:25

1 Answer 1

1

You can use calign=fixed edge angles.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.multipart, positioning}

\usepackage{xinttools}

\usepackage{forest}

\def\numname#1{%
    \ifcase#1 \or one\or two\or three\or four\or five\or six\or seven\or eight\or nine\or ten\or eleven \or twelve\or thirteen\or fourteen\or fifteen\or sixteen\or seventeen\or eighteen\or nineteen\or twenty\fi%
}

\newcounter{cellnum}
\newcommand{\tape}[1]{%
    \setcounter{cellnum}{1}
    \xintFor* ##1 in {#1} \do {%
        \nodepart[text width=1.5ex, align=center]{\numname{\value{cellnum}}} $##1$%
        \stepcounter{cellnum}%
    }
    \addtocounter{cellnum}{-1}%
}

\begin{document}

\begin{forest}
    for tree={
        delay={
            content={\tape{#1}}
        },
        calign=fixed edge angles,
        rectangle split,
        rectangle split horizontal,
        rectangle split parts=\value{cellnum},
        rectangle split part align=base,
        draw,
        l sep=6ex,
        s sep=2em,
        edge path={
            \noexpand\path[draw] 
            ([yshift=-2.5ex]!u.children) -- ([yshift=1ex].parent);
        },
        head/.style n args=2{
            tikz={\node[below=0cm of .\numname{#1} south, font=\scriptsize]{$#2$};}
        }
    }
    [111, head={1}{1}
        [0111, head={1}{2}]
        [111, head={2}{1}]
    ]
\end{forest}

\end{document}

enter image description here

4
  • This is okay if there are only two branches, but the spacing gets really big if more are added.
    – solisoc
    Feb 21, 2020 at 6:17
  • Also, in more complex trees, this causes there to be spaces of different height between different levels of the tree. I would like it to be uniform.
    – solisoc
    Feb 21, 2020 at 6:25
  • @solisoc Well, what can I say. When I was writing an answer to your previous question, I was actually oscillating between shapes.multipart and using a tabular. It is great that you found the option that I thought to be an alternative. What is less great (please forgive me that I say this) is that you only specify part of the requirements. So all I can do is to write an answer based on what I see. I did this here and in you previous question. I am sorry, I cannot read minds (even though I am a cat ;-).
    – user194703
    Feb 21, 2020 at 6:33
  • No, that's quite understandable. I apologise for not thinking enough to state these requirements in the question. Your answer to my previous question was very good, thank you.
    – solisoc
    Feb 21, 2020 at 6:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.