2

I was looking at the lecture notes for an online class, and the instructors have some nice pictures for factor graphs and algorithms on factor graphs. One thing that was very helpful were the colored boxes above each node. I was just trying to understand how I would draw something like this. Seems like the nodes would just be circles, as per the the usual tikz base package. But I was not sure how to get those rectangular boxes. Are those from a particular package perhaps?

enter image description here

1
  • 1
    I would define a \newcommand with four args (colours and placement) drawing three square nodes around the nodes. Have a try and come back with your result to get some help.
    – SebGlav
    Mar 9 at 6:04
9

An empty rectangle split can be used as a label for every circle. And connections with a middle square can be done with the markings library.

\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{shapes.multipart, positioning, decorations.markings, arrows.meta}

\begin{document}
\begin{tikzpicture}[
    threeparts/.style={rectangle split, rectangle split parts=3, 
    rectangle split horizontal, 
    draw, rectangle split part fill={#1},
    minimum height=.4cm, outer sep=1mm, inner sep=2pt},
    threeparts/.default={white, white, white},
    mypath/.style={decoration={markings, mark=at position .5 with {\arrow{Square[fill=white]}}},    
                postaction=decorate},
    state/.style={circle, draw, minimum size=1.2cm, fill=#1, anchor=center},
    state/.default=white,
    font=\sffamily
    ]

\node[state=red!30, label={[threeparts={red!30, white, white}]:{}}] (wa) {WA};
\node[state=green!30, label={[threeparts={white, green!30, white}]:{}}, above right=of wa] (nt) {NT};
\node[state, label={[threeparts={white, white, blue!30}]below:{}}, below right=of wa] (sa) {SA};
\node[state, label={[threeparts={red!30, white, blue!30}]:{}}, above right=of sa] (q) {Q};
\node[state, label={[threeparts={red!30, green!30, blue!30}]:{}}, below right=of q] (nsw) {NSW};
\node[state, label={[threeparts={red!30, white, blue!30}]right:{}}, below right=of sa] (v) {V};
\node[state, label={[threeparts={red!30, white, blue!30}]right:{}}, below =of v] (t) {T};

\foreach \i/\j in {wa/nt, wa/sa, nt/q, nt/sa, sa/q, sa/nsw, sa/v, q/nsw, v/nsw} 
    \draw[mypath] (\i) -- (\j);
\end{tikzpicture}
\end{document}

enter image description here

1
  • Didn't know the rectangle split command, very nice and plain solution.
    – SebGlav
    Mar 9 at 18:18
2

An easy suggestion:

\documentclass[border=3.5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    
 \begin{tikzpicture}[thick,node distance=0mm]
    
    %A
    \node at ( 0,0) [circle,draw=blue](A0) {A};
    \node[rectangle,draw=black,fill=green!20,right=of A0,xshift=0.5cm](A1) {};
    \node[rectangle,draw=black,fill=red!20,right=of A1](A2) {};
    \node[rectangle,draw=black,fill=blue!20,right=of A2](A3) {};
    
    %B
    \node at ( 0,3) [circle,draw=blue](B0) {B};
    \node[rectangle,draw=black,fill=green!20,left=of B0,xshift=-0.5cm](B1) {};
    \node[rectangle,draw=black,fill=red!20,left=of B1](B2) {};
    \node[rectangle,draw=black,fill=blue!20,left=of B2](B3) {};
    
    %C
    \node at ( 1,1) [circle,draw=blue](C0) {C};
    \node[rectangle,draw=black,fill=green!20,above=of C0,yshift=0.2cm](C1) {};
    \node[rectangle,draw=black,fill=white!20,left=of C1](C2) {};
    \node[rectangle,draw=black,fill=white!20,right=of C1](C3) {};
    
    \draw [-] (A0) -- (B0);
    \draw [-] (A0) -- (C0);

 \end{tikzpicture}
    
\end{document}

enter image description here

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.