# How to do a karnaugh map? [duplicate]

Possible Duplicate:
How can I draw a Karnaugh map

For an assignment, I have to include a Karnaugh map. The map looks like this:

I'm pretty new to LaTeX, and really have no idea how to do this. I know how to make tables and all, but after hours of looking around, I can't come up with a relatively easy way to do the circles. The closest I've found is this: http://www.ctan.org/tex-archive/macros/latex/contrib/karnaugh, which at least have the same name, but the map looks very different from what mine is supposed to look like. I realize I should include an attempt of solving it, but I really have no idea.

• See related question how would you do karnaughs maps in latex or context or more you find right under the heading "related" ... – Mensch Jan 19 '13 at 2:07
• Not a duplicate, since, while the name is the same, the end result is different. My teacher was very specific regarding what the map is supposed to look like (which should be like in my picture), while the maps done with kvmacros (which is what was discussed in your link), do not look like what I'm looking for. – Fredrik Jonsén Jan 19 '13 at 3:20

I very slightly modified the answer given by Ignasi here:

to look a little more like what you want. Have a look at the differences between e.g. the karnaugh environment as defined in that answer, versus the modified version here to get a feel for how to change things. If you find this answer useful, then please upvote the answer linked above rather than this one. I personally don't know TikZ at all so I can't help you much more than this unfortunately.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}

%internal group
%#1-space between node and grouping line. Default=0
%#2-top left node
%#3-bottom right node
\newcommand{\implicant}[3][0]{
\draw[rounded corners=3pt] ($(#2.north west)+(135:#1)$) rectangle ($(#3.south east)+(-45:#1)$);
}

%group lateral borders
%#1-space between node and grouping line. Default=0
%#2-top left node
%#3-bottom right node
\newcommand{\implicantcostats}[3][0]{
\draw[rounded corners=3pt] ($(rf.east |- #2.north)+(90:#1)$)-| ($(#2.east)+(0:#1)$) |- ($(rf.east |- #3.south)+(-90:#1)$);
\draw[rounded corners=3pt] ($(cf.west |- #2.north)+(90:#1)$) -| ($(#3.west)+(180:#1)$) |- ($(cf.west |- #3.south)+(-90:#1)$);
}

%group top-bottom borders
%#1-space between node and grouping line. Default=0
%#2-top left node
%#3-bottom right node
\newcommand{\implicantdaltbaix}[3][0]{
\draw[rounded corners=3pt] ($(cf.south -| #2.west)+(180:#1)$) |- ($(#2.south)+(-90:#1)$) -| ($(cf.south -| #3.east)+(0:#1)$);
\draw[rounded corners=3pt] ($(rf.north -| #2.west)+(180:#1)$) |- ($(#3.north)+(90:#1)$) -| ($(rf.north -| #3.east)+(0:#1)$);
}

%group corners
%#1-space between node and grouping line. Default=0
\newcommand{\implicantcantons}[1][0]{
\draw[rounded corners=3pt] ($(rf.east |- 0.south)+(-90:#1)$) -| ($(0.east |- cf.south)+(0:#1)$);
\draw[rounded corners=3pt] ($(rf.east |- 8.north)+(90:#1)$) -| ($(8.east |- rf.north)+(0:#1)$);
\draw[rounded corners=3pt] ($(cf.west |- 2.south)+(-90:#1)$) -| ($(2.west |- cf.south)+(180:#1)$);
\draw[rounded corners=3pt] ($(cf.west |- 10.north)+(90:#1)$) -| ($(10.west |- rf.north)+(180:#1)$);
}
\def\ol#1{\overline{#1}}
%Empty Karnaugh map 4x4
\newenvironment{Karnaugh}%
{
\begin{tikzpicture}[baseline=(current bounding box.north),scale=0.8]
\draw (0,0) grid (4,4);
%
\matrix (mapa) [matrix of nodes,
column sep={0.8cm,between origins},
row sep={0.8cm,between origins},
every node/.style={minimum size=0.3mm},
anchor=8.center,
ampersand replacement=\&] at (0.5,0.5)
{
\& |(c00)| $\ol{yw}$  \& |(c01)| $\ol{y}w$  \& |(c11)| $yw$       \& |(c10)| $y\ol{w}$  \& |(cf)| \phantom{00} \\
|(r00)| $\ol{xz}$      \& |(0)|  \phantom{0} \& |(1)|  \phantom{0} \& |(3)|  \phantom{0} \& |(2)|  \phantom{0} \&                     \\
|(r01)| $\ol{x}z$      \& |(4)|  \phantom{0} \& |(5)|  \phantom{0} \& |(7)|  \phantom{0} \& |(6)|  \phantom{0} \&                     \\
|(r11)| $xz$           \& |(12)| \phantom{0} \& |(13)| \phantom{0} \& |(15)| \phantom{0} \& |(14)| \phantom{0} \&                     \\
|(r10)| $x\ol{z}$      \& |(8)|  \phantom{0} \& |(9)|  \phantom{0} \& |(11)| \phantom{0} \& |(10)| \phantom{0} \&                     \\
|(rf) | \phantom{00}   \&                    \&                    \&                    \&                    \&                     \\
};
}%
{
\end{tikzpicture}
}

%Empty Karnaugh map 2x4
\newenvironment{Karnaughvuit}%
{
\begin{tikzpicture}[baseline=(current bounding box.north),scale=0.8]
\draw (0,0) grid (4,2);
%
\matrix (mapa) [matrix of nodes,
column sep={0.8cm,between origins},
row sep={0.8cm,between origins},
every node/.style={minimum size=0.3mm},
anchor=4.center,
ampersand replacement=\&] at (0.5,0.5)
{
\& |(c00)| $\ol{yz}$  \& |(c01)| $\ol{y}z$  \& |(c11)| $yz$       \& |(c10)| $y\ol{z}$  \& |(cf)| \phantom{00} \\
|(r00)| $\ol{x}$      \& |(0)|  \phantom{0} \& |(1)|  \phantom{0} \& |(3)|  \phantom{0} \& |(2)|  \phantom{0} \&                     \\
|(r01)| $x$           \& |(4)|  \phantom{0} \& |(5)|  \phantom{0} \& |(7)|  \phantom{0} \& |(6)|  \phantom{0} \&                     \\
|(rf) | \phantom{00}  \&                    \&                    \&                    \&                    \&                     \\
};
}%
{
\end{tikzpicture}
}

%Defines 8 or 16 values (0,1,X)
\newcommand{\contingut}[1]{%
\foreach \x [count=\xi from 0]  in {#1}
\path (\xi) node {\x};
}

%Places 1 in listed positions
\newcommand{\minterms}[1]{%
\foreach \x in {#1}
\path (\x) node {1};
}

%Places 0 in listed positions
\newcommand{\maxterms}[1]{%
\foreach \x in {#1}
\path (\x) node {0};
}

%Places X in listed positions
\newcommand{\indeterminats}[1]{%
\foreach \x in {#1}
\path (\x) node {X};
}

\begin{document}
\begin{Karnaugh}
\contingut{0,0,0,0,0,1,0,1,1,1,0,0,0,1,0,1}
\implicant{0}{2}
\implicantdaltbaix[3pt]{3}{10}
\implicantcostats{4}{14}
\end{Karnaugh}
%
\begin{Karnaughvuit}
\minterms{3,4}
\maxterms{0,1,6,7}
\indeterminats{2,5}
\implicant{3}{2}
\implicant{4}{5}
\end{Karnaughvuit}
\end{document}