TikZ(树):如何将树中的子节点对齐到同一级别

TikZ(树):如何将树中的子节点对齐到同一级别

我尝试以某种方式将子节点在树中对齐,使所有子节点都垂直对齐。这是我目前的代码。您可以在节点“我是一个非常非常长的文本”下方看到我的问题。

子元素锚定在其父元素的中心。但是,我希望该级别始终相对于其父元素处于固定位置,以便同一级别的所有子元素始终具有相同的缩进。

\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}

在此处输入图片描述

答案1

改编:

代码:

\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}

结果:

在此处输入图片描述

答案2

只是为了好玩。由于这棵树几乎可以在手册中找到forest,所以我认为值得在这里添加它。

\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}

在此处输入图片描述

这是开箱即用的,不需要任何失踪的孩子或其他推动。

相关内容