diff options
Diffstat (limited to 'Master/texmf-dist/tex/latex')
-rw-r--r-- | Master/texmf-dist/tex/latex/tikz-karnaugh/tikz-karnaugh.tex | 300 |
1 files changed, 300 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/latex/tikz-karnaugh/tikz-karnaugh.tex b/Master/texmf-dist/tex/latex/tikz-karnaugh/tikz-karnaugh.tex new file mode 100644 index 00000000000..ac7b8680b64 --- /dev/null +++ b/Master/texmf-dist/tex/latex/tikz-karnaugh/tikz-karnaugh.tex @@ -0,0 +1,300 @@ +% This is file `tikz-karnaugh.tex', +%% Version 1.0 of 22nd December, 2017 +%% +%% Copyright (C) 2017 by Luis Paulo Laus, laus@utfpr.edu.br +%% +%% This package can be redistributed and/or modified under the terms +%% of the LaTeX Project Public License distributed from CTAN +%% archives in directory macros/latex/base/lppl.txt; either +%% version 1 of the License, or (at your option) any later version, +%% with `The Package' referring to the software `tikz-karnaugh.tex' and its +%% accompanying documentation and `The Copyright Holder' referring to the +%% person Luis Paulo Laus. +%% +%% This software is based on Andreas W. Wieland's kvmacros.tex to whom +%% the author wished to express his gratitude. +%% +%% IMPORTANT NOTICE: +%% +%% For error reports, comments or suggestions in case of UNCHANGED +%% versions send mail to: +%% laus@utfpr.edu.br +%% +%% +\typeout{} +\typeout{Macros for typesetting Karnaugh maps} +\typeout{Version of 22nd December, 2017} +\typeout{by Luis Paulo Laus, laus@utfpr.edu.br} +\typeout{} +%% +%% Change History: +%% 22nd December, 2017: Original Version ported from kvmacros.tex +%% +%% +%% +%% Setting up some TikZ parameters for an Karnaugh Map +%% +\tikzset{ + karnaugh/.style={ + kmbar/.style={black,|-|,thin}, + kmbar label/.style={black}, + bar sep/.style={kmbar sep={##1}}, + bar top sep/.style={kmbar top sep={##1}}, + bar left sep/.style={kmbar left sep={##1}}, + kmindex/.style={transparent}, + karnaugh index/.style={kmindex/.style={##1}}, + kmcell/.style={black}, + kmvar/.style={black}, + kmlines/.style={thin}, + every karnaugh/.try + } +} + +\pgfkeys{/pgf/.cd, + kmbar sep/.initial=0.2\kmunitlength, + kmbar top sep/.initial=1\kmunitlength, + kmbar left sep/.initial=1\kmunitlength, +} + +%% +%% We need a fixed dimension for a single field in a Karnaugh map +%% and also an auxiliary dimension to place de bars. +%% +\newdimen\kmunitlength +\newdimen\kmtemplength +\kmunitlength=8mm +%% +%% First, we have to introduce some counters: +%% +%% \kmrecursiondepth is used to control the recursion of the +%% \karnaughmakemap. +%% +\newcount\kmrecursiondepth +%% +%% The \kmindexcounter is needed for the indices in the fields of the +%% diagrams. +%% +\newcount\kmindexcounter +%% +%% \kmxsize and \kmysize store the dimensions of an entire diagram. +%% +\newcount\kmxsize +\newcount\kmysize +%% +%% Some counters are necessary to compute the marks for the variable +%% identifiers: +%% +\newcount\kmvarno +\newcount\kmxvarno +\newcount\kmyvarno +\newcount\kmmarkstart +\newcount\kmmarklength +\newcount\kmmarknum +\newcount\kmmarkmove +\newcount\kmtemppos +%% +%% And we need a box to store the variable names: +%% +\newbox\kmbox +%% +%% Single fields in a diagram should be indexed, which makes the map easier to +%% use. This is the default. If you don't want indices, simply call +%% \kmnoindex. If you want indices back, call \kmindex: +%% +\def\kmnoindex{% +\def\kmcurrentindex{}} +%% +\def\kmindex{% + \def\kmcurrentindex{% + \the\kmindexcounter\global\advance\kmindexcounter by 1}% +} +%% +\kmindex +%% +%% 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} + \fi}} +%% +%% The macros \kmargumentstring, \kmgetchar and \kmgetonechar are needed to +%% process the variable-length parameters in \karnaughmap: +%% +\def\kmargumentstring#1{\gdef\kmdummystring{#1{}\noexpand\end}} +%% +\def\kmgetchar{\expandafter\kmgetonechar\kmdummystring} +%% +\def\kmgetonechar#1#2\end{{#1}\gdef\kmdummystring{#2\noexpand\end}}% +%% +%% The Macro \karnaughmakemap calls itself recursively until the parameter #1 +%% equals 1, whereupon it returns a single token from the list of arguments, +%% enclosed in a \makebox plus the index (if enabled) in a smaller \makebox: +%% +\def\karnaughmakemap#1#2#3#4{{% + \kmrecursiondepth #1\relax + \ifnum\kmrecursiondepth>1 + \divide\kmrecursiondepth by 2 + \edef\tempx{#3} + \edef\tempy{#4} + \kmtemppos=\tempx\relax + \advance\kmtemppos by \kmrecursiondepth + \edef\tempxs{\the\kmtemppos} + \kmtemppos=\tempy\relax + \advance\kmtemppos by \kmrecursiondepth + \edef\tempys{\the\kmtemppos} + + \ifcase#2 +%% +%% The parameter #2 of \karnaughmakemap is needed because the inner Karnaugh +%% maps need to be mirrored. This is achieved by the following case-statement, +%% which orders the inner Karnaugh maps properly: +%% +%% Case 0: top-left Karnaugh map + \karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% + \karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% + \karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% + \karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% + \or +%% Case 1: top-right Karnaugh map + \karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% + \karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% + \karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% + \karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% + \or +%% Case 2: bottom-left Karnaugh map + \karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% + \karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% + \karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% + \karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% + \or +%% Case 3: bottom-right Karnaugh map + \karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% + \karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% + \karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% + \karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% + \fi + \else + \node[kmcell] at (#3\kmunitlength,#4\kmunitlength){\kmgetchar};% + \node[kmindex,shift={(-0.3\kmunitlength,0.3\kmunitlength)}] at (#3\kmunitlength,#4\kmunitlength){\kmcurrentindex};% + \fi}}% +%% +%% \karnaughmaketopmark typesets the variable marks of a Karnaugh map that are +%% located on top of the diagram: +%% +\def\karnaughmaketopmark{% + \kmmarkstart=1 + \kmpoweroftwo{\kmmarkstart}{\kmxvarno} % \kmymarkstart is the start + % position for the \multiput + \kmmarklength=\kmmarkstart + \multiply\kmmarklength by 2 % \kmmarklength is the length of a mark + \kmmarkmove=\kmmarkstart + \multiply\kmmarkmove by 4 % This is the move distance for the \multiput. + \kmmarknum=\kmxsize + \divide\kmmarknum by \kmmarkmove % This is the number of repetitions for + % the \multiput. + %The highest-order variable mark needs a special treatment: + \ifnum\kmmarknum=0\kmmarknum=1\divide\kmmarklength by 2\fi + \advance\kmmarkmove by \kmmarkstart + \kmtemplength=\pgfkeysvalueof{/pgf/kmbar top sep} + \multiply \kmtemplength by \kmyvarno + \advance \kmtemplength by \pgfkeysvalueof{/pgf/kmbar sep} + \advance \kmtemplength by \kmysize\kmunitlength + \ifnum\kmmarknum=1 + \draw[kmbar,xshift=\kmmarkstart\kmunitlength,yshift=\kmtemplength] (0,0) -- node[above, kmbar label]{\kmgetchar} (\kmmarklength\kmunitlength,0); + \else + \setbox\kmbox=\hbox{\kmgetchar} + \foreach \x in {\kmmarkstart,\kmmarkmove,...,\kmxsize} + \draw[kmbar,xshift=\x\kmunitlength,yshift=\kmtemplength] (0,0) -- node[above, kmbar label]{\copy\kmbox} (\kmmarklength\kmunitlength,0); + \fi +} +%% +%% \karnaughmakeleftmark typesets the variable marks of a Karnaugh map that are +%% located on the left of the diagram: +%% +\def\karnaughmakeleftmark{% + \kmmarkstart=1 + \kmpoweroftwo{\kmmarkstart}{\kmyvarno} % \kmmarkstart is the start + % position for the \multiput + \kmmarklength=\kmmarkstart + \multiply\kmmarklength by 2 % \kmmarklength is the length of a mark + \kmmarkmove=\kmmarkstart + \multiply\kmmarkmove by 4 % This now is the move distance for the + % \multiput. + \kmmarknum=\kmysize + \divide\kmmarknum by \kmmarkmove % This now is the number of + % repetitions for the \multiput. + %The highest-order variable mark needs a special treatment: + \ifnum\kmmarknum=0\kmmarkstart=0\kmmarknum=1\divide\kmmarklength by 2\fi + \advance\kmmarkmove by \kmmarkstart + \kmtemplength=-\pgfkeysvalueof{/pgf/kmbar left sep} + \multiply \kmtemplength by \kmyvarno + \advance \kmtemplength by -\pgfkeysvalueof{/pgf/kmbar sep} + \ifnum\kmmarknum=1 + \draw[kmbar,yshift=\kmmarkstart\kmunitlength,xshift=\kmtemplength] (0,0) -- node[left,kmbar label]{\kmgetchar} (0,\kmmarklength\kmunitlength); + \else + \setbox\kmbox=\hbox{\kmgetchar} + \foreach \y in {\kmmarkstart,\kmmarkmove,...,\kmysize} + \draw[kmbar,yshift=\y\kmunitlength,xshift=\kmtemplength] (0,0) -- node[left,kmbar label]{\copy\kmbox} (0,\kmmarklength\kmunitlength); + \fi +} +%% \karnaughmakemarks calls \karnaughmaketopmark or \karnaughmakeleftmark +%% depending on whether \kmvarno is odd or even. +%% +\def\karnaughmakemarks{% + \ifnum\kmvarno>0 + \let\next=\karnaughmakemarks + \ifodd\kmvarno % We have to make a mark at the top + \advance\kmxvarno by -1 + \karnaughmaketopmark + \else % We have to make a mark at the left + \advance\kmyvarno by -1 + \karnaughmakeleftmark + \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\karnaughmap#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 +%% + \kmvarno=#1 % \kmvarno is the total number of variables + \kmyvarno=#1 % \kmyvarno is the number of variable marks at the left + \divide\kmyvarno by 2 + \kmxvarno=#1 % \kmxvarno is the number of variable marks on top + \advance\kmxvarno by -\kmyvarno + \kmxsize=1 + \kmpoweroftwo{\kmxsize}{\kmxvarno} + \kmysize=1 + \kmpoweroftwo{\kmysize}{\kmyvarno} + \node[kmvar,above left] at (0,\kmysize\kmunitlength){#2};% + \foreach \x in {0,...,\kmxsize} + \draw[kmlines] (\x\kmunitlength,0) -- (\x\kmunitlength,\kmysize\kmunitlength); + \foreach \y in {0,...,\kmysize} + \draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength); + #5 + \kmargumentstring{#3} + \karnaughmakemarks + \pgftransformshift{\pgfpoint{0.5\kmunitlength}{0.5\kmunitlength}} + \kmvarno=#1 % \kmvarno is the total number of variables + \kmindexcounter=0 + \kmargumentstring{#4} + \karnaughmakemap{\the\kmysize}{0}{0}{0} + \ifodd\kmvarno + {\divide\kmxsize by 2 + \karnaughmakemap{\the\kmysize}{1}{\the\kmxsize}{0}} + \fi + \pgftransformreset +}% |