5

I have a simple tree. I cannot figure out how to align children vertically.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
    \node {0}
        child {node {a}}
        child {node {b}}
        child {node {c} 
            child {node {a1}}
            child {node {b1}}
            child {node {c1}}
            child {node {d1}}
        }
        child {node {d}}
        ;
\end{tikzpicture}

\end{document}

Compiling the code gives a subtree under node c, but I want x and x1 to align vertically. Do I have to manually place every node? Options like below of=a miss align the remaining children. I'm looking for something like leftalign whole tree.

2
  • 1
    Welcome to TSE. Please post a Minimal Working Example, instead of a code snippet. Commented Mar 27, 2023 at 6:37
  • What is "x" and "x1" in your MWE? Do you mean something like \node {0} [grow=right] ... ? Or maybe you want [level 2/.style={every node/.style={xshift=-7.5mm}}] (as option to the tikzpicture)? Commented Mar 27, 2023 at 7:24

1 Answer 1

6

I am not sure whether this is what you want, but you can shift the nodes of a certain level to the left and right (since the default sibling distance is 15mm, in order to vertically align the nodes, you would need to shift all nodes by the half of this value which is 7.5mm):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[level 2/.style={every node/.style={xshift=-7.5mm}}]
    \node {0} 
        child {node {a}}
        child {node {b}}
        child {node {c} 
            child {node {a1}}
            child {node {b1}}
            child {node {c1}}
            child {node {d1}}
        }
        child {node {d}}
        ;
\end{tikzpicture}
\end{document}

enter image description here


Variations of the above if one node is missing:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[level 2/.style={sibling distance=22.5mm, every node/.style={xshift=-7.5mm}}]
    \node {0} 
        child {node {a}}
        child {node {b}}
        child {node {c} 
            child {node {a1}}
            child {node {b1}}
            child {node {d1}}
        }
        child {node {d}}
        ;
\end{tikzpicture}
\end{document}

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[level 2/.style={every node/.style={xshift=-7.5mm}}]
    \node {0} 
        child {node {a}}
        child {node {b}}
        child {node {c} 
            child {node {a1}}
            child {node {b1}}
            child[missing] {}
            child {node {d1}}
        }
        child {node {d}}
        ;
\end{tikzpicture}
\end{document}

enter image description here


Maybe, you could also try the forest package:

\documentclass[border=10pt]{standalone}
\usepackage{forest}

\begin{document}
\begin{forest} for tree={node options={text width=1.25em, align=center}}
 [0
    [a] 
    [b] 
    [c
        [a1]
        [b1]
        [c1, calign with current]
        [d1]
    ] 
    [d]
 ]
\end{forest}
\end{document}

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage{forest}

\begin{document}    
\begin{forest} for tree={node options={text width=1.25em, align=center}}
 [0
    [a] 
    [b] 
    [c
        [a1]
        [b1]
        [, phantom, calign with current]
        [d1]
    ] 
    [d]
 ]
\end{forest}
\end{document}

enter image description here

5
  • 1
    thank you, that is exactly what I want
    – Chelz
    Commented Mar 27, 2023 at 7:42
  • what if, I don't have c1(or any other node on level2) and want to have each level have the same width?
    – Chelz
    Commented Mar 27, 2023 at 7:47
  • You may ask this new question in another post.
    – SebGlav
    Commented Mar 27, 2023 at 8:20
  • @Chelz See my edit ... Commented Mar 27, 2023 at 8:36
  • @JasperHabicht Thank you. I was afraid, that I had to compute xshift and sibling distance manually, but that's the best solution for now.
    – Chelz
    Commented Mar 27, 2023 at 8:44

You must log in to answer this question.

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