Rosen 的 K-Map 图表

Rosen 的 K-Map 图表

我需要一些帮助来制作 k-map 图。我希望​​它们看起来就像 Rosen 的离散数学文本中呈现的那样。以下是一些示例:

在此处输入图片描述

在此处输入图片描述

知道如何实现吗?我知道 karnaugh-map 包,但我真的很想让图表看起来像图片一样。

答案1

这可以起到类似的作用。您可以使用 Ti您需要用条目填充的 Z 矩阵,并定义一些图片来帮助您添加青色轮廓。

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

在此处输入图片描述

答案2

这是一条很长的评论。我在这里发布一个可能对这类图表有帮助的替代解决方案。我们可以先画一个矩阵(使用上面的 SC 样式),然后根据需要进行装饰。用 Rosen 的离散数学文本中的一张图来说明。需要rounded corners选项和库。fit

在此处输入图片描述

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

相关内容