目录树中的对齐

目录树中的对齐

我想绘制目录树,请参阅下面的代码。我无法在以下区域正确对齐:

  • 这里是文本的 H 和这是更多文本的 T 必须对齐。
  • 所有水平箭头应大小相同。目前似乎就是这样,但更改某些节点的文本/长度会使箭头混乱。
  • 所有子节点都应左对齐;Hello 中的每个 H 都应对齐。目前似乎也是这种情况,但对于不同的文本,情况并非总是如此。

有人能帮助我吗?:)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows,trees}

\tikzset{
  basic/.style  = {draw, text width=2cm, drop shadow,font=\sffamily,rectangle},
  root/.style   = {basic, rounded corners=2pt, thin, align=center,
                   fill=green!30},
  level 2/.style = {basic, rounded corners=6pt, thin,align=center,     fill=green!60,
                   text width=8em},
  level 3/.style = {basic, thin, align=left, fill=pink!60, text width=6.5em}
}

\begin{document}
\begin{tikzpicture}[
  level 1/.style={sibling distance=40mm},
  edge from parent/.style={->,draw},
  >=latex]

\node (c1){Here is text};
\node [below of = c1, xshift=15pt] (c11) {Hello};
\node [below=of c11.west,anchor=west] (c12) {Hello};
\node [below=of c12.west,anchor=west] (c13) {Hello};
\node [below=of c13.west,anchor=west] (c14) {Hello};

\node [below of = c14, xshift=-15pt] (c2) {This is some more text};
\node [below of = c2, xshift=30pt] (c21) {Hello};
\node [below=of c21.west,anchor=west] (c22) {Hello};
\node [below=of c22.west,anchor=west] (c23) {Hello};

\foreach \value in {1,2,3,4}
  \draw[->] (c1.195) |- (c1\value.west);

\foreach \value in {1,2,3}
  \draw[->] (c2.195) |- (c2\value.west);
\end{tikzpicture}
\end{document}

答案1

我认为您会对dirtreeHenri 的回答中的包裹感到更舒服。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,shapes,positioning,shadows}

\begin{document}
\begin{tikzpicture}[>=latex]

\node[anchor=west] (c1) {Here is text};
\node [below right = of  c1.south west] (c11) {Hello};
\node [below=of c11.west,anchor=west] (c12) {Hello};
\node [below=of c12.west,anchor=west] (c13) {Hello};
\node [below=of c13.west,anchor=west] (c14) {Hello};

\node[anchor=west]  (c2) at ([yshift=-9mm]c1.west |- c14) {This is some more text};
\node [below right = of  c2.south west] (c21) {Hello};
\node [below=of c21.west,anchor=west] (c22) {Hello World};
\node [below=of c22.west,anchor=west] (c23) {Hello};

\foreach \value in {1,2,3,4}
  \draw[->] (c1.south west) |- (c1\value.west);

\foreach \value in {1,2,3}
  \draw[->] (c2.south west) |- (c2\value.west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

dirtree解决方案能满足您的需求吗?

\documentclass{article}
\usepackage{dirtree}
\renewcommand\DTstyle{}
\begin{document}

\dirtree{%
.1 .
.2 Here is text.
.3 Hello.
.3 Hello.
.3 Hello.
.3 Hello.
.2 This is some more text.
.3 Hello.
.3 Hello.
.3 Hello.
}

\end{document}

在此处输入图片描述

答案3

这是一个使用强大forest包的解决方案。我创建了两个版本。第一个使用当前在 CTAN 和 TeX 发行版中的包。第二个使用目前正在测试并在 Git Hub 上提供的新测试版。现在,我介绍第一个解决方案:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\forestset{
  dir tree/.style={
    for tree={
      parent anchor=south west,
      child anchor=west,
      anchor=mid west,
      inner ysep=1pt,
      grow'=0,
      align=left,
      if level=1{no edge}{
        edge path={
          \noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) |- (.child anchor)\forestoption{edge label};
        },
      },
      font=\sffamily,
      if n children=0{}{
        delay={
          prepend={[,phantom, calign with current]}
        }
      },
      fit=band,
      before computing xy={
        l=2em
      }
    },
  }
}
\begin{document}
\begin{forest}
  dir tree,
  for tree={
    edge={-Stealth}
  }
  [
    [Here is some text.
      [Short thing.]
      [Longer thing.]
      [{Much, much, much, much, much longer thing.}]
      [Taller thing, font=\Huge]
    ]
    [This is some text.
      [A]
      [B]
      [C]
      [D]
    ]
    [Here is some more text with yet further words.
      [This is an extremely tiresome and long and lengthy and repetitious kind of thing.]
      [This is another extremely tiresome and long and lengthy and repetitious kind of thing.]
      [This is one more boring and extremely tiresome and long and lengthy and repetitious kind of thing.]
      [This is disappointingly similar to previous extremely tiresome and long and lengthy and repetitious kinds of things.]
    ]
  ]
\end{forest}
\end{document}

带有当前 <code>forest</code> 的目录树

相关内容