[Tex/LaTex] How to draw a Karnaugh map

karnaughtablestikz-pgf

Can you please help me drawing the kmap as shown in the link.

enter image description here

A sample of the circles around the 1's is sufficient.

*****Edit****
I made the following code. Can you help me now in completing this diagram?

\begin{tikzpicture}

\matrix (fig) [table,text width=10mm,name=fig]
{
  &   & 1 &   &   & 1 & 1 &  \\ 
  1 & 1 &   & 1 & 1 &   &   & 1\\ 
  1 &   &   &   &   &   &   &  \\ 
  & 1 &   & 1 & 1 &   & 1 & 1\\ 
};

\node (nom) [draw,fit=(fig-2-1)(fig-2-2)]{};
\draw (nom.north)--(-1,2);
\end{tikzpicture}

Edit 09-Jan-13
I came with following solution but it still needs refinement as the legs of the line crosses the X1, X2, etc. needs to be moved away from the map and connects to the line. Also the ellipse is not working ok. Finally, the line crosses X1 needs to be broken not crossing X1 and so on for X2, X3, etc.

Thank you for your support

\tikzset{
table nodes/.style={
    rectangle,
    draw=black,
    align=center,
    minimum height=7mm,
    text depth=0.5ex,
    text height=2ex,
    inner xsep=0pt,
    outer sep=0pt
},      
table/.style={
    matrix of nodes,
    row sep=-\pgflinewidth,
    column sep=-\pgflinewidth,
    nodes={
        table nodes
    },
    execute at empty cell={\node[draw=]{0};}
 }
}

\begin{center}
\begin{tikzpicture}
\matrix (kmap) [every node/.style={anchor=base},table,text width=10mm,name=kmap]
{
 \node (a11){}; & \node (a12){}; & \node (a13){1}; & \node (a14){}; & \node (a15){}; & \node (a16){1}; & \node (a17){1}; & \node (a18){}; \\
 \node (a21){1}; & \node (a22){1}; & \node (a23){}; & \node (a24){1}; & \node (a25){1}; & \node (a26){}; & \node (a27){}; & \node (a28){1}; \\
 \node (a31){1}; & \node (a32){}; & \node (a33){}; & \node (a34){}; & \node (a35){}; & \node (a36){}; & \node (a37){}; & \node (a38){}; \\
 \node (a41){}; & \node (a42){1}; & \node (a43){}; & \node (a44){1}; & \node (a45){1}; & \node (a46){}; & \node (a47){1}; & \node (a48){1}; \\
};

% Right X_1
\draw (a15.north west) -- ++(0,.6) node (lx1) {};
\draw (a18.north east) -- ++(0,.6) node (rx1) {};
\draw (lx1.north) to node[midway]{$X_1$} (rx1.north);

% X2
\draw (a43.south west) -- ++(0,-.3) node (lx2) {};
\draw (a46.south east) -- ++(0,-.3) node (rx2) {};
\draw (lx2.south) to node[midway]{$X_2$} (rx2.south);

% Right X_3
\draw (a16.north west) -- ++(0,.3) node (lx3) {};
\draw (a17.north east) -- ++(0,.3) node (rx3) {};
\draw (lx3.north) to node[midway]{$X_3$} (rx3.north);

% Left X_3
\draw (a12.north west) -- ++(0,.3) node (lx3) {};
\draw (a13.north east) -- ++(0,.3) node (rx3) {};
\draw (lx3.north) to node[midway]{$X_3$} (rx3.north);

% X4
\draw (a31.north west) -- ++(-0.3,0) node (lx4) {};
\draw (a41.south west) -- ++(-0.3,0) node (rx4) {};
\draw (lx4.west) to node[midway]{$X_4$} (rx4.west);

% X5
\draw (a28.north east) -- ++(0.3,0) node (lx5) {};
\draw (a38.south east) -- ++(0.3,0) node (rx5) {};
\draw (lx5.east) to node[midway]{$X_4$} (rx5.east);

\draw[dashed] (a13) circle (.3cm);
\draw (a44.east) ellipse (15pt and 10pt);

\end{tikzpicture}
\end{center}

enter image description here

Best Answer

I highly recommend kvmacros. It isn't your average package which you can simply use with \usepackage{}, instead you have to manually install it, but this should not be a problem.

It is not the most intuitive, but if you play with it a little, you'll get the idea. So, here how the code could look:

\documentclass{article}
\input{kvmacros}

\begin{document}
\karnaughmap{5}{}%
{{$x_1$}{$x_4$}{$x_2$}{$x_5$}{$x_3$}}%
{%
0011011001101000%
0110011011001000%
}%
{%
%Single Ones
\put(2.5,3.5){\oval(0.9,0.9)[]}
\put(1.5,0.5){\oval(0.9,0.9)[]}
\put(7.5,2.5){\oval(0.9,0.9)[]}
\put(0.5,1.5){\oval(0.9,0.9)[]}
%Pairs of Ones
\put(1,2.5){\oval(1.9,0.9)[]}
\put(4,2.5){\oval(1.9,0.9)[]}
\put(6,3.5){\oval(1.9,0.9)[]}
\put(7,0.5){\oval(1.9,0.9)[]}
\put(4,0.5){\oval(1.9,0.9)[]}
}
\end{document}

And most importantly the result:

KV Map

I am assuming every not 1 is 0 (else simply use ~). If you don't need the number in each square, you could just put \kvnoindex in front, for other options just consult the documentation.

EDIT: This might be more than was asked, but here is another example, with a more fancy result.

\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\input{kvmacros}

\begin{document}
\karnaughmap{5}{$f(x_i)$}%
{{$x_1$}{$x_2$}{$x_3$}{$x_4$}{$x_5$}}%
{%
0011011001100110%
0110011001000110%
}%
{%
%Single Ones
\textcolor{Blue}{
\put(2.5,3.5){\oval(0.9,0.9)[]}
\put(7.5,2.5){\oval(0.9,0.9)[]}
\put(0.5,1.5){\oval(0.9,0.9)[]}}
%Pairs of Ones
\put(1,2.5){\oval(1.9,0.9)[]}
%Quadruples of Ones
\textcolor{Yellow}{
\put(4,2){\oval(1.9,1.9)[]}}%
\textcolor{Green}{
\put(6,4){\oval(1.9,1.9)[b]}
\put(6,0){\oval(1.9,1.9)[t]}}%
\textcolor{Red}{
\put(5,0.5){\oval(3.9,0.9)[r]}
\put(3,0.5){\oval(3.9,0.9)[l]}}
}
\end{document}

Here the result:

Output

Have Fun!