# How can I draw a Karnaugh map

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.

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


-
We can certainly help if you can provide the basic parts of the drawing. Setting up from scratch is really not fun. –  percusse Dec 19 '12 at 22:41
Check this answer: tex.stackexchange.com/a/36879/14100 for ways of drawing Karnaugh maps. –  Scott H. Dec 19 '12 at 23:05
If the link above sufficiently answers your question, then let us know. –  Scott H. Dec 19 '12 at 23:13
I agree. Starting from scratch is not fun. I am sorry. I will do something based on Scott's post then I will post it. –  OOzy Pal Dec 20 '12 at 13:28

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:

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)[]}
\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:

Have Fun!

-
Sorry I was busy with finals. Thank you so much for your help. I used your solution to show the Prof. that the paper is coming along ok. The problem with kvmacros is that it stacks x1, x2, x3, etc on the top of each other making the figure too hight. –  OOzy Pal Jan 9 '13 at 21:05

Finally, I figured it out. It is exactly the same as my question but I got the idea. Thank you all.

\tikzset{
table nodes/.style={
rectangle,
draw=black,
align=center,
minimum height=0.75cm,
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
}
}
}

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

% X1
\draw (0,1.55) -- ++(0,5mm) -- ++(4,0) node[above=-1mm, midway] {$x_1$} -- ++(0,-.45);

% X2
\draw (-2,-1.6) -- ++(0,-1.5mm) -- ++(4,0) node[below, midway] {$x_2$} -- ++(0,1.5mm);

%X3
\draw (-3,1.55) -- ++(0,1.5mm) -- ++(2,0) node[above=-1mm, midway] {$x_3$} -- ++(0,-1.5mm);
\draw (1,1.55) -- ++(0,1.5mm) -- ++(2,0) node[above=-1mm, midway] {$x_3$} -- ++(0,-1.5mm);

% X4
\draw (-4.1,0) -- ++(-2mm,0) -- ++(0,-1.5) node[left, midway] {$x_4$} -- ++(2mm,0);

% X5
\draw (4.1,.75) -- ++(2mm,0) -- ++(0,-1.5) node[right, midway] {$x_5$} -- ++(-2mm,0);

\filldraw[red] (-1,-1.5) circle (1pt);

\draw[dashed] (a13) circle (.3cm);
\draw[rounded corners=3pt] (-.95,-1.45) rectangle (0.95,-.8);

-