我一直在网上寻找解决以下问题的方法,但是我一直无法找到合适的解决方案。
\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning}
\begin{document}
\begin{tikzpicture}
\tikzstyle{tbcircle}=[circle,minimum size=25pt,inner sep=0pt]
\tikzstyle{forward tbcircle}=[tbcircle,fill=green!50]
\tikzstyle{backward tbcircle}=[tbcircle,fill=red!50]
\tikzstyle{tbsquare}=[rectangle,rounded corners,minimum width=25pt,minimum height=25pt,fill=orange!50]
\tikzstyle{tbarrow} = [line width=1.5mm, draw=blue, -triangle 45]
\coordinate (a0) at (0,0);
\coordinate (e1) at (2,0); % horizontal spacing between layers/nodes
\coordinate (e2) at (0,4); % vertical spacing between forward/backward pass
\coordinate (labelshift) at (0,1);
% secondary points
\foreach \y in {0,...,4}{
\ifnum\y>0\relax%
\coordinate (a\y) at ($(a0)+\y*(e1)$);
\else%
%
\fi
\node[forward tbcircle,draw] (A\y) at (a\y) {$a^{[\y]}$};
\coordinate (b\y) at ($(a0)-(e2)+\y*(e1)$);
\node[backward tbcircle,draw] (B\y) at (b\y) {$b^{[\y]}$};
}
% draw final square
\coordinate (sq) at ($(a4)!0.5!(b4)+(e1)$);
\node[tbsquare,anchor=west] (SQ) at (sq) {Loss function};
% draw arrows
\foreach \y/\x in {0/1,1/2,2/3,3/4}{
\path[tbarrow] (A\y) -- (A\x);
}
\foreach \y/\x in {4/3,3/2,2/1,1/0}{
\path[tbarrow] (B\y) -- (B\x);
}
% draw angled arrow
\path[tbarrow] (A4) -| ($(A4)+0.5*(e1)$) |- ($(SQ.north west)!1/3!(SQ.south west)$);
\path[tbarrow] ($(SQ.north west)!2/3!(SQ.south west)$) -| ($(SQ.north west)!2/3!(SQ.south west)-0.5*(e1)$) |- (B4);
% draw titles
\node[] (forwardpasstitle) at ($(a2)+(labelshift)$) {Forward pass};
\node[] (backwardspasstitle) at ($(b2)-(labelshift)$) {Backward propagation};
\end{tikzpicture}
\end{document}
从此链接。
这个问题与这个问题但建议的解决方案将箭头作为形状/节点插入,而我希望它是一个箭头/路径,遵循结构
\draw[tbarrow] (a0) to (a1);
% or
\draw[tbarrow] (a0) -- (a1);
% or
\path[tbarrow] (a0) --(a1);
此外,我希望能够调整
- 调整箭头的边缘宽度(在合理范围内)。
- 调整上图中的填充颜色-白色。
- 调整边缘颜色-上图中的黑色。
或者,我还想编辑箭头,但网上似乎有足够的信息让我自己弄清楚。我提前道歉,因为我的 TiKz 经验有限。
答案1
全面修订。高度可定制、可填写等。
\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning,arrows.meta}
\begin{document}
\begin{tikzpicture}[tbcircle/.style={circle,minimum size=25pt,inner sep=0pt},
forward tbcircle/.style={tbcircle,fill=green!50},
backward tbcircle/.style={tbcircle,fill=red!50},
tbsquare/.style={rectangle,rounded corners,minimum width=25pt,minimum height=25pt,fill=orange!50},
tbcarrow/.style={/utils/exec=\tikzset{tbcarrow pars/.cd,#1},
line width=\pgfkeysvalueof{/tikz/tbcarrow pars/width},
draw=\pgfkeysvalueof{/tikz/tbcarrow pars/draw},
-{Triangle[line width=\pgfkeysvalueof{/tikz/tbcarrow pars/line width},
length=1.25*\pgfkeysvalueof{/tikz/tbcarrow pars/width},
width=2.5*\pgfkeysvalueof{/tikz/tbcarrow pars/width},
fill=\pgfkeysvalueof{/tikz/tbcarrow pars/fill}]},
postaction={draw=\pgfkeysvalueof{/tikz/tbcarrow pars/fill},-,
line width=\pgfkeysvalueof{/tikz/tbcarrow pars/width}-2*\pgfkeysvalueof{/tikz/tbcarrow pars/line width},
shorten <=\pgfkeysvalueof{/tikz/tbcarrow pars/line width},
shorten >=1.25*\pgfkeysvalueof{/tikz/tbcarrow pars/width}-1.5*\pgfkeysvalueof{/tikz/tbcarrow pars/line width}}
},
tbcarrow pars/.cd,
fill/.initial=white,draw/.initial=black,width/.initial=4pt,
line width/.initial=0.4pt,]
\coordinate (a0) at (0,0);
\coordinate (e1) at (2,0); % horizontal spacing between layers/nodes
\coordinate (e2) at (0,4); % vertical spacing between forward/backward pass
\coordinate (labelshift) at (0,1);
% secondary points
\foreach \y in {0,...,4}{
\ifnum\y>0\relax%
\coordinate (a\y) at ($(a0)+\y*(e1)$);
\else%
%
\fi
\node[forward tbcircle,draw] (A\y) at (a\y) {$a^{[\y]}$};
\coordinate (b\y) at ($(a0)-(e2)+\y*(e1)$);
\node[backward tbcircle,draw] (B\y) at (b\y) {$b^{[\y]}$};
}
% draw final square
\coordinate (sq) at ($(a4)!0.5!(b4)+(e1)$);
\node[tbsquare,anchor=west] (SQ) at (sq) {Loss function};
% draw arrows
\foreach \y/\x in {0/1,1/2,2/3,3/4}{
\draw[tbcarrow={fill=blue}] (A\y) -- (A\x);
}
\foreach \y/\x in {4/3,3/2,2/1,1/0}{
\draw[tbcarrow={fill=blue}] (B\y) -- (B\x);
}
% draw angled arrow
\draw[tbcarrow={fill=red,draw=blue}] (A4) -| ($(A4)+0.5*(e1)$) |- ($(SQ.north west)!1/3!(SQ.south west)$);
\draw[tbcarrow={fill=red,draw=orange,width=6pt,line width=1pt}] ($(SQ.north west)!2/3!(SQ.south west)$) -| ($(SQ.north west)!2/3!(SQ.south west)-0.5*(e1)$) |- (B4);
%
% draw titles
\node[] (forwardpasstitle) at ($(a2)+(labelshift)$) {Forward pass};
\node[] (backwardspasstitle) at ($(b2)-(labelshift)$) {Backward propagation};
\end{tikzpicture}
\end{document}
旧版本,沿着弯曲的路径行进但无法填充。
\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning}
\usetikzlibrary{decorations}
\pgfkeys{/tikz/.cd,
outlined arrow width/.store in=\OutlinedArrowWidth,
outlined arrow width=8pt,
outlined arrow step/.store in=\OutlinedArrowStep,
outlined arrow step=1pt,
outlined arrow length/.store in=\OutlinedArrowLength,
outlined arrow length=5pt,
}
\pgfdeclaredecoration{outlined arrow}{initial}
{% initial arrow butt
\state{initial}[width=\OutlinedArrowStep,next state=cont] {
\pgfmoveto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
\pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{\OutlinedArrowWidth/2}}
\pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{-\OutlinedArrowWidth/2}}
\pgfpathlineto{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
\pgfcoordinate{lastup}{\pgfpoint{1pt}{\OutlinedArrowWidth/2}}
\pgfcoordinate{lastdown}{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
\xdef\marmotarrowstart{0}
}
\state{cont}[width=\OutlinedArrowStep]{
\ifdim\pgfdecoratedremainingdistance>\OutlinedArrowLength% continue the outlined path
\pgfmoveto{\pgfpointanchor{lastup}{center}}
\pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
\pgfcoordinate{lastup}{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
\pgfmoveto{\pgfpointanchor{lastdown}{center}}
\pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
\pgfcoordinate{lastdown}{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
\else
\ifnum\marmotarrowstart=0% draw the arrow head
\pgfmoveto{\pgfpointadd{\pgfpointanchor{lastup}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
\pgflineto{\pgfpoint{-0.5\pgflinewidth}{\OutlinedArrowWidth}}
\pgflineto{\pgfpointadd{\pgfpointdecoratedpathlast}{\pgfpoint{-0.5\pgflinewidth}{0}}}
\pgflineto{\pgfpoint{-0.5\pgflinewidth}{-\OutlinedArrowWidth}}
\pgflineto{\pgfpointadd{\pgfpointanchor{lastdown}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
\xdef\marmotarrowstart{1}
\else
\fi
\fi%
}
\state{final}[width=5pt]
{ % perhaps unnecessary but doesn't hurt either
\pgfmoveto{\pgfpointdecoratedpathlast}
}
}
\begin{document}
\begin{tikzpicture}[tbcircle/.style={circle,minimum size=25pt,inner sep=0pt},
forward tbcircle/.style={tbcircle,fill=green!50},
backward tbcircle/.style={tbcircle,fill=red!50},
tbsquare/.style={rectangle,rounded corners,minimum width=25pt,minimum height=25pt,fill=orange!50},
tbarrow/.style={line width=1.5mm, draw=blue, -triangle 45},decoration=outlined arrow,]
\coordinate (a0) at (0,0);
\coordinate (e1) at (2,0); % horizontal spacing between layers/nodes
\coordinate (e2) at (0,4); % vertical spacing between forward/backward pass
\coordinate (labelshift) at (0,1);
% secondary points
\foreach \y in {0,...,4}{
\ifnum\y>0\relax%
\coordinate (a\y) at ($(a0)+\y*(e1)$);
\else%
%
\fi
\node[forward tbcircle,draw] (A\y) at (a\y) {$a^{[\y]}$};
\coordinate (b\y) at ($(a0)-(e2)+\y*(e1)$);
\node[backward tbcircle,draw] (B\y) at (b\y) {$b^{[\y]}$};
}
% draw final square
\coordinate (sq) at ($(a4)!0.5!(b4)+(e1)$);
\node[tbsquare,anchor=west] (SQ) at (sq) {Loss function};
% draw arrows
\foreach \y/\x in {0/1,1/2,2/3,3/4}{
\draw[decorate] (A\y) -- (A\x);
}
\foreach \y/\x in {4/3,3/2,2/1,1/0}{
\draw[decorate] (B\y) -- (B\x);
}
% draw angled arrow
\draw[decorate] (A4) to[out=0,in=180] ($(SQ.north west)!1/3!(SQ.south west)$);
\draw[decorate] ($(SQ.north west)!2/3!(SQ.south west)$) to[out=180,in=0] (B4);
% draw titles
\node[] (forwardpasstitle) at ($(a2)+(labelshift)$) {Forward pass};
\node[] (backwardspasstitle) at ($(b2)-(labelshift)$) {Backward propagation};
\end{tikzpicture}
\end{document}
这是矩形箭头。
\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning}
\usetikzlibrary{decorations}
\pgfkeys{/tikz/.cd,
outlined arrow width/.store in=\OutlinedArrowWidth,
outlined arrow width=8pt,
outlined arrow step/.store in=\OutlinedArrowStep,
outlined arrow step=1pt,
outlined arrow length/.store in=\OutlinedArrowLength,
outlined arrow length=5pt,
}
\pgfdeclaredecoration{outlined arrow}{initial}
{% initial arrow butt
\state{initial}[width=\OutlinedArrowStep,next state=cont] {
\pgfmoveto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
\pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{\OutlinedArrowWidth/2}}
\pgfpathlineto{\pgfpoint{0.3\pgflinewidth}{-\OutlinedArrowWidth/2}}
\pgfpathlineto{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
\pgfcoordinate{lastup}{\pgfpoint{1pt}{\OutlinedArrowWidth/2}}
\pgfcoordinate{lastdown}{\pgfpoint{1pt}{-\OutlinedArrowWidth/2}}
\xdef\marmotarrowstart{0}
}
\state{cont}[width=\OutlinedArrowStep]{
\ifdim\pgfdecoratedremainingdistance>\OutlinedArrowLength% continue the outlined path
\pgfmoveto{\pgfpointanchor{lastup}{center}}
\pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
\pgfcoordinate{lastup}{\pgfpoint{\OutlinedArrowStep}{\OutlinedArrowWidth/2}}
\pgfmoveto{\pgfpointanchor{lastdown}{center}}
\pgfpathlineto{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
\pgfcoordinate{lastdown}{\pgfpoint{\OutlinedArrowStep}{-\OutlinedArrowWidth/2}}
\else
\ifnum\marmotarrowstart=0% draw the arrow head
\pgfmoveto{\pgfpointadd{\pgfpointanchor{lastup}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
\pgflineto{\pgfpoint{-0.5\pgflinewidth}{\OutlinedArrowWidth}}
\pgflineto{\pgfpointadd{\pgfpointdecoratedpathlast}{\pgfpoint{-0.5\pgflinewidth}{0}}}
\pgflineto{\pgfpoint{-0.5\pgflinewidth}{-\OutlinedArrowWidth}}
\pgflineto{\pgfpointadd{\pgfpointanchor{lastdown}{center}}{\pgfpoint{-0.5\pgflinewidth}{0}}}
\xdef\marmotarrowstart{1}
\else
\fi
\fi%
}
\state{final}[width=5pt]
{ % perhaps unnecessary but doesn't hurt either
\pgfmoveto{\pgfpointdecoratedpathlast}
}
}
\begin{document}
\begin{tikzpicture}[tbcircle/.style={circle,minimum size=25pt,inner sep=0pt},
forward tbcircle/.style={tbcircle,fill=green!50},
backward tbcircle/.style={tbcircle,fill=red!50},
tbsquare/.style={rectangle,rounded corners,minimum width=25pt,minimum height=25pt,fill=orange!50},
tbarrow/.style={line width=1.5mm, draw=blue, -triangle 45},decoration=outlined arrow,]
\coordinate (a0) at (0,0);
\coordinate (e1) at (2,0); % horizontal spacing between layers/nodes
\coordinate (e2) at (0,4); % vertical spacing between forward/backward pass
\coordinate (labelshift) at (0,1);
% secondary points
\foreach \y in {0,...,4}{
\ifnum\y>0\relax%
\coordinate (a\y) at ($(a0)+\y*(e1)$);
\else%
%
\fi
\node[forward tbcircle,draw] (A\y) at (a\y) {$a^{[\y]}$};
\coordinate (b\y) at ($(a0)-(e2)+\y*(e1)$);
\node[backward tbcircle,draw] (B\y) at (b\y) {$b^{[\y]}$};
}
% draw final square
\coordinate (sq) at ($(a4)!0.5!(b4)+(e1)$);
\node[tbsquare,anchor=west] (SQ) at (sq) {Loss function};
% draw arrows
\foreach \y/\x in {0/1,1/2,2/3,3/4}{
\draw[decorate] (A\y) -- (A\x);
}
\foreach \y/\x in {4/3,3/2,2/1,1/0}{
\draw[decorate] (B\y) -- (B\x);
}
% draw angled arrow
\draw[decorate] ($(SQ.north west)!1/4!(SQ.south west)+(-0.4,0)$) coordinate
(auxA) -- ($(SQ.north west)!1/4!(SQ.south west)$);
\draw[double, double distance=8pt-\pgflinewidth] (A4.east) -- ++ (0.6,0) |-
([xshift=1pt]auxA);
\draw[decorate] ([xshift=4mm]B4.east)coordinate(auxB) -- (B4.east);
\draw[double, double distance=8pt-\pgflinewidth] ($(SQ.north west)!3/4!(SQ.south west)$)
-- ++ (-0.4,0) |-
([xshift=-1pt]auxB);
%
% draw titles
\node[] (forwardpasstitle) at ($(a2)+(labelshift)$) {Forward pass};
\node[] (backwardspasstitle) at ($(b2)-(labelshift)$) {Backward propagation};
\end{tikzpicture}
\end{document}
根据查看器的选择,您可能会看到模糊的线条。我选择查看器时,这种情况会发生,以通知您有关此问题,并告诉您这是查看器问题。