From c682a2af4035b8fc25ef591f11edb7e57bd51179 Mon Sep 17 00:00:00 2001
From: Norbert Preining <norbert@preining.info>
Date: Thu, 14 Oct 2021 03:01:09 +0000
Subject: CTAN sync 202110140301

---
 .../tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex | 300 +++++++++++++++++++--
 1 file changed, 283 insertions(+), 17 deletions(-)

(limited to 'graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex')

diff --git a/graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex b/graphics/pgf/contrib/tikz-karnaugh/tex/tikzlibrarykarnaugh.code.tex
index 5ff82dab66..330cb6f3a5 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.2
-%% Version date: 2018-03-19
+%% Version: 1.3
+%% Version date: 2021-10-14
 %% 
 %% 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.2 of 19 March 2018}
+\typeout{Version 1.3 of 14 October 2021}
 \typeout{by Luis Paulo Laus, laus@utfpr.edu.br}
 \typeout{}
 %%
@@ -36,6 +36,9 @@
 %% (kmindexposx and kmindexposy) 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.
 %%
 %% Setting up some TikZ parameters for Karnaugh Maps
 %%
@@ -49,10 +52,14 @@
     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}},
     binary index/.is if=indexbin,
+    Gray index/.is if=indexGray,
+    American style/.is if=Americanstyle,
     kmcell/.style={},
     kmvar/.style={},
     kmbox/.style={},
@@ -66,13 +73,17 @@
 %%
 \newif\ifenableindices
 \enableindicesfalse
+\newif\ifindexGray
+\indexGrayfalse
 \newif\ifdisablebars
 \disablebarsfalse
 \newif\ifindexbin
 \indexbinfalse
+\newif\ifAmericanstyle
+\Americanstylefalse
 %%
 %% We need a fixed dimension for a single field in a Karnaugh map
-%% and also an auxiliary dimension to place the bars.
+%% and also, an auxiliary dimension to place the bars.
 %%
 \newdimen\kmunitlength
 \newdimen\kmtemplength
@@ -112,11 +123,13 @@
 %%
 \def\kmcurrentindex{\kmcurrentindexdec}
 \def\kmcurrentindexdec{%
-\the\kmindexcounter\global\advance\kmindexcounter by 1}%
-
+    \the\kmindexcounter\global\advance\kmindexcounter by 1}%
 \def\kmcurrentindexbin{%
-\pgfmathdectobase\mynumber{\the\kmindexcounter}{2} \mynumber\global\advance\kmindexcounter by 1}%
-
+    \kmdectobin{\kmindexcounter}\global\advance\kmindexcounter by 1}%
+\def\kmcurrentindexGray{%
+    \kmdectoKG{\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:
 %%
@@ -127,6 +140,127 @@
     \kmpoweroftwo{#1}{#2}
   \fi}}
 %%
+%% \pgfmathdectoGray
+%%
+%% Convert a representation of an integer from
+%% base 10 to Gray Code.
+%%
+%% #1 - a macro to store the result.
+%% #2 - a number in base 10 (in a macro, not a register)
+%%
+%% e.g.
+%%
+%% \pgfmathdectoGray\mynumber{9}
+%%
+%% \mynumber <- 1101
+%%
+{\makeatletter
+\gdef\pgfmathdectoGray#1#2{%
+    \pgfmathdectobase{\pgfresult@}{#2}{2}%
+    \c@pgfmath@counta=0\relax%
+    \let\pgfresult@@=\pgfmath@empty%
+    \let\pgf@lib@sh@next\pgfmathdectoGray@%
+    \expandafter\pgfmathdectoGray@\pgfresult@\pgf@stop%
+    \let#1=\pgfresult@@%
+    \pgfmath@ensurenumberlength{#1}}
+\gdef\pgfmathdectoGray@#1{%
+    \ifx#1\pgf@stop%
+        \let\pgf@lib@sh@next\relax%
+    \else%
+        \edef\pgfresult@@{\pgfresult@@\ifnum#1=\c@pgfmath@counta\relax0\else1\fi}%
+        \c@pgfmath@counta=#1\relax%
+    \fi%
+    \pgf@lib@sh@next}
+%%
+%% The macros \kmdectoKG converts the cell index
+%% to a binary code that resembles Gray code 
+%% deinterleaving the variables 
+%%
+\gdef\kmdectoKG#1{%
+    \kmbarnum=#1\relax% decimal number to be converted
+    \kmbarlength=\kmvarno\relax% number of bits
+    \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\next\kmdectoKG@%
+    \next%
+    \kmresult@\kmresult@@}
+\gdef\kmdectoKG@{%
+    \ifnum\kmbarlength=0%
+        \let\next\relax%
+    \else%
+        \edef\kmresult@{\ifodd\kmbarnum1\else0\fi\kmresult@}%
+        \divide \kmbarnum by 2%
+        \advance\kmbarlength by -1\relax%
+        \let\next\kmdectoKG@@%
+    \fi%
+    \next}
+\gdef\kmdectoKG@@{%
+    \ifnum\kmbarlength=0%
+        \let\next\relax%
+    \else%
+        \edef\kmresult@@{\ifodd\kmbarnum1\else0\fi\kmresult@@}%
+        \divide \kmbarnum by 2%
+        \advance\kmbarlength by -1\relax%
+        \let\next\kmdectoKG@%
+    \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 
+%%
+%% e.g.
+%%
+%% \kmvarno=6\relax\kmdectobin{9}
+%%
+%% 001001
+%% 
+\gdef\kmdectobin#1{%
+    \kmbarnum=#1\relax% decimal number to be converted
+    \kmbarlength=\kmvarno\relax% number of bits
+    \let\kmresult@=\empty
+    \let\next\kmdectobin@%
+    \next%
+    \kmresult@}
+\gdef\kmdectobin@{%
+    \ifnum\kmbarlength=0%
+        \let\next\relax%
+    \else%
+        \edef\kmresult@{\ifodd\kmbarnum1\else0\fi\kmresult@}%
+        \divide \kmbarnum by 2%
+        \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 \kmargumentstring and \kmsetoptstr are needed to
 %% process the variable-length parameters in \karnaughmap:
 %%
@@ -279,7 +413,7 @@
     \ifodd\kmvarno % We have to make a bar at the top
       \advance\kmxvarno by -1
       \karnaughmaketopbar
-    \else          % We have to make a bar at the left
+    \else % We have to make a bar at the left
       \advance\kmyvarno by -1
       \karnaughmakeleftbar
     \fi
@@ -289,6 +423,38 @@
   \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
+%%
+\def\karnaughmakelabels{%
+  \ifnum\kmvarno>0
+    \let\next=\karnaughmakelabels
+    \ifodd\kmvarno % 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
+    \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
+}
+
 %%
 %% \karnaughmap is the macro that a user calls if he wants to draw a
 %% Karnaugh map: 
@@ -302,7 +468,6 @@
 %% #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
 %%
-  \ifenableindices\ifindexbin\gdef\kmcurrentindex{\kmcurrentindexbin}\pgfmathsetbasenumberlength{#1}\else\gdef\kmcurrentindex{\kmcurrentindexdec}\fi\fi
   \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
@@ -314,6 +479,7 @@
   \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
@@ -324,10 +490,45 @@
   \fi
   \draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength);
   #5
-  \ifdisablebars\relax\else
-    \node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
+  \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};
+    \fi
     \kmargumentstring{#3}
-    \karnaughmakebars
+    \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};
+  \else
+    \ifdisablebars\relax\else
+      \node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
+      \kmargumentstring{#3}
+      \karnaughmakebars
+    \fi
+  \fi
+  \ifenableindices
+     \ifindexbin
+        \gdef\kmcurrentindex{\kmcurrentindexbin}
+        \pgfmathsetbasenumberlength{#1}
+     \else
+        \ifindexGray
+           \gdef\kmcurrentindex{\kmcurrentindexGray}
+        \else
+           \gdef\kmcurrentindex{\kmcurrentindexdec}
+        \fi
+     \fi
   \fi
   \kmvarno=#1 % \kmvarno is the total number of variables 
   \kmindexcounter=0
@@ -420,6 +621,37 @@
   \fi
   \next
 }
+
+%% \karnaughmakelabelsvert is a modification of \karnaughmakebars.
+%% Instead of making the bars it sets two macros, \kmtoplabels
+%% and \kmleftlabels, with the labels for americal style maps
+%%
+\def\karnaughmakelabelsvert{%
+  \ifnum\kmvarno>0
+    \let\next=\karnaughmakelabelsvert
+    \ifodd\kmvarno % 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
+    \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
+}
 %%
 %% \karnaughmapvert 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): 
@@ -433,7 +665,6 @@
 %% #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
 %%
-  \ifenableindices\ifindexbin\gdef\kmcurrentindex{\kmcurrentindexbin}\pgfmathsetbasenumberlength{#1}\else\gdef\kmcurrentindex{\kmcurrentindexdec}\fi\fi
   \kmvarno=#1 % \kmvarno is the total number of variables 
   \kmxvarno=#1 % \kmxvarno is the number of variable bars on top
   \divide\kmxvarno by 2
@@ -455,10 +686,45 @@
     \draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength);
   \draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength);
   #5
-  \ifdisablebars\relax\else
-    \node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
+  \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};
+    \fi
     \kmargumentstring{#3}
-    \karnaughmakebarsvert
+    \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};
+  \else
+    \ifdisablebars\relax\else
+      \node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};
+      \kmargumentstring{#3}
+      \karnaughmakebarsvert
+    \fi
+  \fi
+  \ifenableindices
+     \ifindexbin
+        \gdef\kmcurrentindex{\kmcurrentindexbin}
+        \pgfmathsetbasenumberlength{#1}
+     \else
+        \ifindexGray
+           \gdef\kmcurrentindex{\kmcurrentindexGrayvert}
+        \else
+           \gdef\kmcurrentindex{\kmcurrentindexdec}
+        \fi
+     \fi
   \fi
   \kmvarno=#1 % \kmvarno is the total number of variables 
   \kmindexcounter=0
-- 
cgit v1.2.3

