4

I am trying to recreate this graph

graph

How do I draw axis-aligned arrows and how can I draw a background?

\documentclass[9pt, landscape, twocolumn]{extarticle}

\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}[English]

\usepackage{bbm}
\usepackage{mathtools}
% \usepackage{mathabx}
\usepackage{dsfont}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
% \usepackage{latexsym}
% \usepackage{array}

\usepackage[colorlinks=true, allcolors=black, breaklinks=true]{hyperref}

% \usepackage{bbding}
\usepackage{pifont}

\usepackage[dvipsnames]{xcolor}
\usepackage[most,listings]{tcolorbox}
\tcbset{colframe=white}

\usepackage{multirow}

% Margins
\usepackage{geometry}
\geometry{
a4paper,
left=0.75in,
right=0.75in,
top=1in,
bottom=1in
}

% Header and footer
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{Juan Da Silva}
\lhead{Machine Learning}
\rfoot{Page \thepage}
\lfoot{TUM}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}

\usepackage{ragged2e}
\justifying

% Titles
\usepackage{titlesec}
\titleformat*{\section}{\normalsize\bfseries}
\titleformat*{\subsection}{\normalsize\bfseries}
\titleformat*{\subsubsection}{\normalsize\bfseries}

% \newcommand{\myparagraph}[1]{\paragraph{\normalsize\bfseries #1}\mbox{}\\}

\setlength{\parskip}{1em}
\setlength{\parindent}{0pt}
\renewcommand{\baselinestretch}{1.25}

% Definitions, theorems, and propositions
% \newtheorem*{definition}{Definition}
% \newtheorem*{theorem}{Theorem}
% \newtheorem*{lemma}{Lemma}
% \newtheorem*{proposition}{Proposition}
% \newtheorem*{notation}{Notation}
% \newtheorem*{corollary}{Corollary}

% Equation counters
% \counterwithin*{equation}{section}
% \counterwithin*{equation}{subsection}

\usepackage{enumitem}
\setlist[itemize]{itemsep=0pt, topsep=0pt}

% Plots
% \usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}

\usepackage{tabularray}

\begin{document}

    {\centering
    \begin{tikzpicture}[baseline={(current bounding box.center)}, yshift=0.0cm, ->, >=Stealth, auto, semithick, node distance=2cm]
        
        \tikzstyle{empty}=[fill=none, draw=none, minimum size=0.75cm]
        \tikzstyle{signstate}=[draw=yellow!75, circle, minimum size=0.25cm, fill=yellow!15, inner sep=0.5pt]
        \tikzstyle{xstate}=[draw=LimeGreen!75, rounded corners, minimum size=0.75cm, fill=LimeGreen!15]
        \tikzstyle{ystate}=[draw=red!75, rounded corners, minimum size=0.75cm, fill=red!15]
        \tikzstyle{state}=[fill=none, draw=none, minimum size=0.5cm]
        \tikzstyle{coord}=[fill=none, draw=none]

        \node[empty] (empty1) {};
        \node[signstate, right=1.5 of empty1] (times1) {$\times$};
        \node[state, above=0.5 of times1] (waa) {$W_{aa}$};
        \node[state, right=0.75 of waa](by){$b_y$};
        \node[state, right=0.75 of by](g2){$g_2$};
        \node[signstate, below=0.15 of g2] (odot1) {$\odot$};
        \node[signstate, below=0.15 of odot1] (plus1) {$+$};
        \node[signstate, right=0.75 of g2] (times2) {$\times$};
        \node[state, right=0.75 of times2] (wya) {$W_{ya}$};
        \node[state, below=0.5 of wya] (g1) {$g_1$};
        \node[signstate, left=0.35 of g1] (odot2) {$\odot$};
        \node[empty, right=1.5 of g1] (empty2) {};
        \node[signstate, below=0.5 of plus1] (times3) {$\times$};
        \node[state, left=0.75 of times3] (wax) {$W_{ax}$};
        \node[state, below=0.5 of g1] (ba) {$b_{a}$};
        \node[ystate, above=0.75 of g2](y){$y^{\langle t\rangle}$};
        \node[xstate, below=0.75 of times3](x){$x^{\langle t\rangle}$};
        \node[coord, left=1 of ba](coordinate1){};
        \node[signstate, left=0.15 of odot2](plus2){$+$};

        \path (empty1) edge node[near start, above] {$a^{\langle t-1 \rangle}$} (times1);
        \path (waa) edge (times1);
        \path (plus1) edge (odot1);
        \path (odot1) edge (g2);
        \path (wya) edge (times2);
        \path (g2) edge (y);
        \path (odot2) edge (g1);
        \path (g1) edge node[near end, above] {$a^{\langle t \rangle}$} (empty2);
        \path (wax) edge (times3);
        \path (x) edge (times3);
        \path (times3) edge (plus2);

    \end{tikzpicture}
    \par}
\end{document}
3
  • Note that you shouldn't use \tikzstyle any more. Use \tikzset instead. Are you looking for |- and -|? Not sure what you mean by 'axis aligned'. Are you trying to change a tcolorbox background or add a background within the tikzpicture? If the latter, why include tcolorbox in your example here?
    – cfr
    Commented May 26 at 20:01
  • @cfr Exactly, I mean -| and |-. But I don't know why it is not drawing it. I am trying to add a background within the tikzpicture as in the photo
    – juan19.99
    Commented May 26 at 20:19
  • You can't use it with edge? Is that the issue?
    – cfr
    Commented May 26 at 20:57

1 Answer 1

6

I'd recommend the |- and -| syntax, with the extended versions from tikzext for the lines. For the background, use backgrounds and fit to fit the background to the nodes you want it to contain.

For example (with minor positioning adjustments),

\PassOptionsToPackage{dvipsnames}{xcolor}
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{arrows.meta,positioning,ext.paths.ortho,backgrounds,fit}
\begin{document}
\begin{tikzpicture}[%
    baseline={(current bounding box.center)}, 
    yshift=0.0cm, 
    ->, 
    >=Stealth, 
    auto, 
    semithick, 
    node distance=2cm,
    empty/.style={fill=none, draw=none, minimum size=0.75cm},
    signstate/.style={draw=yellow!75, circle, minimum size=0.25cm, fill=yellow!15, inner sep=0.5pt},
    xstate/.style={draw=LimeGreen!75, rounded corners, minimum size=0.75cm, fill=LimeGreen!15},
    ystate/.style={draw=red!75, rounded corners, minimum size=0.75cm, fill=red!15},
    state/.style={fill=none, draw=none, minimum size=0.5cm},
    coord/.style={fill=none, draw=none},
  ]
  
  \node[empty] (empty1) {};
  \node[signstate, right=1.5 of empty1] (times1) {$\times$};
  \node[state, above=0.5 of times1] (waa) {$W_{aa}$};
  \node[state, right=0.75 of waa](by){$b_y$};
  \node[state, above right=0 and 0.75 of by](g2){$g_2$};
  \node[signstate, below=0.35 of g2] (odot1) {$\odot$};
  \node[signstate] (plus1) at (odot1 |- times1) {$+$};
  \node[signstate] [](times2) at ([xshift=7.5mm]g2.east |- waa) {$\times$};
  \node[state, right=0.75 of times2] (wya) {$W_{ya}$};
  \node[state] (g1) at (wya |- times1) {$g_1$};
  \node[signstate, left=0.35 of g1] (odot2) {$\odot$};
  \node[empty, right=1.5 of g1] (empty2) {};
  \node[signstate, below=0.5 of plus1] (times3) {$\times$};
  \node[state, left=0.75 of times3] (wax) {$W_{ax}$};
  \node[state, below=0.5 of g1] (ba) {$b_{a}$};
  \node[ystate, above=0.75 of g2](y){$y^{\langle t\rangle}$};
  \node[xstate, below=0.75 of times3](x){$x^{\langle t\rangle}$};
  \node[coord, left=1 of ba](coordinate1){};
  \node[signstate, below left=0.15 of odot2](plus2){$+$};
  
  \path (empty1) edge node[near start, above] {$a^{\langle t-1 \rangle}$} (times1);
  \path (waa) edge (times1);
  \path (plus1) edge (odot1);
  \path (odot1) edge (g2);
  \path (wya) edge (times2);
  \path (g2) edge (y);
  \path (odot2) edge (g1);
  \path (g1) edge coordinate [pos=.2] (c1)  node[near end, above] {$a^{\langle t \rangle}$} (empty2);
  \path (wax) edge (times3);
  \path (x) edge (times3);
  \path [draw] (times3) -| (plus2);
  \foreach \i/\j in {ba/plus2,plus2/odot2,times1/plus2,by/plus1}
  \path [draw] (\i) |- (\j);
  \foreach \i/\j in {times2/plus1}
  \path [draw] (\i) -|- (\j);
  \foreach \i/\j in {times2/c1}
  \path [draw,-] (\i) |-| (\j);
  \scoped[on background layer]{\node [fit=(waa) (wya) (ba) (wax) (g2) (c1),fill=blue!15,draw=blue!30,rounded corners] {};}
\end{tikzpicture}
\end{document}

fitted background and orthogonal lines

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .