%%
%% askmaps.sty
%%
%% American Style K-maps (2, 3, 4 and 5 variables)
%%
%% (c)2013 Jesse op den Brouw, <J.E.J.opdenBrouw@hhs.nl>
%%
%% LPPL LaTeX Public Project License
%%  
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
%% Version and date
\gdef\askmapsversion{0.1}
\gdef\askmapsdate{2013/12/04}
%
\ProvidesPackage{askmaps}[\askmapsdate\space Version \askmapsversion\space
      LaTeX package for drawing American Style Karnaugh maps]
  
%% This package provides 2, 3, 4 and 5 variable Karnaugh maps that can
%% be found in numerous american text books on digital design. Note
%% that it is possible to draw many different K-maps that are all
%% legitimate as long as a square (minterm) differs only in one
%% literal with its adjacent squares.
%% This package draws K-maps where the most significant input variables
%% are placed on top of the columns and the least significant variables
%% are placed left of the rows. Such K-maps can be found in the text books:
%%  Design of Logic Systems, 2nd, Lewin & Protheroe
%%  Digital System Design with VHDL, 2nd, Zwolinski

%% Packages needed
\RequirePackage{pict2e}

%% The macros \askmapargumentstring, \askmapgetchar and \askmapgetonechar are
%% needed to process the variable-length parameters in \askmapxxx:
%% These routine politely reused from karnaugh package
\def\askmapargumentstring#1{\gdef\askmapdummystring{#1{}\noexpand\end}}
\def\askmapgetchar{\expandafter\askmapgetonechar\askmapdummystring}
\def\askmapgetonechar#1#2\end{{#1}\gdef\askmapdummystring{#2\noexpand\end}}%

%% Dimension for length of one square
\newdimen\askmapunitlength
\askmapunitlength=10mm

%% Some use options
\newif\ifaskmap@opti% want indexes
\newif\ifaskmap@optf% want output function printed
\newif\ifaskmap@optb% want input variable bit combinations at columns and rows

%% We want bit combinations along with the columns and rows
%\askmap@optbtrue

%% Process user options
\def\askmap@processoptions#1{%
	\askmap@optifalse\askmap@optftrue\askmap@optbtrue%
	\@tfor\askmap@opt:=#1\do{%
		\if i\askmap@opt\askmap@optitrue\fi%
		\if I\askmap@opt\askmap@optifalse\fi%
		\if f\askmap@opt\askmap@optftrue\fi%
		\if F\askmap@opt\askmap@optffalse\fi%
		\if b\askmap@opt\askmap@optbtrue\fi%
		\if B\askmap@opt\askmap@optbfalse\fi%
		\if c\askmap@opt\askmap@optitrue\askmap@optftrue\askmap@optbtrue\fi%
		\if C\askmap@opt\askmap@optifalse\askmap@optffalse\askmap@optbfalse\fi%
	}%
}%

%% Replacement for macros from karnaugh package
%% #1 = number of input variables
%% #2 = function output variable
%% #3 = function input variables
%% #4 = list of function values
%% #5 = user picture commands
\newcommand{\askmap}[5]{%
\if 2#1\relax
	\askmapii{#2}{#3}{f}{#4}{#5}%
\else
	\if 3#1\relax
		\askmapiii{#2}{#3}{f}{#4}{#5}%
	\else
		\if 4#1\relax
			\askmapiv{#2}{#3}{f}{#4}{#5}%
		\else
			\if 5#1\relax
				\askmapv{#2}{#3}{f}{#4}{#5}%
			\else
				\PackageWarning{askmaps}{Sorry, no K-maps with #1 variables supported by package askmaps!}
			\fi%
		\fi%
	\fi%
\fi%
}%	

%% #1= output function variable
%% #2= 2 input variables
%% #3= options list
%% #4= 4 function values
%% #5= user defined picture commands
\newcommand{\askmapii}[5]{%
{\unitlength\askmapunitlength%
\askmap@processoptions{#3}%
\begin{picture}(4.4,3)(-1.2,0)%
\linethickness{1pt}%
\put(0,0){\framebox(2,2)}%
\put(1,0){\line(0,1){2}}%
\put(0,1){\line(1,0){2}}%
\ifaskmap@optf%
\put(2.35,2.25){#1}%
\put(1.9,1.9){\line(1,1){0.4}}%
\fi%
\askmapargumentstring{#2}%
\put(-0.4,2.55){\askmapgetchar}%
\put(-0.9,2.1){\makebox(0.5,0){\askmapgetchar}}%
\ifaskmap@opti%
\put(0.01,1.05){\scriptsize{\textsl{0}}}% a little bit to the edge because
\put(0.01,0.03){\scriptsize{\textsl{1}}}% the edge line is thick
\put(1.03,1.05){\scriptsize{\textsl{2}}}%
\put(1.03,0.03){\scriptsize{\textsl{3}}}%
\fi%
\ifaskmap@optb%
\put(-0.6,1.5){\makebox(0.5,0)[r]{\footnotesize{0}}}%
\put(-0.6,0.5){\makebox(0.5,0)[r]{\footnotesize{1}}}%
\put(0.5,2.2){\makebox(0,0){\footnotesize{0}}}%
\put(1.5,2.2){\makebox(0,0){\footnotesize{1}}}%
\fi%
\askmapargumentstring{#4}%
\put(0.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,0.5){\makebox(0,0){\askmapgetchar}}%
\thicklines%
\put(0,2){\line(-1,1){0.70}}%
\thinlines%
#5%
\end{picture}%
}%
}% end \askmapii

%% #1= output function variable
%% #2= 3 input variables
%% #3= options list
%% #4= 8 function values
%% #5= user defined picture commands
\newcommand{\askmapiii}[5]{%
{\unitlength\askmapunitlength%
\askmap@processoptions{#3}%
\begin{picture}(6.4,3)(-1.2,0)%
\linethickness{1pt}%
\put(0,0){\framebox(4,2)}%
\multiput(1,0)(1,0){3}{\line(0,1){2}}%
\put(0,1){\line(1,0){4}}%
\ifaskmap@optf%
\put(4.35,2.25){#1}%
\put(3.9,1.9){\line(1,1){0.4}}%
\fi%
\askmapargumentstring{#2}%
\put(-0.4,2.55){\askmapgetchar\askmapgetchar}%
\put(-0.9,2.1){\makebox(0.5,0){\askmapgetchar}}%
\ifaskmap@opti%
\put(0.01,1.05){\scriptsize{\textsl{0}}}%
\put(0.01,0.03){\scriptsize{\textsl{1}}}%
\put(1.03,1.05){\scriptsize{\textsl{2}}}%
\put(1.03,0.03){\scriptsize{\textsl{3}}}%
\put(3.03,1.05){\scriptsize{\textsl{4}}}%
\put(3.03,0.03){\scriptsize{\textsl{5}}}%
\put(2.03,1.05){\scriptsize{\textsl{6}}}%
\put(2.03,0.03){\scriptsize{\textsl{7}}}%
\fi%
\askmapargumentstring{#4}%
\put(0.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,0.5){\makebox(0,0){\askmapgetchar}}%
\ifaskmap@optb%
\put(-0.6,1.5){\makebox(0.5,0)[r]{\footnotesize{0}}}%
\put(-0.6,0.5){\makebox(0.5,0)[r]{\footnotesize{1}}}%
\put(0.5,2.2){\makebox(0,0){\footnotesize{00}}}%
\put(1.5,2.2){\makebox(0,0){\footnotesize{01}}}%
\put(2.5,2.2){\makebox(0,0){\footnotesize{11}}}%
\put(3.5,2.2){\makebox(0,0){\footnotesize{10}}}%
\fi%
\thicklines%
\put(0,2){\line(-1,1){0.70}}%
\thinlines%
#5%
\end{picture}%
}%
}% end \askmapiii

%% #1= output function variable
%% #2= 4 input variables
%% #3= options list
%% #4= 16 function values
%% #5= user defined picture commands
\newcommand{\askmapiv}[5]{%
{\unitlength\askmapunitlength%
\askmap@processoptions{#3}%
\begin{picture}(6.4,5)(-1.2,0)%
\linethickness{1pt}%
\put(0,0){\framebox(4,4)}%
\multiput(1,0)(1,0){3}{\line(0,1){4}}%
\multiput(0,1)(0,1){3}{\line(1,0){4}}%
\ifaskmap@optf%
\put(4.35,4.25){#1}%
\put(3.9,3.9){\line(1,1){0.4}}%
\fi%
\askmapargumentstring{#2}%
\put(-0.4,4.55){\askmapgetchar\askmapgetchar}%
\put(-1.0,4.1){\makebox(0.5,0){\askmapgetchar\askmapgetchar}}%
\ifaskmap@opti%
\put(0.01,3.05){\scriptsize{\textsl{0}}}%
\put(0.01,2.05){\scriptsize{\textsl{1}}}%
\put(0.01,0.03){\scriptsize{\textsl{2}}}%
\put(0.01,1.05){\scriptsize{\textsl{3}}}%
\put(1.03,3.05){\scriptsize{\textsl{4}}}%
\put(1.03,2.05){\scriptsize{\textsl{5}}}%
\put(1.03,0.03){\scriptsize{\textsl{6}}}%
\put(1.03,1.05){\scriptsize{\textsl{7}}}%
\put(3.03,3.05){\scriptsize{\textsl{8}}}%
\put(3.03,2.05){\scriptsize{\textsl{9}}}%
\put(3.03,0.03){\scriptsize{\textsl{10}}}%
\put(3.03,1.05){\scriptsize{\textsl{11}}}%
\put(2.03,3.05){\scriptsize{\textsl{12}}}%
\put(2.03,2.05){\scriptsize{\textsl{13}}}%
\put(2.03,0.03){\scriptsize{\textsl{14}}}%
\put(2.03,1.05){\scriptsize{\textsl{15}}}%
\fi%
\askmapargumentstring{#4}%
\put(0.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,1.5){\makebox(0,0){\askmapgetchar}}%
\ifaskmap@optb%
\put(-0.6,3.5){\makebox(0.5,0)[r]{\footnotesize{00}}}%
\put(-0.6,2.5){\makebox(0.5,0)[r]{\footnotesize{01}}}%
\put(-0.6,1.5){\makebox(0.5,0)[r]{\footnotesize{11}}}%
\put(-0.6,0.5){\makebox(0.5,0)[r]{\footnotesize{10}}}%
\put(0.5,4.2){\makebox(0,0){\footnotesize{00}}}%
\put(1.5,4.2){\makebox(0,0){\footnotesize{01}}}%
\put(2.5,4.2){\makebox(0,0){\footnotesize{11}}}%
\put(3.5,4.2){\makebox(0,0){\footnotesize{10}}}%
\fi
\thicklines%
\put(0,4){\line(-1,1){0.70}}%
\thinlines%
#5%
\end{picture}%
}%
}% end \askmapiv

%% #1= output function variable
%% #2= 5 input variables
%% #3= options list
%% #4= 32 function values
%% #5= user defined picture commands
\newcommand{\askmapv}[5]{%
{\unitlength\askmapunitlength%
\askmap@processoptions{#3}%
\begin{picture}(10.4,5)(-1.2,0)%
\linethickness{1pt}%
\put(0,0){\framebox(8,4)}%
\multiput(1,0)(1,0){7}{\line(0,1){4}}%
\multiput(0,1)(0,1){3}{\line(1,0){8}}%
\ifaskmap@optf%
\put(8.35,4.25){#1}%
\put(7.9,3.9){\line(1,1){0.4}}%
\fi%
\askmapargumentstring{#2}%
\put(-0.4,4.55){\askmapgetchar\askmapgetchar\askmapgetchar}%
\put(-1.0,4.1){\makebox(0.5,0){\askmapgetchar\askmapgetchar}}%
\ifaskmap@opti%
\put(0.01,3.05){\scriptsize{\textsl{0}}}%
\put(0.01,2.05){\scriptsize{\textsl{1}}}%
\put(0.01,0.03){\scriptsize{\textsl{2}}}%
\put(0.01,1.05){\scriptsize{\textsl{3}}}%
\put(1.03,3.05){\scriptsize{\textsl{4}}}%
\put(1.03,2.05){\scriptsize{\textsl{5}}}%
\put(1.03,0.03){\scriptsize{\textsl{6}}}%
\put(1.03,1.05){\scriptsize{\textsl{7}}}%
\put(3.03,3.05){\scriptsize{\textsl{8}}}%
\put(3.03,2.05){\scriptsize{\textsl{9}}}%
\put(3.03,0.03){\scriptsize{\textsl{10}}}%
\put(3.03,1.05){\scriptsize{\textsl{11}}}%
\put(2.03,3.05){\scriptsize{\textsl{12}}}%
\put(2.03,2.05){\scriptsize{\textsl{13}}}%
\put(2.03,0.03){\scriptsize{\textsl{14}}}%
\put(2.03,1.05){\scriptsize{\textsl{15}}}%
\put(7.03,3.05){\scriptsize{\textsl{16}}}%
\put(7.03,2.05){\scriptsize{\textsl{17}}}%
\put(7.03,0.03){\scriptsize{\textsl{18}}}%
\put(7.03,1.05){\scriptsize{\textsl{19}}}%
\put(6.03,3.05){\scriptsize{\textsl{20}}}%
\put(6.03,2.05){\scriptsize{\textsl{21}}}%
\put(6.03,0.03){\scriptsize{\textsl{22}}}%
\put(6.03,1.05){\scriptsize{\textsl{23}}}%
\put(4.03,3.05){\scriptsize{\textsl{24}}}%
\put(4.03,2.05){\scriptsize{\textsl{25}}}%
\put(4.03,0.03){\scriptsize{\textsl{26}}}%
\put(4.03,1.05){\scriptsize{\textsl{27}}}%
\put(5.03,3.05){\scriptsize{\textsl{28}}}%
\put(5.03,2.05){\scriptsize{\textsl{29}}}%
\put(5.03,0.03){\scriptsize{\textsl{30}}}%
\put(5.03,1.05){\scriptsize{\textsl{31}}}%
\fi%
\askmapargumentstring{#4}%
\put(0.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(0.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(1.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(3.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(2.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(7.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(7.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(7.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(7.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(6.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(6.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(6.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(6.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(4.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(4.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(4.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(4.5,1.5){\makebox(0,0){\askmapgetchar}}%
\put(5.5,3.5){\makebox(0,0){\askmapgetchar}}%
\put(5.5,2.5){\makebox(0,0){\askmapgetchar}}%
\put(5.5,0.5){\makebox(0,0){\askmapgetchar}}%
\put(5.5,1.5){\makebox(0,0){\askmapgetchar}}%
\ifaskmap@optb%
\put(-0.6,3.5){\makebox(0.5,0)[r]{\footnotesize{00}}}%
\put(-0.6,2.5){\makebox(0.5,0)[r]{\footnotesize{01}}}%
\put(-0.6,1.5){\makebox(0.5,0)[r]{\footnotesize{11}}}%
\put(-0.6,0.5){\makebox(0.5,0)[r]{\footnotesize{10}}}%
\put(0.5,4.2){\makebox(0,0){\footnotesize{000}}}%
\put(1.5,4.2){\makebox(0,0){\footnotesize{001}}}%
\put(2.5,4.2){\makebox(0,0){\footnotesize{011}}}%
\put(3.5,4.2){\makebox(0,0){\footnotesize{010}}}%
\put(4.5,4.2){\makebox(0,0){\footnotesize{110}}}%
\put(5.5,4.2){\makebox(0,0){\footnotesize{111}}}%
\put(6.5,4.2){\makebox(0,0){\footnotesize{101}}}%
\put(7.5,4.2){\makebox(0,0){\footnotesize{100}}}%
\fi%
\thicklines%
\put(0,4){\line(-1,1){0.70}}%
\thinlines%
#5%
\end{picture}%
}%
}% end \askmapv

\endinput
%%
%% End of file 'askmaps.sty'.
