使用 tikz 和 let 语法进行相对定位会导致位置(y 轴)稍微不正确

使用 tikz 和 let 语法进行相对定位会导致位置(y 轴)稍微不正确

b我正在尝试根据x一个节点的位置和y另一个节点的位置来设置节点的位置。

我使用该let函数,正如我从另一个人那里学到的线

在我的代码中,这在某种程度上导致了稍微不正确的位置,即节点的位置比预期的略低,从而产生了轻微但明显的间隙。

在这里你可以看到b节点和它的标签位于y节点的位置下方w1在此处输入图片描述

我知道这是一个很小的问题,但我想了解一下。是什么导致了差距?

\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}

答案1

pgfmanual v3.1.4 教程第 70-71 页解释并解决了这个问题。您在这里看到的是pg“低于基线”和b高于x和的效果w。您可以通过为节点提供一些固定的text heights 和 来解决此问题text depth。(我还利用这个机会摆脱了\tikzstyle这里未使用的弃用和删除的库。红线仅用于引导眼睛。)

\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}

在此处输入图片描述

相关内容