1

I've been looking around the web for solve the following problem, however I've been unable to find a proper solution.

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

I wish to draw arrows as in the following picture as a stand-still with different color choices. enter image description here

from this link.

This question is related to this question, but the proposed solution inserts the arrows as a shape / node, whereas I wish for it to be an arrow/path, adhering to the structure

\draw[tbarrow] (a0) to (a1);

% or 
\draw[tbarrow] (a0) -- (a1);

% or
\path[tbarrow] (a0) --(a1);

Furthermore I want to able to adjust

  1. Adjust edge width of the arrow (within reasonable bounds).
  2. Adjust fill color - white in the above figure.
  3. Adjust edge color - black in the above figure.

Optionally, I also want to edit the arrow head, but there seems to be enough information around the webs that I can figure that out on my own. I apologize in advance as my TiKz experience is somewhat limited.

  • Something like the outlined arrow from here? – user121799 May 22 at 11:45
0

Complete revision. Highly customizable, fillable etc.

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

enter image description here

Older version, goes along curved paths but cannot be filled.

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

enter image description here

And here are rectangled arrows.

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

enter image description here

Depending on the viewer choice you may see faint lines. I chose my viewer such that this happens to inform you about this, and to tell you it is a viewer issue.

  • That seems about right, although I would like my arrow to the rectangle "Loss function" to be angled, instead of curved. I'll try it out and get back to you. – Therbian Granbat May 22 at 11:59
  • Alright, I've tested it and it seems to fulfill nearly all my criteria. I spoke with a few local LaTeX experts and they told me that make the angled arrows is difficult so I'll be happy with your solution for that, thanks! Although I'll probably stick with white and black, I'm curious how I would change these colours in the above configuration? – Therbian Granbat May 22 at 13:16
  • @TherbianGranbat In order to add a color (say blue), replace \draw[decorate] by \draw[blue,decorate]. (I'll try to add rectangled arrows.) – user121799 May 22 at 13:19
  • That works for the edges, but how about the fill colour? \draw[blue,fill=yellow,decorate] only edits the colour of the arrow head and not the whole arrow. – Therbian Granbat May 22 at 13:35
  • @TherbianGranbat I added a version with filled rectangled arrows. – user121799 May 22 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.