summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2022-02-09 03:00:53 +0000
committerNorbert Preining <norbert@preining.info>2022-02-09 03:00:53 +0000
commit8363587176bd1746ab2d65ee183b196e6d3458cf (patch)
tree64a89e6416d2b23d149b3dd918af467ae0b7441a /graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex
parent98810f6d7d064e61186d0ce674c4ee9de47a1f55 (diff)
CTAN sync 202202090300
Diffstat (limited to 'graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex')
-rw-r--r--graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex519
1 files changed, 447 insertions, 72 deletions
diff --git a/graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex b/graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex
index 330cb6f3a5..f9a9434435 100644
--- a/graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex
+++ b/graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex
@@ -1,6 +1,6 @@
%% This is file `tikzlibrarykarnaugh.code.tex' formerly known as `tikz-karnaugh.tex',
-%% Version: 1.3
-%% Version date: 2021-10-14
+%% Version: 1.4
+%% Version date: 2022-02-08
%%
%% Copyright (C) 2018 by Luis Paulo Laus, laus@utfpr.edu.br
%%
@@ -22,7 +22,7 @@
%%
\typeout{}
\typeout{Macros for typesetting Karnaugh maps}
-\typeout{Version 1.3 of 14 October 2021}
+\typeout{Version 1.4 of 8 February 2022}
\typeout{by Luis Paulo Laus, laus@utfpr.edu.br}
\typeout{}
%%
@@ -32,13 +32,17 @@
%% options in the identifiers and values lists. New vertical mode.
%% New keys to enable indices and disable bars. Macro file renamed
%% to proper tikz library file name.
-%% 1.2: 19 March 2018: stop using \pgftransformshift, new kmindexpos
-%% (kmindexposx and kmindexposy) to control index position, cell
+%% 1.2: 19 March 2018: stop using \pgftransformshift, new kmindex pos
+%% (kmindex posx and kmindex posy) to control index position, cell
%% typesetting order switched (first content then index), parameter
%% names changed to include ``km'' letters, new binary index mode
%% 1.3: 14 October 2021: New American style KM (for maps with ones
%% and zeros labels surrounding the map). In addition, a new macro for
%% decimal to Gray Code conversion (\pgfmathdectoGray) is provided.
+%% 1.4: February 2022: new style and length for American style;
+%% change some key names (standard); new 'karnaugh cell size' key;
+%% bug fixes; positioning of labels simplified; two new macros for type
+%% setting maps as they should appear and not using the truth table order.
%%
%% Setting up some TikZ parameters for Karnaugh Maps
%%
@@ -52,20 +56,25 @@
kmbar left sep/.initial=1\kmunitlength,
enable indices/.is if=enableindices,
kmindex/.style={red,font=\tiny},
- kmtoplabel/.style={blue,font=\footnotesize},
- kmleftlabel/.style={blue,font=\footnotesize},
- kmindexposx/.initial=0.2\kmunitlength,
- kmindexposy/.initial=0.8\kmunitlength,
- kmindexpos/.style 2 args={kmindexposx={##1\kmunitlength},kmindexposy={##2\kmunitlength}},
+ kmindex posx/.initial=0.2\kmunitlength,
+ kmindex posy/.initial=0.8\kmunitlength,
+ kmindex pos/.style 2 args={kmindex posx={##1\kmunitlength},kmindex posy={##2\kmunitlength}},
binary index/.is if=indexbin,
Gray index/.is if=indexGray,
American style/.is if=Americanstyle,
+ kmsep line/.style={},
+ kmsep line length/.initial=1\kmunitlength,
+ kmlabel top/.style={above,blue,font=\footnotesize\ttfamily},
+ kmlabel left/.style={left,blue,font=\footnotesize\ttfamily},
kmcell/.style={},
kmvar/.style={},
kmbox/.style={},
kmlines/.style={},
- every karnaugh/.try,
- }
+ x=1\kmunitlength,
+ y=1\kmunitlength,
+ every karnaugh/.try
+ },
+ karnaugh cell size/.code=\pgfmathsetlength\kmunitlength{#1}
}
%%
@@ -107,7 +116,7 @@
\newcount\kmysize
%%
%% Some counters are necessary to compute the bars for the variable
-%% identifiers:
+%% identifiers. Also used as temp varaibles in code conversion:
%%
\newcount\kmvarno
\newcount\kmxvarno
@@ -128,16 +137,22 @@
\kmdectobin{\kmindexcounter}\global\advance\kmindexcounter by 1}%
\def\kmcurrentindexGray{%
\kmdectoKG{\kmindexcounter}\global\advance\kmindexcounter by 1}%
+\def\kmcurrentindexGraytab{%
+ \kmdectoKGtab{\kmindexcounter}\global\advance\kmindexcounter by 1}%
\def\kmcurrentindexGrayvert{%
\kmdectoKGvert{\kmindexcounter}\global\advance\kmindexcounter by 1}%
-%%
-%% We need a macro that computes the powers of two:
-%%
-\def\kmpoweroftwo#1#2{{% Computes #1=2^#2, both of which have to be counters
- \ifnum#2>0
- \global\multiply#1 by 2
- \advance#2 by -1
- \kmpoweroftwo{#1}{#2}
+\def\kmcurrentindexGraytabvert{%
+ \kmdectoKGtabvert{\kmindexcounter}\global\advance\kmindexcounter by 1}%
+%%
+%% We need a macro that computes the powers of two.
+%% Computes #1=2^#2, both of which have to be counters and
+%% both are modified
+%%
+\def\kmpoweroftwo#1#2{{%
+ \ifnum#2>0%
+ \global\multiply#1 by 2%
+ \advance#2 by -1%
+ \kmpoweroftwo{#1}{#2}%
\fi}}
%%
%% \pgfmathdectoGray
@@ -167,7 +182,7 @@
\ifx#1\pgf@stop%
\let\pgf@lib@sh@next\relax%
\else%
- \edef\pgfresult@@{\pgfresult@@\ifnum#1=\c@pgfmath@counta\relax0\else1\fi}%
+ \edef\pgfresult@@{\pgfresult@@\ifnum#1=\c@pgfmath@counta\relax0\else1\fi}% xor
\c@pgfmath@counta=#1\relax%
\fi%
\pgf@lib@sh@next}
@@ -179,16 +194,16 @@
\gdef\kmdectoKG#1{%
\kmbarnum=#1\relax% decimal number to be converted
\kmbarlength=\kmvarno\relax% number of bits
- \let\kmresult@=\empty
- \let\kmresult@@=\empty
+ \let\kmresult@=\empty%
+ \let\kmresult@@=\empty%
\let\next\kmdectoKG@%
\next%
\kmresult@@\kmresult@}
\gdef\kmdectoKGvert#1{%
\kmbarnum=#1\relax% decimal number to be converted
\kmbarlength=\kmvarno\relax% number of bits
- \let\kmresult@=\empty
- \let\kmresult@@=\empty
+ \let\kmresult@=\empty%
+ \let\kmresult@@=\empty%
\let\next\kmdectoKG@%
\next%
\kmresult@\kmresult@@}
@@ -213,6 +228,68 @@
\fi%
\next}
%%
+%% The macros \kmdectoKGtab converts the cell index
+%% to a binary code that resembles Gray code
+%% without deinterleaving the variables.
+%% Used in Table Mode
+%%
+\gdef\kmdectoKGtab#1{%
+ \kmbarnum=#1\relax% decimal number to be converted
+ \kmbarlength=\kmvarno\relax% number of bits
+ \let\kmresult@=\empty%
+ \let\kmresult@@=\empty%
+ \let\next\kmdectobin@%
+ \next%
+ \kmbarlength=\kmvarno\relax% number of bits
+ \kmbarmove=0\relax% last bit
+ \let\next\kmdectoKG@tab%
+ \expandafter\next\kmresult@\end%
+ \kmresult@@}
+\gdef\kmdectoKG@tab#1{%
+ \ifx#1\end%
+ \let\next\relax%
+ \else%
+ \edef\kmresult@@{\kmresult@@\ifnum#1=\kmbarmove\relax0\else1\fi}% xor
+ \kmbarmove=#1\relax%
+ \advance\kmbarlength by -1%
+ \ifnum\kmbarlength=\kmxvarno\kmbarmove=0\fi%
+ \fi%
+ \next}%
+\gdef\kmdectoKGtabvert#1{%
+ \kmbarnum=#1\relax% decimal number to be converted
+ \kmbarlength=\kmvarno\relax% number of bits
+ \let\kmresult@=\empty%
+ \let\kmresult@@=\empty%
+ \let\kmresult@@@=\empty%
+ \let\next\kmdectobin@%
+ \next%
+ \kmbarlength=\kmvarno\relax% number of bits
+ \kmbarmove=0\relax% last bit
+ \let\next\kmdectoKG@tabvert%
+ \expandafter\next\kmresult@\end%
+ \kmresult@@@\kmresult@@}
+\gdef\kmdectoKG@tabvert#1{%
+ \ifx#1\end%
+ \let\next\relax%
+ \else%
+ \edef\kmresult@@{\kmresult@@\ifnum#1=\kmbarmove\relax0\else1\fi}% xor
+ \kmbarmove=#1\relax%
+ \advance\kmbarlength by -1%
+ \ifnum\kmbarlength=\kmxvarno%
+ \kmbarmove=0\relax%
+ \let\next\kmdectoKG@@tabvert%
+ \fi%
+ \fi%
+ \next}
+\gdef\kmdectoKG@@tabvert#1{%
+ \ifx#1\end%
+ \let\next\relax%
+ \else%
+ \edef\kmresult@@@{\kmresult@@@\ifnum#1=\kmbarmove\relax0\else1\fi}% xor
+ \kmbarmove=#1\relax%
+ \fi
+ \next}
+%%
%% The macros \kmdectobin converts a decimal number
%% passed as argument to binary such that the number of
%% digits (length) is equal to \kmvarno
@@ -239,27 +316,41 @@
\advance\kmbarlength by -1\relax%
\fi%
\next}
-}
%%
-%% The macros \kmmakeindexGray creates a Gray coded index
-%% inside each cell
-%% Deprecated!!! Replaced by a new mechanism based on \kmdectoKG
-%%
-\def\kmmakeindexGray{
- \kmbarlength=\kmxsize\relax
- \advance\kmbarlength by -1\relax
- \kmtemppos=\kmysize\relax
- \advance\kmtemppos by -1\relax
- \foreach \x in {0,...,\kmbarlength}{
- \foreach \y in {0,...,\kmtemppos}{
- \pgfmathsetbasenumberlength{\kmyvarno}
- \pgfmathdectoGray\mynumbery{\y}
- \pgfmathsetbasenumberlength{\kmxvarno}
- \pgfmathdectoGray\mynumberx{\x}
- \node[shift={((\pgfkeysvalueof{/tikz/kmindexposx},(\pgfkeysvalueof{/tikz/kmindexposy}+\kmtemppos\kmunitlength)},kmindex] at(\x\kmunitlength,-\y\kmunitlength) {\mynumbery\mynumberx};
- }
- }
-}
+%% The macros \kmdectoKGdec coverts an index to
+%% pseudo Gray code in decimal (test purpose).
+%% No vertical mode available
+%%
+\gdef\kmdectoKGdec#1{%
+ \kmbarnum=#1\relax% decimal number to be converted
+ \kmbarstart=0\relax%
+ \kmbarmove=0\relax%
+ \kmbarlength=1\relax%
+ \c@pgfmath@counta=\kmxsize\relax%
+ \kmdectoKGdec@%
+ \advance\kmbarmove by \kmbarstart%
+ \the\kmbarmove}
+\gdef\kmdectoKGdec@{%
+ \ifnum\kmbarnum=0%
+ \let\next\relax%
+ \else%
+ \ifodd\kmbarnum\advance\kmbarstart by \kmbarlength\fi%
+ \multiply\kmbarlength by 2%
+ \divide \kmbarnum by 2%
+ \let\next\kmdectoKGdec@@%
+ \fi%
+ \next}
+\gdef\kmdectoKGdec@@{%
+ \ifnum\kmbarnum=0%
+ \let\next\relax%
+ \else%
+ \ifodd\kmbarnum\advance\kmbarmove by \c@pgfmath@counta\fi%
+ \multiply\c@pgfmath@counta by 2%
+ \divide \kmbarnum by 2%
+ \let\next\kmdectoKGdec@%
+ \fi%
+ \next}
+} % makeatletter
%%
%% The macros \kmargumentstring and \kmsetoptstr are needed to
%% process the variable-length parameters in \karnaughmap:
@@ -336,7 +427,7 @@
\kmsetoptstr{} % reads argument as <[opt]>str
\node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (#3\kmunitlength,#4\kmunitlength){\kmstr};%
\ifenableindices
- \node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindexposx},\pgfkeysvalueof{/tikz/kmindexposy})}]
+ \node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}]
at (#3\kmunitlength,#4\kmunitlength) {\kmcurrentindex};
\fi
\fi}}%
@@ -423,7 +514,7 @@
\fi
\next
}
-
+%%
%% \karnaughmakelabels is a modification of \karnaughmakebars.
%% Instead of making the bars it sets two macros, \kmtoplabels
%% and \kmleftlabels, with the labels for American style maps
@@ -491,26 +582,24 @@
\draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength);
#5
\ifAmericanstyle
- \pgfmathsetbasenumberlength{\kmxvarno}
\kmtemppos=\kmxsize
\advance\kmtemppos by -1
- \kmtemplength=\pgfkeysvalueof{/tikz/kmbar top sep}
\pgfmathsetbasenumberlength{\kmxvarno}
\foreach \x in {0,...,\kmtemppos}
- \node[xshift=0.5\kmunitlength, yshift=\kmtemplength,kmtoplabel] at (\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber};
+ \node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber};
\kmtemppos=\kmysize
\advance\kmtemppos by -1
\ifnum\kmtemppos>0
- \kmtemplength=\pgfkeysvalueof{/tikz/kmbar left sep}
\pgfmathsetbasenumberlength{\kmyvarno}
\foreach \y in {0,...,\kmtemppos}
- \node[xshift=-\kmtemplength, yshift=\kmysize\kmunitlength-0.5\kmunitlength,left,kmleftlabel] at (0,-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber};
+ \node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber};
\fi
\kmargumentstring{#3}
\gdef\kmtoplabels{}
\gdef\kmleftlabels{}
- \karnaughmakelabels
- \draw[kmbox] (0,\kmysize\kmunitlength) -- ++(135:1\kmunitlength) node[kmbar label,below left,pos=0.5]{\kmleftlabels} node[kmbar label,above right,pos=0.5]{\kmtoplabels} node[kmvar,above left]{#2};
+ \karnaughmakelabels
+ \kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length}
+ \draw[kmsep line] (0,\kmysize\kmunitlength) -- ++(-\kmtemplength,\kmtemplength) node[kmbar label,below left,pos=0.5]{\kmleftlabels} node[kmbar label,above right,pos=0.5]{\kmtoplabels} node[kmvar,above left]{#2};
\else
\ifdisablebars\relax\else
\node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
@@ -521,7 +610,6 @@
\ifenableindices
\ifindexbin
\gdef\kmcurrentindex{\kmcurrentindexbin}
- \pgfmathsetbasenumberlength{#1}
\else
\ifindexGray
\gdef\kmcurrentindex{\kmcurrentindexGray}
@@ -597,7 +685,7 @@
\kmsetoptstr{} % reads argument as <[opt]>str
\node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (#3\kmunitlength,#4\kmunitlength){\kmstr};
\ifenableindices
- \node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindexposx},\pgfkeysvalueof{/tikz/kmindexposy})}]
+ \node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}]
at (#3\kmunitlength,#4\kmunitlength) {\kmcurrentindex};
\fi
\fi}}%
@@ -622,9 +710,9 @@
\next
}
-%% \karnaughmakelabelsvert is a modification of \karnaughmakebars.
+%% \karnaughmakelabelsvert is a modification of \karnaughmakebarsvert.
%% Instead of making the bars it sets two macros, \kmtoplabels
-%% and \kmleftlabels, with the labels for americal style maps
+%% and \kmleftlabels, with the labels for American style maps
%%
\def\karnaughmakelabelsvert{%
\ifnum\kmvarno>0
@@ -687,26 +775,24 @@
\draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength);
#5
\ifAmericanstyle
- \pgfmathsetbasenumberlength{\kmxvarno}
\kmtemppos=\kmxsize
\advance\kmtemppos by -1
- \kmtemplength=\pgfkeysvalueof{/tikz/kmbar top sep}
- \pgfmathsetbasenumberlength{\kmxvarno}
- \foreach \x in {0,...,\kmtemppos}
- \node[xshift=0.5\kmunitlength, yshift=\kmtemplength,kmtoplabel] at (\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber};
- \kmtemppos=\kmysize
- \advance\kmtemppos by -1
\ifnum\kmtemppos>0
- \kmtemplength=\pgfkeysvalueof{/tikz/kmbar left sep}
- \pgfmathsetbasenumberlength{\kmyvarno}
- \foreach \y in {0,...,\kmtemppos}
- \node[xshift=-\kmtemplength, yshift=\kmysize\kmunitlength-0.5\kmunitlength,left,kmleftlabel] at (0,-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber};
+ \pgfmathsetbasenumberlength{\kmxvarno}
+ \foreach \x in {0,...,\kmtemppos}
+ \node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber};
\fi
+ \kmtemppos=\kmysize
+ \advance\kmtemppos by -1
+ \pgfmathsetbasenumberlength{\kmyvarno}
+ \foreach \y in {0,...,\kmtemppos}
+ \node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber};
\kmargumentstring{#3}
\gdef\kmtoplabels{}
\gdef\kmleftlabels{}
\karnaughmakelabelsvert
- \draw[kmbox] (0,\kmysize\kmunitlength) -- ++(135:1\kmunitlength) node[kmbar label,below left,pos=0.5]{\kmleftlabels} node[kmbar label,above right,pos=0.5]{\kmtoplabels} node[kmvar,above left]{#2};
+ \kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length}
+ \draw[kmsep line] (0,\kmysize\kmunitlength) -- ++(-\kmtemplength,\kmtemplength) node[kmbar label,below left,pos=0.5]{\kmleftlabels} node[kmbar label,above right,pos=0.5]{\kmtoplabels} node[kmvar,above left]{#2};
\else
\ifdisablebars\relax\else
\node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
@@ -717,7 +803,6 @@
\ifenableindices
\ifindexbin
\gdef\kmcurrentindex{\kmcurrentindexbin}
- \pgfmathsetbasenumberlength{#1}
\else
\ifindexGray
\gdef\kmcurrentindex{\kmcurrentindexGrayvert}
@@ -737,5 +822,295 @@
\karnaughmakemapvert{\the\kmxsize}{0}{0}{0}
\fi
}%
+%%
+%% Table Mode
+%% Non-recursive mode. Input is given is Karnaugh map order and not in truth table
+%% order. It should allow for bigger maps because the algorithm employed is not the
+%% time and memory consuming recursive algorithm of the old macros.
+%%
+%% \karnaughmakebarstab calls \karnaughmaketopbar or \karnaughmakeleftbar
+%% depending on whether \kmvarno is in the first or second half.
+%%
+\def\karnaughmakebarstab{%
+ \ifnum\kmvarno>0
+ \let\next=\karnaughmakebarstab
+ \ifnum\kmyvarno>0 % We have to make a bar at the left
+ \advance\kmyvarno by -1
+ \karnaughmakeleftbar
+ \else % We have to make a bar at the top
+ \advance\kmxvarno by -1
+ \karnaughmaketopbar
+ \fi
+ \advance\kmvarno by -1
+ \else
+ \let\next=\relax
+ \fi
+ \next
+}
+%%
+%% \karnaughmakelabelstab is a modification of \karnaughmakebarstab.
+%% Instead of making the bars it sets two macros, \kmtoplabels
+%% and \kmleftlabels, with the labels for American style maps
+%%
+\def\karnaughmakelabelstab{%
+ \ifnum\kmvarno>0
+ \let\next=\karnaughmakelabelstab
+ \ifnum\kmyvarno>0 % We have to make a bar at the left
+ \advance\kmyvarno by -1
+ \kmsetoptstr % reads argument as <[opt]>str
+ \ifx\kmleftlabels\empty
+ \xdef\kmleftlabels{\kmstr}
+ \else
+ \xdef\kmleftlabels{\kmleftlabels, \kmstr}
+ \fi
+ \else % We have to make the label for top
+ \advance\kmxvarno by -1
+ \kmsetoptstr % reads argument as <[opt]>str
+ \ifx\kmtoplabels\empty
+ \xdef\kmtoplabels{\kmstr}
+ \else
+ \xdef\kmtoplabels{\kmtoplabels, \kmstr}
+ \fi
+ \fi
+ \advance\kmvarno by -1
+ \else
+ \let\next=\relax
+ \fi
+ \next
+}
+%% \karnaughmap is the macro that a user calls if he wants to draw a
+%% Karnaugh map:
+%%
+\def\karnaughmaptab#1#2#3#4#5{%
+%%
+%% #1 is the number of variables in the Karnaugh map
+%% #2 is the identifier of the function
+%% #3 is the list of identifiers of those variables
+%% #4 is the list of tokens that have to be written into the map
+%% #5 a possibly empty set of TikZ commands that will be drown before
+%% the function values so the values will appear on top of them
+%%
+ \kmvarno=#1 % \kmvarno is the total number of variables
+ \kmyvarno=#1 % \kmyvarno is the number of variable bars at the left
+ \divide\kmyvarno by 2
+ \kmxvarno=#1 % \kmxvarno is the number of variable bars on top
+ \advance\kmxvarno by -\kmyvarno
+ \kmxsize=1
+ \kmpoweroftwo{\kmxsize}{\kmxvarno}
+ \kmysize=1
+ \kmpoweroftwo{\kmysize}{\kmyvarno}
+ \kmtemppos=\kmxsize
+ \advance\kmtemppos by -1
+ \kmtemplength=\pgfkeysvalueof{/tikz/kmbar top sep}
+ \foreach \x in {1,...,\kmtemppos}
+ \draw[kmlines] (\x\kmunitlength,0) -- (\x\kmunitlength,\kmysize\kmunitlength);
+ \kmtemppos=\kmysize
+ \advance\kmtemppos by -1
+ \ifnum\kmtemppos>0
+ \foreach \y in {1,...,\kmtemppos}
+ \draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength);
+ \fi
+ \draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength);
+ #5
+ \ifAmericanstyle
+ \kmtemppos=\kmxsize
+ \advance\kmtemppos by -1
+ \pgfmathsetbasenumberlength{\kmxvarno}
+ \foreach \x in {0,...,\kmtemppos}
+ \node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber};
+ \kmtemppos=\kmysize
+ \advance\kmtemppos by -1
+ \ifnum\kmtemppos>0
+ \pgfmathsetbasenumberlength{\kmyvarno}
+ \foreach \y in {0,...,\kmtemppos}
+ \node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber};
+ \fi
+ \kmargumentstring{#3}
+ \gdef\kmtoplabels{}
+ \gdef\kmleftlabels{}
+ \karnaughmakelabelstab
+ \kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length}
+ \draw[kmsep line] (0,\kmysize\kmunitlength) -- ++(-\kmtemplength,\kmtemplength) node[kmbar label,below left,pos=0.5]{\kmleftlabels} node[kmbar label,above right,pos=0.5]{\kmtoplabels} node[kmvar,above left]{#2};
+ \else
+ \ifdisablebars\relax\else
+ \node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
+ \kmargumentstring{#3}
+ \karnaughmakebarstab
+ \fi
+ \fi
+ \ifenableindices
+ \kmvarno=#1 % \kmvarno is the total number of variables
+ \ifindexbin
+ \gdef\kmcurrentindex{\kmcurrentindexbin}
+ \else
+ \ifindexGray
+ \gdef\kmcurrentindex{\kmcurrentindexGraytab}
+ \kmyvarno=#1 % \kmyvarno is the number of variable bars at the left
+ \divide\kmyvarno by 2
+ \kmxvarno=#1 % \kmxvarno is the number of variable bars on top
+ \advance\kmxvarno by -\kmyvarno
+ \else
+ \gdef\kmcurrentindex{\kmcurrentindexdec}
+ \fi
+ \fi
+ \fi
+ \kmindexcounter=0
+ \kmargumentstring{#4}
+ \advance\kmxsize by -1
+ \advance\kmysize by -1
+ \foreach \y in {\kmysize,...,0} {
+ \foreach \x in {0,...,\kmxsize} {
+ \kmsetoptstr{}
+ \node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (\x\kmunitlength,\y\kmunitlength){\kmstr};%
+ \ifenableindices
+ \node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}] at (\x\kmunitlength,\y\kmunitlength) {\kmcurrentindex};
+ \fi
+ }
+ }
+}%
+%%
+%% Vertical Table Mode
+%% The next three macros are similar to the ones for normal mode.
+%%
+%% \karnaughmakebarsvert calls \karnaughmaketopbar or \karnaughmakeleftbar
+%% depending on whether \kmvarno is in the first or second half.
+%%
+\def\karnaughmakebarstabvert{%
+ \ifnum\kmvarno>0
+ \let\next=\karnaughmakebarstabvert
+ \ifnum\kmxvarno>0 % We have to make a bar at the top
+ \advance\kmxvarno by -1
+ \karnaughmaketopbar
+ \else % We have to make a bar at the left
+ \advance\kmyvarno by -1
+ \karnaughmakeleftbar
+ \fi
+ \advance\kmvarno by -1
+ \else
+ \let\next=\relax
+ \fi
+ \next
+}
+
+%% \karnaughmakelabelstabvert is a modification of \karnaughmakebarstabvert.
+%% Instead of making the bars it sets two macros, \kmtoplabels
+%% and \kmleftlabels, with the labels for American style maps
+%%
+\def\karnaughmakelabelstabvert{%
+ \ifnum\kmvarno>0
+ \let\next=\karnaughmakelabelstabvert
+ \ifnum\kmxvarno>0 % We have to make a bar at the top
+ \advance\kmxvarno by -1
+ \kmsetoptstr % reads argument as <[opt]>str
+ \ifx\kmtoplabels\empty
+ \xdef\kmtoplabels{\kmstr}
+ \else
+ \xdef\kmtoplabels{\kmtoplabels, \kmstr}
+ \fi
+ \else % We have to make the label for left
+ \advance\kmyvarno by -1
+ \kmsetoptstr % reads argument as <[opt]>str
+ \ifx\kmleftlabels\empty
+ \xdef\kmleftlabels{\kmstr}
+ \else
+ \xdef\kmleftlabels{\kmleftlabels, \kmstr}
+ \fi
+ \fi
+ \advance\kmvarno by -1
+ \else
+ \let\next=\relax
+ \fi
+ \next
+}
+%%
+%% \karnaughmaptabvert is the macro that a user calls if he wants to draw a
+%% Karnaugh map in vertical mode (not be confused with TeX vertical mode):
+%%
+\def\karnaughmaptabvert#1#2#3#4#5{%
+%%
+%% #1 is the number of variables in the Karnaugh map
+%% #2 is the identifier of the function
+%% #3 is the list of identifiers of those variables
+%% #4 is the list of tokens that have to be written into the map
+%% #5 a possibly empty set of TikZ commands that will be drown before
+%% the function values so the values will appear on top of them
+%%
+ \kmvarno=#1 % \kmvarno is the total number of variables
+ \kmxvarno=#1 % \kmxvarno is the number of variable bars on top
+ \divide\kmxvarno by 2
+ \kmyvarno=#1 % \kmyvarno is the number of variable bars at the left
+ \advance\kmyvarno by -\kmxvarno
+ \kmxsize=1
+ \kmpoweroftwo{\kmxsize}{\kmxvarno}
+ \kmysize=1
+ \kmpoweroftwo{\kmysize}{\kmyvarno}
+ \kmtemppos=\kmxsize
+ \advance\kmtemppos by -1
+ \ifnum\kmtemppos>0
+ \foreach \x in {1,...,\kmtemppos}
+ \draw[kmlines] (\x\kmunitlength,0) -- (\x\kmunitlength,\kmysize\kmunitlength);
+ \fi
+ \kmtemppos=\kmysize
+ \advance\kmtemppos by -1
+ \foreach \y in {1,...,\kmtemppos}
+ \draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength);
+ \draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength);
+ #5
+ \ifAmericanstyle
+ \kmtemppos=\kmxsize
+ \advance\kmtemppos by -1
+ \ifnum\kmtemppos>0
+ \pgfmathsetbasenumberlength{\kmxvarno}
+ \foreach \x in {0,...,\kmtemppos}
+ \node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber};
+ \fi
+ \kmtemppos=\kmysize
+ \advance\kmtemppos by -1
+ \pgfmathsetbasenumberlength{\kmyvarno}
+ \foreach \y in {0,...,\kmtemppos}
+ \node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber};
+ \kmargumentstring{#3}
+ \gdef\kmtoplabels{}
+ \gdef\kmleftlabels{}
+ \karnaughmakelabelstabvert
+ \kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length}
+ \draw[kmsep line] (0,\kmysize\kmunitlength) -- ++(-\kmtemplength,\kmtemplength) node[kmbar label,below left,pos=0.5]{\kmleftlabels} node[kmbar label,above right,pos=0.5]{\kmtoplabels} node[kmvar,above left]{#2};
+ \else
+ \ifdisablebars\relax\else
+ \node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
+ \kmargumentstring{#3}
+ \karnaughmakebarstabvert
+ \fi
+ \fi
+ \ifenableindices
+ \kmvarno=#1 % \kmvarno is the total number of variables
+ \ifindexbin
+ \gdef\kmcurrentindex{\kmcurrentindexbin}
+ \else
+ \ifindexGray
+ \gdef\kmcurrentindex{\kmcurrentindexGraytabvert}
+ \kmyvarno=#1 % \kmyvarno is the number of variable bars at the left
+ \divide\kmyvarno by 2
+ \kmxvarno=#1 % \kmxvarno is the number of variable bars on top
+ \advance\kmxvarno by -\kmyvarno
+ \else
+ \gdef\kmcurrentindex{\kmcurrentindexdec}
+ \fi
+ \fi
+ \fi
+ \kmindexcounter=0
+ \kmargumentstring{#4}
+ \advance\kmxsize by -1
+ \advance\kmysize by -1
+ \foreach \x in {0,...,\kmxsize} {
+ \foreach \y in {\kmysize,...,0} {
+ \kmsetoptstr{}
+ \node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (\x\kmunitlength,\y\kmunitlength){\kmstr};%
+ \ifenableindices
+ \node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}] at (\x\kmunitlength,\y\kmunitlength) {\kmcurrentindex};
+ \fi
+ }
+ }
+}%
\endinput \ No newline at end of file