相対配置を使用して手動でノードを配置してグラフを作成しようとしています。しかし、ノードの配置方法に問題があります。これを MWE に組み込もうとしました。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex}
]
\small
\node (a) [test node] {First node};
\node (b) [test node, above right=of a] {Upper branch 1};
\node (c) [test node, below right=of a] {Lower branch 1};
\begin{scope}[red]
\foreach \pos/\n in {above right/x, right/y, below right/z}
{
\node (\n) [circle, draw, \pos=of a] {};
\foreach \a in {north, center, south}
{
\draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
}
}
\foreach \n in {x, y, z}
\draw (0,2 -| \n.center) -- ++(0,-4);
\node [circle, draw, below=of a] at (3,0) {};
\end{scope}
\end{tikzpicture}
\end{document}
ご覧のとおり、最初のノードに対する円とテキスト ノードの配置は一致していません。次に、3 つの円は水平に揃っていません。そして 3 つ目の「ボーナス」として、キーとbelow
x 方向の明示的な位置の組み合わせで配置された最後の円は、3 つ目の方法で一致していません。
手動で座標を追加せずに、このようなノード (さまざまな形状やさまざまな配置方法) を配置する正しい方法は何ですか? さまざまな形状を組み合わせる必要があり、ノードをさまざまなブランチに正しく分散するには、いくつかの x 座標を明示的に設定する必要があります...
編集: 明確にするために、円と長方形を垂直に揃え、西側のアンカーを水平に揃えたいと思います。
答え1
\node (b) [test node, above right=of a] {Upper branch 1};
anchor
ノードb
のをに設定しsouth west
、ないにwest
。すると、b.south west
は の 1cm 右、1cm 上に配置されますa.north east
。また、ノードが円の場合、x
アンカーsouth west
との間には -方向の差がありますwest
。
のアンカーを の代わりに使用するb
必要がありますwest
south west
anchor=west
後オプションabove right=of a
。
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex},
node distance=1cm and 1cm
]
\small
\node (a) [test node] {First node};
\node (b) [test node, above right=of a,anchor=west] {Upper branch 1};
\node (c) [test node, below right=of a,anchor=west] {Lower branch 1};
\begin{scope}[red]
\foreach \pos/\n in {above right/x, right/y, below right/z}
{
\node (\n) [circle, draw, \pos=of a,anchor=west] {};
\foreach \a in {north, center, south}
{
\draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
}
}
\foreach \n in {x, y, z}
\draw (0,2 -| \n.center) -- ++(0,-4);
\node [circle, draw, below=of a] at (3,0) {};
\end{scope}
\draw[green](a.east)--+(1,0);
\draw[blue](a.north east)--++(1,0)--+(0,1);
\draw[orange](a.south east)--++(1,0)--+(0,-1);
\end{tikzpicture}
\end{document}
また、次のwest
アンカーを基準にしてアンカーを配置したい場合もあります。east
a
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
test node/.style={rectangle, draw, text height=1.5ex, text depth=0.25ex},
node distance=1cm and 1cm
]
\small
\node (a) [test node] {First node};
\node (b) [test node, above right=of a.east,anchor=west] {Upper branch 1};
\node (c) [test node, below right=of a.east,anchor=west] {Lower branch 1};
\begin{scope}[red]
\foreach \pos/\n in {above right/x, right/y, below right/z}
{
\node (\n) [circle, draw, \pos=of a.east,anchor=west] {};
\foreach \a in {north, center, south}
{
\draw[shift=(\n.\a)] plot [mark=x] coordinates{(0,0)};
}
}
\foreach \n in {x, y, z}
\draw (0,2 -| \n.center) -- ++(0,-4);
\node [circle, draw, below=of a] at (3,0) {};
\end{scope}
\draw[green](a.east)--+(1,0);
\draw[blue](a.east)++(1,0)--+(0,1);
\draw[orange](a.east)++(1,0)--+(0,-1);
\draw[purple](a.center)--++(0,-1)--+(3,0);
\end{tikzpicture}
\end{document}