Я пытаюсь построить график, вручную размещая узлы с помощью относительного размещения. Но я столкнулся с некоторыми проблемами с тем, как размещены узлы. Я попытался включить его в 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}
Как вы видите, расположение кругов и текстовых узлов относительно первого узла не совпадает. Во-вторых, три круга не выровнены по горизонтали. И в качестве третьего "бонуса" последний круг, размещенный с комбинацией ключа below
и явного положения в направлении x, не совпадает третьим способом.
Как правильно разместить такие узлы (с разными формами и разными способами их размещения) без ручного добавления координат? Мне нужно объединить разные формы и мне нужно явно задать некоторые x-координаты, чтобы правильно распределить узлы по разным ветвям...
Редактировать: Для уточнения: я хотел бы выровнять круги и прямоугольники по вертикали, а также выровнять их западные якоря по горизонтали.
решение1
\node (b) [test node, above right=of a] {Upper branch 1};
устанавливает anchor
узел b
в значение south west
инетк west
. Затем b.south west
располагается на 1 см правее и на 1 см выше 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}