K-Map Diagrams from Rosen

This does something of that sort. You can use a TikZ matrix that you need to fill with entries, and define some pics to help you add the cyan contours.

\documentclass{article}
\usepackage[bottom=4in]{geometry} % just for the example
\usepackage{subcaption}
\usepackage{tikz}
\makeatletter
\pgfmathdeclarefunction{Dim}{1}{%
\begingroup%
\pgfutil@tempcnta0%
\@for\pgfutil@tempa:=#1\do{\advance\pgfutil@tempcnta1}%
\edef\pgfmathresult{\the\pgfutil@tempcnta}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\makeatletter
\usetikzlibrary{calc,matrix,fit}
\tikzset{k-map diagram/.style={matrix of math nodes,nodes in empty cells,
    nodes={draw,minimum size=1.8em,anchor=center},
    column 1/.style={nodes={draw=none,minimum size=0em,text=cyan,font=\tiny}},
    row 1/.style={nodes={draw=none,minimum size=0em,text=cyan,font=\tiny}},
    column sep=-0.5*\pgflinewidth,row sep=-0.5*\pgflinewidth,
    append after command={let 
        \p1=($(\tikzlastnode-2-2.west)-(\tikzlastnode.west)$) in
     (\tikzlastnode.east)++ (\x1,0)}
    },pics/ofit/.style={code={
     \pgfmathtruncatemacro{\mydim}{Dim("#1")-1}
     \ifcase\mydim
      \draw[cyan] #1 circle[radius=1.5ex];
     \or
      \def\mysplit##1,##2;{\edef\myA{##1}\edef\myB{##2}}
      \mysplit#1;
      \draw[cyan] let \p1=\myA,\p2=\myB,\n1={atan2(\y2-\y1,\x2-\x1)}
       in 
       ($ (\p1)!1.5ex!90:(\p2) $) 
       arc[start angle=\n1+90,end angle=\n1+270,radius=1.5ex] --
        ($ (\p2)!1.5ex!90:(\p1) $) 
       arc[start angle=\n1-90,end angle=\n1+90,radius=1.5ex] -- cycle;
     \fi
    }},
    pics/cfit/.style={code={
     \pgfmathtruncatemacro{\mydim}{Dim("#1")-1}
     \ifcase\mydim
      \draw[cyan] ($#1+(0,1.5ex)$)
        arc[start angle=90,end angle=-90,radius=1.5ex]
        -- ++ (-\pgfkeysvalueof{/tikz/cfit/arm},0) -- ++ (0,3ex) -- cycle;
      \or
       \def\mysplit##1,##2;{\edef\myA{##1}\edef\myB{##2}}
      \mysplit#1;
      \draw[cyan] let \p1=\myA,\p2=\myB,\n1={-1*atan2(\y2-\y1,\x2-\x1)}
       in 
        ($(\p1)+(\n1:1.5ex)$)
        arc[start angle=\n1,end angle=\n1-90,radius=1.5ex]
        --
        ($(\p2)+(\n1-90:1.5ex)$)
        arc[start angle=\n1-90,end angle=\n1-180,radius=1.5ex]
        -- ++ (\n1+90:\pgfkeysvalueof{/tikz/cfit/arm}) 
        -- ($(\p1)+(\n1:1.5ex)+(\n1+90:\pgfkeysvalueof{/tikz/cfit/arm})$)
        -- cycle;
      \fi 
    }},cfit/.cd,arm/.initial=0.9em}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}{.2\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram]{
       & y & \bar y \\
     x & 1 & \\
     \bar x & 1 & \\
    };
    \end{tikzpicture}
    \caption{}
\end{subfigure}\hspace{2em}
\begin{subfigure}{.2\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram]{
       & y & \bar y \\
     x &  & 1 \\
     \bar x & 1 & \\
    };
    \end{tikzpicture}
    \caption{}
\end{subfigure}\hspace{2em}
\begin{subfigure}{.2\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram]{
       & y & \bar y \\
     x &  & 1\\
     \bar x & 1 & 1\\
    };
    \end{tikzpicture}
    \caption{}
\end{subfigure}
\end{figure}
%
\begin{figure}
\centering
\begin{subfigure}{.2\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram] (kmat){
       & y & \bar y \\
     x & 1 & \\
     \bar x & 1 & \\
    };
    \pic{ofit={(kmat-2-2),(kmat-3-2)}};
    \end{tikzpicture}
    \caption{}
\end{subfigure}\hspace{2em}
\begin{subfigure}{.2\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram] (kmat) {
       & y & \bar y \\
     x &  & 1 \\
     \bar x & 1 & \\
    };
    \path pic{ofit={(kmat-2-3)}} pic{ofit={(kmat-3-2)}};
    \end{tikzpicture}
    \caption{}
\end{subfigure}\hspace{2em}
\begin{subfigure}{.2\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram]{
       & y & \bar y \\
     x &  & 1\\
     \bar x & 1 & 1\\
    };
    \path pic{ofit={(kmat-2-3),(kmat-3-3)}} pic{ofit={(kmat-3-2),(kmat-3-3)}};
    \end{tikzpicture}
    \caption{}
\end{subfigure}
\end{figure}
%
\begin{figure}
\centering
\begin{subfigure}{.3\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram] (kmat){
       & yz & y\bar z& \bar y z & \bar y\bar z\\
     x & & 1 & 1 &  \\
     \bar x & 1 & & 1 & \\
    };
    \path pic{ofit={(kmat-2-3),(kmat-2-4)}}
     pic{ofit={(kmat-2-4),(kmat-3-4)}}
     pic{ofit={(kmat-3-2)}};
    \end{tikzpicture}
    \caption{}
\end{subfigure}\hspace{2em}
\begin{subfigure}{.3\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram] (kmat){
       & yz & y\bar z& \bar y z & \bar y\bar z\\
     x & & 1 & 1 &  1\\
     \bar x & 1 & & 1 & 1\\
    };
    \node[rounded corners=1.5ex,fit=(kmat-2-4.center)(kmat-3-5.center),
        inner sep=1.5ex,draw=cyan]{};
    \path 
    pic{cfit={(kmat-3-2)}} pic[rotate=180]{cfit={(kmat-3-5)}};
    \end{tikzpicture}
    \caption{}
\end{subfigure}
\end{figure}
%
\begin{figure}
\centering
\begin{subfigure}{.3\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram] (kmat){
       & yz & y\bar z& \bar y z & \bar y\bar z\\
     x & 1& 1 & 1 & 1 \\
     \bar x & 1 & & 1 & 1 \\
    };
    \path pic{ofit={(kmat-2-2),(kmat-2-5)}}
     pic{ofit={(kmat-2-5),(kmat-3-5)}}
     pic{cfit={(kmat-2-2),(kmat-3-2)}}
     pic[cfit/arm=2.7em]{cfit={(kmat-3-4),(kmat-2-4)}};
    \end{tikzpicture}
    \caption{}
\end{subfigure}\hspace{2em}
\begin{subfigure}{.3\textwidth}
\centering
    \begin{tikzpicture}
    \node[k-map diagram] (kmat){
       & yz & y\bar z& \bar y z & \bar y\bar z\\
     x &  & 1 & 1 &  \\
     \bar x &  & & 1 & 1\\
    };
    \path pic{ofit={(kmat-2-3),(kmat-2-4)}}
     pic{ofit={(kmat-2-4),(kmat-3-4)}}
     pic{ofit={(kmat-3-4),(kmat-3-5)}};
    \end{tikzpicture}
    \caption{}
\end{subfigure}
\end{figure}
\end{document}

enter image description here


This is a long comment. I post here for an alternative solution that may be helpful for these kind of diagrams. We can first draw a matrix (with the above SC's style), then decorate as desired. Illustrated with a figure from Rosen's Discrete Mathematics text. rounded corners option and fit library are needed.

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix,fit}
\tikzset{kmap/.style={ %% from Schrödinger's cat
matrix of math nodes,nodes in empty cells,
nodes={draw,minimum size=8mm,anchor=center},
column 1/.style={nodes={draw=none,minimum size=0,text=cyan,font=\tiny}},
row 1/.style={nodes={draw=none,minimum size=0,text=cyan,font=\tiny}},
column sep=-.5*\pgflinewidth,
row sep=-.5*\pgflinewidth
}
}
\begin{document}
\begin{tikzpicture}
\node[kmap] (km){
                &yz &y\bar z&\bar y\bar z&\bar y z\\
    wx          &   & 1     &1           &        \\
    w\bar x     & 1 & 1     & 1          &        \\
    \bar w\bar x&   & 1     & 1          &        \\
    \bar w x    & 1 & 1     & 1          & 1      \\
};

\draw[cyan] (km-5-2.center) circle(3mm);

\node[draw=cyan,rounded corners,inner sep=-1mm,rounded corners=3mm,fit={(km-3-2) (km-3-3)}]{};

\node[draw=cyan,rounded corners,inner sep=-1mm,rounded corners=3mm,fit={(km-2-3) (km-2-4) (km-5-3) (km-5-4)}]{};

\begin{scope}
\clip (km-1-1.north west) rectangle (km-5-5.south east);
\path (km-5-5)+(0:1) node (km-5-5t){};
\node[draw=cyan,rounded corners,inner sep=-1mm,rounded corners=3mm,fit={(km-5-5) (km-5-5t)}]{};
\end{scope}
\end{tikzpicture}
\end{document} 

Tags:

Diagrams