0

Many questions have already asked how to add a loop to a finite state machine-like plot, but I can't find an answer specifically regarding bayesnet. Can someone tell me how to add a self-loop to a node in bayesnet?

Something like this: how do I draw a self loop and also label graph edges in Tikz?

Edit: Added sample code. One problem is that the loop and the plate's bottom text don't both fit in the plate.

    \begin{figure}
        \centering
        \tikz{
        \node[latent] (b_t) {$b_t$} ;
        \node[latent, right=of b_t] (t_t)
        {$t_t$} ;
        \node[obs, right=of t_t] (o_t) {$o_t$} ;
        % \edge {b_t} {b_t} ;
        \edge {b_t} {t_t} ;
        \edge {t_t} {o_t} ;
        \path (b_t) edge [loop above] (b_t);
        \plate[inner sep=0.225cm, yshift=.25cm] {plate1} {(b_t) (t_t) (o_t) (b_t')} {T}; %
    }
    \end{figure}

enter image description here

1
  • 1
    Welcome! If you provide us with a sample code, you will get very quickly an answer. Otherwise you expect others to make up some code, and this can take long.
    – user194703
    Mar 21, 2020 at 22:51

1 Answer 1

1

The loops work as usual. I just copied a random example and added a loop.

\documentclass[tikz,border=3mm]{standalone} 
\usetikzlibrary{arrows.meta,bending}
\usetikzlibrary{bayesnet}

\begin{document}

\begin{tikzpicture}[>={Stealth[bend]}]
% nodes
 \node[obs] (x) {$x$};%
 \node[latent,above=of x,xshift=-1cm,path picture={\fill[gray!25] (path picture bounding box.south) rectangle (path picture bounding box.north west);}] 
(y) {$y$}; %
 \node[latent,above=of x,xshift=1cm] (z) {$z$}; %
 \path (z) edge [loop above] node (z') {$Z$} (z);

% plate
\plate[inner sep=.25cm,yshift=.2cm,transform shape=false]{plate1}{(x)(y)(z)(z')}{$N$}; %

% edges
 \edge {y,z} {x}  
\end{tikzpicture}

\end{document}

enter image description here

ADDENDUM: As for the code from your update: add an auxiliary coordinate for the fit.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{bayesnet}
\begin{document}
\begin{figure}
        \centering
        \begin{tikzpicture}
        \node[latent] (b_t) {$b_t$} ;
        \node[latent, right=of b_t] (t_t)
        {$t_t$} ;
        \node[obs, right=of t_t] (o_t) {$o_t$} ;
        % \edge {b_t} {b_t} ;
        \edge {b_t} {t_t} ;
        \edge {t_t} {o_t} ;
        \path (b_t) edge [loop above] coordinate[midway](aux) (b_t);
        \plate[inner sep=0.225cm] {plate1}
         {(b_t) (t_t) (o_t) (aux)} {T}; %
\end{tikzpicture}
\caption{Adding an auxiliary coordinate.}
\end{figure}

\begin{figure}
\centering
 \begin{tikzpicture}
  \begin{scope}[local bounding box=pft]
        \node[latent] (b_t) {$b_t$} ;
        \node[latent, right=of b_t] (t_t)
        {$t_t$} ;
        \node[obs, right=of t_t] (o_t) {$o_t$} ;
        % \edge {b_t} {b_t} ;
        \edge {b_t} {t_t} ;
        \edge {t_t} {o_t} ;
        \path (b_t) edge [loop above] (b_t);
  \end{scope}       
  \plate[inner sep=0.225cm] {plate1} {(pft)} {T}; %
\end{tikzpicture}
\caption{Using a \texttt{local bounding box} node. (Note that this will probably improve
at a given point when the \texttt{bbox} library gets improved. Currently
development is frozen.)}
\end{figure}
\end{document}

enter image description here

7
  • How did you find the syntax for \path? Mar 23, 2020 at 1:08
  • @RylanSchaeffer I do not understand the question. This is all in the pgfmanual. \path is (sort of) the main command, and \draw is just a shorthand for \path[draw], \fill for \path[fill] and so on (up to some subtleties which are not important here).
    – user194703
    Mar 23, 2020 at 1:12
  • I'm confused by z'. What is this node? I'm currently running into the program that if I don't include your Z, my plate isn't large enough to include the loop and the plate value (e.g. N). Mar 23, 2020 at 1:15
  • @RylanSchaeffer z' is just the name of the node of content Z that is above the loop. It is not really used here.
    – user194703
    Mar 23, 2020 at 1:17
  • 1
    @RylanSchaeffer The IMHO cleaner way is to add an auxiliary coordinate, see the update. What I said above was not entirely correct. I did use z' for the fit, just forgot about it. Similarly you can add a coordinate at the outermost coordinate of the fit. (You could simplify everything by using a local bounding box, I guess.)
    – user194703
    Mar 23, 2020 at 1:26

You must log in to answer this question.

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