Estoy intentando construir un gráfico colocando manualmente los nodos usando una ubicación relativa. Pero tengo algunos problemas con la forma en que se colocan los nodos. Intenté incluirlo en un 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}
Como puede ver, la ubicación de los círculos y de los nodos de texto en relación con el primer nodo no coincide. En segundo lugar, los tres círculos no se alinean horizontalmente. Y como tercer "bonus", el último círculo colocado con una combinación de la below
clave y una posición explícita en la dirección x no coincide en una tercera forma.
¿Cuál es la forma correcta de colocar dichos nodos (con diferentes formas y diferentes formas de colocarlos) sin agregar coordenadas manualmente? Necesito combinar diferentes formas y necesito establecer algunas coordenadas x explícitamente para distribuir correctamente los nodos en diferentes ramas...
Editar: Para aclarar: me gustaría alinear verticalmente los círculos y los rectángulos, y me gustaría alinear horizontalmente sus anclajes occidentales.
Respuesta1
\node (b) [test node, above right=of a] {Upper branch 1};
establece el anchor
nodo b
en south west
ynoa west
. Luego b.south west
se coloca 1 cm a la derecha y 1 cm arriba de a.north east
. Y si el nodo es un círculo, hay una diferencia de x
dirección entre ancla south west
y west
.
Si el ancla de b
debería estar west
en lugar de south west
tienes que usaranchor=west
despuésla opción 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}
Y tal vez desee colocar los west
anclajes en relación con el east
anclaje de 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}