Tikz Arrow 标签定位问题

Tikz Arrow 标签定位问题

我有以下 tikz 图片: Tikz 图片描绘了循环神经网络随时间推移的展开

\usepackage{tikz}
\usetikzlibrary{positioning, chains}
\begin{document}
\begin{tikzpicture}[
item/.style={circle,draw,thick,align=center, minimum size=1.2cm},
hidden/.style={item,on chain,join}]

 \begin{scope}[start chain=going right,nodes=hidden,every
 join/.style={-latex,very thick},local bounding box=chain]
 \draw node (A0) {$h_0$} node (A1) {$h_1$} node (A2) {$h_2$} node[xshift=2em] (At)
 {$h_t$};
 \end{scope}
 \node[left=1em of chain,scale=2] (eq) {$=$};
 \node[left=2em of eq,item] (AL) {$h$};
 \path (AL.west) ++ (-1em,2em) coordinate (aux);
 \draw[very thick,-latex,rounded corners] (AL.east) -| ++ (1em,2em) -- (aux)
 |- (AL.west) node[midway, left] {$U$};
 \foreach \X in {0,1,2,t}
 {\draw[very thick,-latex] (A\X.north) -- ++ (0,2em) node[midway, right] {$V$}
 node[above,item,fill=gray!10] (h\X) {$\hat{y}_\X$};
 \draw[very thick,latex-] (A\X.south) -- ++ (0,-2em) node[midway, right] {$W$}
 node[below,item,fill=gray!10] (x\X) {$x_\X$};
 \path (A\X.east) -- (A\X -| At.west) node[midway, above] {$U$};
}
 
 \draw[white,line width=0.8ex] (AL.north) -- ++ (0,1.9em);
 \draw[very thick,-latex] (AL.north) -- ++ (0,2em) node[midway, right] {$V$}
 node[above,item,fill=gray!10] {$\hat{y}_t$};
 \draw[very thick,latex-] (AL.south) -- ++ (0,-2em) node[midway, right] {$W$}
 node[below,item,fill=gray!10] {$x_t$};
 \path (x2) -- (xt) node[midway,scale=2,font=\bfseries] {\dots};
\end{tikzpicture}
\end{document}

我无法定位,U以便它与各自的箭头对齐。有人能给我指出正确的方向吗?

答案1

对于第一个,可以在路径中使用U代码将线段的中间位置放在左侧。-- (aux) --++(0em,-2em) node[midway, left] {$U$} -- (AL.west);U

对于其他的U,可以使用下面的代码来U分别放置每一个。

\path (A0.east) -- (A1.west) node[midway, above] {$U$};
\path (A1.east) -- (A2.west) node[midway, above] {$U$};
\path (A2.east) -- (At.west) node[midway, above] {$U$};

\path (A\X.east) -- (A\X -| At.west) node[midway, above] {$U$};然后就可以删除该线了。

答案2

代码更短,更清晰,边缘标签放置正确。与下面的 OP 代码 MWE 相比,额外使用库arrows.meta(用于更好的箭头)、 ext.paths.orth(用于正交 / r-lr/ 箭头类型)和quotes(边缘标签):

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                ext.paths.ortho,    % defined in the tikz-ext package
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
  start chain = going right,
   arr/.style = {-{Straight Barb[scale=0.8]}, semithick},
  item/.style = {circle, draw, thick, fill=gray!30, minimum size=11mm},
hidden/.style = {item, fill=none,
                 on chain, join=by arr},
                        ]
% from right to left
    \foreach \i in {0,1,2,t}
{
\node (A\i) [hidden] {$h_\i$};
\node (B\i) [item, above=of A\i]  {$\hat{y}_\i$};
\node (C\i) [item, below=of A\i]  {$x_\i$};
\draw[arr]  (A\i) to["$V$" '] (B\i);
\draw[arr]  (A\i) to["$W$"] (C\i);
}
\path   (A0) to ["$V$"] (A1) to ["$V$"] (A2) to ["$V$"] (At);
\draw[thick,densely dashed, white, shorten >=2mm, shorten <=2mm]   (A2) to (At);
% equals sign 
\node (eq) [left=1em of A0, scale=2] {$=$};
% general/common symbol
\node (AL) [item, fill=none, left=2em of eq,] {$h$};
\node (Y) [item, above=of AL]  {$\hat{y}_t$};
\node (X) [item, below=of AL]  {$x_t$};
\draw[arr]  (AL) to["$V$" '] (Y);
\draw[arr]  (AL) to["$W$"]   (X);
\draw[arr, rounded corners]
    (AL.east) -| ++ (1em,2em) r-lr (AL) node[midway, left] {$U$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容