b
我正在尝试根据x
一个节点的位置和y
另一个节点的位置来设置节点的位置。
我使用该let
函数,正如我从另一个人那里学到的线。
在我的代码中,这在某种程度上导致了稍微不正确的位置,即节点的位置比预期的略低,从而产生了轻微但明显的间隙。
我知道这是一个很小的问题,但我想了解一下。是什么导致了差距?
\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 页解释并解决了这个问题。您在这里看到的是p
和g
“低于基线”和b
高于x
和的效果w
。您可以通过为节点提供一些固定的text height
s 和 来解决此问题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}