Relative positioning with tikz and let syntax results in slightly incorrect (y-axis) position

by Zapnuk   Last Updated September 11, 2019 22:23 PM

I'm trying to set the position of the b node based on the x position of one, and the y position of another node.

I use the let function, as I learned from another thread.

In my code this somehow results in a slightly incorrect position where the node is positioned slightly lower than expected, resulting in a minor but noticable gap.

Here you can see that the b node and its label is below the y position of node w1: enter image description here

I know this is a very minor problem, but I would like to understand it. What causes the gap?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{shapes,arrows,3d,calc,fit,shadows,snakes,automata,backgrounds,petri,positioning,chains,decorations.pathreplacing}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture} [start chain=2,node distance=13mm, label distance=3mm]
\tikzstyle{init} = [
    draw,
    circle,
    inner sep=2pt,
    font=\Huge,
    join = by -latex
]

\tikzstyle{squa} = [
    draw,
    inner sep=2pt,
    font=\Large,
    join = by -latex
]

\node[on chain=2] (x2) at (0,0) {$x_2$};
\node[on chain=2,join=by o-] {$w_2$};
\node[on chain=2,init] (sigma) {$\displaystyle\Sigma$};
\node[on chain=2,label=above:{\parbox{2cm}{\centering Activation \\ function}}] (phi) {$\phi(...)$};
\node[on chain=2,label=above:Output, join=by -latex] (y) {$y$};
\begin{scope}[start chain=1]
    \node[on chain=1,label=above:Inputs] at (0,1.5cm) (x1) {$x_1$};
    \node[on chain=1,label=above:Weights, join=by o-] (w1) {$w_1$};
\end{scope}

\begin{scope}[start chain=4]
    \node[on chain=4] at (0,-1.5cm) (xn) {$x_{n}$};
    \node[on chain=4,join=by o-] (wn) {$w_{n}$};
\end{scope}

\path let \p1=(sigma), \p2=(w1) in node[label=above:Bias] (b) at (\x1, \y2) {$b$};

\draw[-latex] (w1) -- (sigma);
\draw[-latex] (wn) -- (sigma);
\draw[o-latex] (b) -- (sigma);
\path (x2) -- (xn) node [font=\large, midway, sloped] {$\dots$};

\draw[-latex] (sigma) -- (phi) node[above, midway] (net) {\textit{net}};

\end{tikzpicture}
\caption{Structure of an Artificial Neuron}
\end{figure}
\end{document}


Answers 1


The issue is explained and solved in the tutorial in the pgfmanual v3.1.4 on pages 70-71. What you seeing here are the effects of p and g "going below the baseline" and b being higher than x and w. You can solve the issue by giving the nodes some fixed text heights and text depths. (I also used the opportunity to get rid of the deprecated \tikzstyle and removed libraries which are not used here. The red lines are only to guide the eye.)

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc,chains,positioning,arrows}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[start chain=2,node distance=13mm, label
 distance=3mm,fw/.style={text depth=0.25ex,text height=1.5ex},
 every label/.append style={fw},
 init/.style={draw, circle, inner sep=2pt, font=\Huge, join= by -latex},
 squa/.style={draw, inner sep=2pt, font=\Large, join = by -latex}]

\node[on chain=2,fw] (x2) at (0,0) {$x_2$};
\node[on chain=2,join=by o-,fw] {$w_2$};
\node[on chain=2,init] (sigma) {$\displaystyle\Sigma$};
\node[on chain=2,label=above:{\parbox{2cm}{\centering Activation \\ function}}]
(phi) {$\phi(\dots)$};
\node[on chain=2,label=above:Output, join=by -latex] (y) {$y$};
\begin{scope}[start chain=1]
    \node[on chain=1,label=above:Inputs,fw] at (0,1.5cm) (x1) {$x_1$};
    \node[on chain=1,label=above:Weights, join=by o-,fw] (w1) {$w_1$};
\end{scope}

\begin{scope}[start chain=4]
    \node[on chain=4] at (0,-1.5cm) (xn) {$x_{n}$};
    \node[on chain=4,join=by o-] (wn) {$w_{n}$};
\end{scope}

\path let \p1=(sigma), \p2=(w1) in node[label={[name=Bias]above:Bias},fw] (b) at (\x1, \y2) {$b$};

\draw[-latex] (w1) -- (sigma);
\draw[-latex] (wn) -- (sigma);
\draw[o-latex] (b) -- (sigma);
\path (x2) -- (xn) node [font=\large, midway, sloped] {$\dots$};

\draw[-latex] (sigma) -- (phi) node[above, midway] (net) {\textit{net}};
\begin{scope}[overlay,red,thin] % only for illustration
 \draw (b.base-|b.south east) -- ++ (-4.5,0);
 \draw (Bias.base-|Bias.south east) -- ++ (-5,0);
\end{scope}
\end{tikzpicture}
\caption{Structure of an Artificial Neuron.}
\end{figure}
\end{document}

enter image description here

Schrödinger's cat
Schrödinger's cat
September 11, 2019 21:58 PM

Related Questions


Nodes on arrows not aligned nicely

Updated May 12, 2018 12:23 PM

Position of nodes in TIKZ

Updated October 20, 2017 09:23 AM

Position of arrows in Tikz

Updated November 08, 2017 15:23 PM