ファントムノードを使用してノードレベルを調整する

ファントムノードを使用してノードレベルを調整する

MWE について考えてみましょう。

\documentclass[landscape]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
  phantom/.style = {}
}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 3in/#1, level distance = 1.5cm}] 
  \node [block] {P1}
    child {node [block] {P2}
      child {node [block] {P3}
        child {node [block] {P4}}
        child {node [block] {P5}}
      }
    }
    child {node [block] {P6}
      child {node [phantom] {}
        child {node [block] {P7}}
      }
    }
    child {node [block] {P8}
      child {node [block] {P9}
        child {node [block] {P10}}
      }
    }
  ;
\end{tikzpicture}

\end{document}

ファントム ノードを使用して、P7 を P4、P5、P10 と同じレベルにしています。

しかし、ファントム ノードの代わりに空白スペースが作成され、P6 からのコネクタはそこで終了し、その空白スペースから P7 への新しいコネクタが開始されます。

P6 から P7 への直線コネクタを取得するにはどうすればよいですか? 一般的に、さまざまなノードの「レベル」を調整するにはどうすればよいでしょうか?

答え1

これは、forest役に立つかもしれません。tierキーを使用すると、手間をかけずに目的の配置を取得できます (コードが短くなっていることにも注意してください)。

\documentclass{article}
\usepackage{forest}

\tikzset{
treenode/.style={
  align=center, 
  inner sep=2pt, 
  rounded corners=2pt,
  minimum width = 2cm,
  font=\sffamily
  },
block/.style={
  treenode, 
  rectangle, 
  white, 
  font=\sffamily\bfseries, 
  draw=black, 
  fill=black
  },
  phantom/.style = {}
}

\begin{document}


\begin{forest}
for tree={
  block,
  edge={->,>=latex},
  where level={0}{s sep=1.5cm}{}
}
[P1
  [P2
    [P3
      [P4]
      [P5,tier=last]
    ]
  ]
  [P6,before computing xy={s=(s("!p")+s("!n"))}
    [P7,tier=last]
  ]
  [P8
    [P9]
    [P10,tier=last]
  ]
]
\end{forest}

\end{document}

ここに画像の説明を入力してください

ノードの現在のラベル付けスキーマもパッケージに残すことができるため、コードをさらに短くすることができます。

答え2

これは可能な解決策です。スペースを削除するには、coordinateの代わりにを使用しますnode。ここでは、 edge from parent/.style={draw=none}P6 から P7 への線を描画しないようにするために、(P6)--(P7) の線を手動で描画します。

ここに画像の説明を入力してください

コード

\documentclass[border=1cm]{standalone}
%\documentclass[landscape]{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\begin{document}

\tikzset{
  treenode/.style = {align=center, inner sep=2pt, rounded corners = 2pt, minimum width = 2cm, text centered, font=\sffamily},
  block/.style = {treenode, rectangle, white, font=\sffamily\bfseries, draw=black, fill=black},
  phantom/.style = {},
}

\begin{tikzpicture}[->,>=stealth',
level/.style={sibling distance = 3in/#1, level distance = 1.5cm}
] 
  \node [block] {P1}
    child {node [block] {P2}
      child {node [block] {P3}
        child {node [block] {P4}}
        child {node [block] {P5}}
      }
    }
    child {node [block](a) {P6}
      child {coordinate [phantom,edge from parent/.style={draw=none}] {}
        child {node [block](b) {P7}}
      }
    }
    child {node [block] {P8}
      child {node [block] {P9}
        child {node [block] {P10}}
      }
    }
  ;
\draw[->] (a)--(b);
\end{tikzpicture}

関連情報