3

I try to align the children in a tree in a way, that all children are vertically aligned. That's the code I have so far. You can see my problem below the node "I'm a very very very long text".

The child is anchored to the center of it's parent. However, I want the level always with a fixed position to it's parent so that all children on the same level have always the same indent.

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\tikzstyle{selected}=[draw=red,fill=red!30]
\tikzstyle{optional}=[dashed,fill=gray!50]
\begin{tikzpicture}[%
  grow via three points={one child at (0.5,-0.7) and
  two children at (0.5,-0.7) and (0.5,-1.4)},
  edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
  \node {texmf}
    child { node {doc}}     
    child { node {fonts}}
    child { node {I'm a very very very long text}
        child { node {I want to have the same indent like generic}}
        child { node [optional] {I want to have the same indent like latex}}
        child { node {I want to have the same indent like plain}}}
    child [missing] {}              
    child [missing] {}              
    child [missing] {}
    child { node [selected] {tex}
        child { node {generic}}
        child { node [optional] {latex}}
        child { node {plain}}
    };
\end{tikzpicture}
\end{document}

enter image description here

3

Adaptations:

  • added tikz option: growth parent anchor=south west (default was south)
  • changed tikz option: edge from parent path to starting point ([xshift=2mm] \tikzparentnode.south west)
  • changed the grow points to avoid overlapping
  • Use tikzset instead of tikzstyle (see Should \tikzset or \tikzstyle be used to define TikZ styles?).

Code:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}

\begin{document}
    \begin{tikzpicture}[%
        every node/.style={draw=black,thick,anchor=west},
        selected/.style={draw=red,fill=red!30},
        optional/.style={dashed,fill=gray!50},
        %
        grow via three points={one child at (0.5,-0.5) and
        two children at (0.5,-0.5) and (0.5,-1.3)},
        edge from parent path={([xshift=2mm] \tikzparentnode.south west) |- (\tikzchildnode.west)},
        growth parent anchor=south west
    ]
        \node {texmf}
            child { node {doc}}     
            child { node {fonts}}
            child { node {I'm a very very very long text}
                child { node {I want to have the same indent like generic}}
                child { node [optional] {I want to have the same indent like latex}}
                child { node {I want to have the same indent like plain}}
            }
            child [missing] {}              
            child [missing] {}              
            child [missing] {}
            child { node [selected] {tex}
                child { node {generic}}
                child { node [optional] {latex}}
                child { node {plain}}
            };
    \end{tikzpicture}
\end{document}

Result:

enter image description here

1

Just for fun. Since this tree can be found almost literally in the forest manual, I thought it might be worthwhile to add it here.

\documentclass{article}
\usepackage[edges]{forest}
\begin{document}
\tikzset{selected/.style={draw=red,fill=red!30},
        optional/.style={dashed,fill=gray!50}}
\begin{forest}
for tree={draw,grow'=0,folder}
[texmf
 [doc]
 [fonts]
 [I'm a very very very long text
  [I want to have the same indent like generic]
  [I want to have the same indent like latex,optional]
  [I want to have the same indent like plain]
 ] 
 [tex,selected
  [generic]
  [latex,optional]
  [plain]
 ]
]
\end{forest}
\end{document}

enter image description here

This works out of the box and does not require any missing children nor other nudging.

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.