| 1 |
%% This is file `tikzlibrarykarnaugh.code.tex' formerly known as `tikz-karnaugh.tex', |
| 2 |
%% Version: 1.5 |
| 3 |
%% Version date: 2022-02-15 |
| 4 |
%% |
| 5 |
%% Copyright (C) 2017-2022 by Luis Paulo Laus, [email protected] |
| 6 |
%% |
| 7 |
%% This package can be redistributed and/or modified under the terms |
| 8 |
%% of the LaTeX Project Public License distributed from CTAN |
| 9 |
%% archives in directory macros/latex/base/lppl.txt; either |
| 10 |
%% version 1 of the License, or (at your option) any later version, |
| 11 |
%% with `The Package' referring to the software |
| 12 |
%% `tikzlibrarykarnaugh.code.tex' |
| 13 |
%% and its accompanying documentation and `The Copyright Holder' |
| 14 |
%% referring to the person Luis Paulo Laus. |
| 15 |
%% |
| 16 |
%% |
| 17 |
%% IMPORTANT NOTICE: |
| 18 |
%% |
| 19 |
%% For error reports, comments or suggestions in case of UNCHANGED |
| 20 |
%% versions send mail to: |
| 21 |
%% [email protected] |
| 22 |
%% |
| 23 |
\typeout{} |
| 24 |
\typeout{Macros for typesetting Karnaugh maps} |
| 25 |
\typeout{Version 1.5 of 15 February 2022} |
| 26 |
\typeout{by Luis Paulo Laus, [email protected]} |
| 27 |
\typeout{} |
| 28 |
%% |
| 29 |
%% Change History: |
| 30 |
%% 1.0: 22 December 2017: Original Version ported from kvmacros.tex |
| 31 |
%% 1.1: 10 January 2018: new style for the map outer box. Support for |
| 32 |
%% options in the identifiers and values lists. New vertical mode. |
| 33 |
%% New keys to enable indices and disable bars. Macro file renamed |
| 34 |
%% to proper tikz library file name. |
| 35 |
%% 1.2: 19 March 2018: stop using \pgftransformshift, new kmindex pos |
| 36 |
%% (kmindex posx and kmindex posy) to control index position, cell |
| 37 |
%% typesetting order switched (first content then index), parameter |
| 38 |
%% names changed to include ``km'' letters, new binary index mode |
| 39 |
%% 1.3: 14 October 2021: New American style KM (for maps with ones |
| 40 |
%% and zeros labels surrounding the map). In addition, a new macro for |
| 41 |
%% decimal to Gray Code conversion (\pgfmathdectoGray) is provided. |
| 42 |
%% 1.4: 8 February 2022: new style and length for American style; |
| 43 |
%% change some key names (standard); new 'karnaugh cell size' key; |
| 44 |
%% bug fixes; positioning of labels simplified; two new macros for type |
| 45 |
%% setting maps as they should appear and not using the truth table order. |
| 46 |
%% 1.5: 15 February 2022: bug fixes on Gray code generation |
| 47 |
%% |
| 48 |
%% Setting up some TikZ parameters for Karnaugh Maps |
| 49 |
%% |
| 50 |
\tikzset{ |
| 51 |
karnaugh/.style={ |
| 52 |
disable bars/.is if=disablebars, |
| 53 |
kmbar/.style={|-|}, |
| 54 |
kmbar label/.style={}, |
| 55 |
kmbar sep/.initial=0.2\kmunitlength, |
| 56 |
kmbar top sep/.initial=1\kmunitlength, |
| 57 |
kmbar left sep/.initial=1\kmunitlength, |
| 58 |
enable indices/.is if=enableindices, |
| 59 |
kmindex/.style={red,font=\tiny}, |
| 60 |
kmindex posx/.initial=0.2\kmunitlength, |
| 61 |
kmindex posy/.initial=0.8\kmunitlength, |
| 62 |
kmindex pos/.style 2 args={kmindex posx={##1\kmunitlength},kmindex posy={##2\kmunitlength}}, |
| 63 |
binary index/.is if=indexbin, |
| 64 |
Gray index/.is if=indexGray, |
| 65 |
American style/.is if=Americanstyle, |
| 66 |
kmsep line/.style={}, |
| 67 |
kmsep line length/.initial=1\kmunitlength, |
| 68 |
kmlabel top/.style={above,blue,font=\footnotesize\ttfamily}, |
| 69 |
kmlabel left/.style={left,blue,font=\footnotesize\ttfamily}, |
| 70 |
kmcell/.style={}, |
| 71 |
kmvar/.style={}, |
| 72 |
kmbox/.style={}, |
| 73 |
kmlines/.style={}, |
| 74 |
x=1\kmunitlength, |
| 75 |
y=1\kmunitlength, |
| 76 |
every karnaugh/.try |
| 77 |
}, |
| 78 |
karnaugh cell size/.code=\pgfmathsetlength\kmunitlength{#1} |
| 79 |
} |
| 80 |
|
| 81 |
%% |
| 82 |
%% New ifs for the options above |
| 83 |
%% |
| 84 |
\newif\ifenableindices |
| 85 |
\enableindicesfalse |
| 86 |
\newif\ifindexGray |
| 87 |
\indexGrayfalse |
| 88 |
\newif\ifdisablebars |
| 89 |
\disablebarsfalse |
| 90 |
\newif\ifindexbin |
| 91 |
\indexbinfalse |
| 92 |
\newif\ifAmericanstyle |
| 93 |
\Americanstylefalse |
| 94 |
%% |
| 95 |
%% We need a fixed dimension for a single field in a Karnaugh map |
| 96 |
%% and also, an auxiliary dimension to place the bars. |
| 97 |
%% |
| 98 |
\newdimen\kmunitlength |
| 99 |
\newdimen\kmtemplength |
| 100 |
\kmunitlength=8mm |
| 101 |
%% |
| 102 |
%% First, we have to introduce some counters: |
| 103 |
%% |
| 104 |
%% \kmrecursiondepth is used to control the recursion of the |
| 105 |
%% \karnaughmakemap macro. |
| 106 |
%% |
| 107 |
\newcount\kmrecursiondepth |
| 108 |
%% |
| 109 |
%% The \kmindexcounter is needed for the indices in the fields of the |
| 110 |
%% diagrams. |
| 111 |
%% |
| 112 |
\newcount\kmindexcounter |
| 113 |
%% |
| 114 |
%% \kmxsize and \kmysize store the dimensions of an entire diagram. |
| 115 |
%% |
| 116 |
\newcount\kmxsize |
| 117 |
\newcount\kmysize |
| 118 |
%% |
| 119 |
%% Some counters are necessary to compute the bars for the variable |
| 120 |
%% identifiers. Also used as temp varaibles in code conversion: |
| 121 |
%% |
| 122 |
\newcount\kmvarno |
| 123 |
\newcount\kmxvarno |
| 124 |
\newcount\kmyvarno |
| 125 |
\newcount\kmbarstart |
| 126 |
\newcount\kmbarlength |
| 127 |
\newcount\kmbarnum |
| 128 |
\newcount\kmbarmove |
| 129 |
\newcount\kmtemppos |
| 130 |
%% |
| 131 |
%% Single cells in a diagram should be indexed, which makes the map easier to |
| 132 |
%% use (ok, not really, but it might be useful). |
| 133 |
%% |
| 134 |
\def\kmcurrentindex{\kmcurrentindexdec} |
| 135 |
\def\kmcurrentindexdec{% |
| 136 |
\the\kmindexcounter\global\advance\kmindexcounter by 1}% |
| 137 |
\def\kmcurrentindexbin{% |
| 138 |
\kmdectobin{\kmindexcounter}\global\advance\kmindexcounter by 1}% |
| 139 |
\def\kmcurrentindexGray{% |
| 140 |
\kmdectoKG{\kmindexcounter}\global\advance\kmindexcounter by 1}% |
| 141 |
\def\kmcurrentindexGraytab{% |
| 142 |
\kmdectoKGtab{\kmindexcounter}\global\advance\kmindexcounter by 1}% |
| 143 |
\def\kmcurrentindexGrayvert{% |
| 144 |
\kmdectoKGvert{\kmindexcounter}\global\advance\kmindexcounter by 1}% |
| 145 |
\def\kmcurrentindexGraytabvert{% |
| 146 |
\kmdectoKGtabvert{\kmindexcounter}\global\advance\kmindexcounter by 1}% |
| 147 |
%% |
| 148 |
%% We need a macro that computes the powers of two. |
| 149 |
%% Computes #1=2^#2, both of which have to be counters and |
| 150 |
%% both are modified |
| 151 |
%% |
| 152 |
\def\kmpoweroftwo#1#2{{% |
| 153 |
\ifnum#2>0% |
| 154 |
\global\multiply#1 by 2% |
| 155 |
\advance#2 by -1% |
| 156 |
\kmpoweroftwo{#1}{#2}% |
| 157 |
\fi}} |
| 158 |
%% |
| 159 |
%% \pgfmathdectoGray |
| 160 |
%% |
| 161 |
%% Convert a representation of an integer from |
| 162 |
%% base 10 to Gray Code. |
| 163 |
%% |
| 164 |
%% #1 - a macro to store the result. |
| 165 |
%% #2 - a number in base 10 (in a macro, not a register) |
| 166 |
%% |
| 167 |
%% e.g. |
| 168 |
%% |
| 169 |
%% \pgfmathdectoGray\mynumber{9} |
| 170 |
%% |
| 171 |
%% \mynumber <- 1101 |
| 172 |
%% |
| 173 |
{\makeatletter |
| 174 |
\gdef\pgfmathdectoGray#1#2{% |
| 175 |
\pgfmathdectobase{\pgfresult@}{#2}{2}% |
| 176 |
\c@pgfmath@counta=0\relax% |
| 177 |
\let\pgfresult@@=\pgfmath@empty% |
| 178 |
\let\pgf@lib@sh@next\pgfmathdectoGray@% |
| 179 |
\expandafter\pgfmathdectoGray@\pgfresult@\pgf@stop% |
| 180 |
\let#1=\pgfresult@@% |
| 181 |
\pgfmath@ensurenumberlength{#1}} |
| 182 |
\gdef\pgfmathdectoGray@#1{% |
| 183 |
\ifx#1\pgf@stop% |
| 184 |
\let\pgf@lib@sh@next\relax% |
| 185 |
\else% |
| 186 |
\edef\pgfresult@@{\pgfresult@@\ifnum#1=\c@pgfmath@counta\relax0\else1\fi}% xor |
| 187 |
\c@pgfmath@counta=#1\relax% |
| 188 |
\fi% |
| 189 |
\pgf@lib@sh@next} |
| 190 |
%% |
| 191 |
%% The macros \kmdectoKG converts the cell index |
| 192 |
%% to a binary code that resembles Gray code |
| 193 |
%% deinterleaving the variables |
| 194 |
%% |
| 195 |
\gdef\kmdectoKG#1{% |
| 196 |
\kmbarnum=#1\relax% decimal number to be converted |
| 197 |
\kmbarlength=\kmvarno\relax% number of bits |
| 198 |
\let\kmresult@=\empty% |
| 199 |
\let\kmresult@@=\empty% |
| 200 |
\let\next\kmdectoKG@% |
| 201 |
\next% |
| 202 |
\kmresult@@\kmresult@} |
| 203 |
\gdef\kmdectoKGvert#1{% |
| 204 |
\kmbarnum=#1\relax% decimal number to be converted |
| 205 |
\kmbarlength=\kmvarno\relax% number of bits |
| 206 |
\let\kmresult@=\empty% |
| 207 |
\let\kmresult@@=\empty% |
| 208 |
\let\next\kmdectoKG@% |
| 209 |
\next% |
| 210 |
\kmresult@\kmresult@@} |
| 211 |
\gdef\kmdectoKG@{% |
| 212 |
\ifnum\kmbarlength=0% |
| 213 |
\let\next\relax% |
| 214 |
\else% |
| 215 |
\edef\kmresult@{\ifodd\kmbarnum1\else0\fi\kmresult@}% |
| 216 |
\divide \kmbarnum by 2% |
| 217 |
\advance\kmbarlength by -1\relax% |
| 218 |
\let\next\kmdectoKG@@% |
| 219 |
\fi% |
| 220 |
\next} |
| 221 |
\gdef\kmdectoKG@@{% |
| 222 |
\ifnum\kmbarlength=0% |
| 223 |
\let\next\relax% |
| 224 |
\else% |
| 225 |
\edef\kmresult@@{\ifodd\kmbarnum1\else0\fi\kmresult@@}% |
| 226 |
\divide \kmbarnum by 2% |
| 227 |
\advance\kmbarlength by -1\relax% |
| 228 |
\let\next\kmdectoKG@% |
| 229 |
\fi% |
| 230 |
\next} |
| 231 |
%% |
| 232 |
%% The macros \kmdectoKGtab converts the cell index |
| 233 |
%% to a binary code that resembles Gray code |
| 234 |
%% without deinterleaving the variables. |
| 235 |
%% Used in Table Mode |
| 236 |
%% |
| 237 |
\gdef\kmdectoKGtab#1{% |
| 238 |
\kmbarnum=#1\relax% decimal number to be converted |
| 239 |
\kmbarlength=\kmvarno\relax% number of bits |
| 240 |
\let\kmresult@=\empty% |
| 241 |
\let\kmresult@@=\empty% |
| 242 |
\let\next\kmdectobin@% |
| 243 |
\next% |
| 244 |
\kmbarlength=\kmvarno\relax% number of bits |
| 245 |
\kmbarmove=0\relax% last bit |
| 246 |
\let\next\kmdectoKG@tab% |
| 247 |
\expandafter\next\kmresult@\end% |
| 248 |
\kmresult@@} |
| 249 |
\gdef\kmdectoKG@tab#1{% |
| 250 |
\ifx#1\end% |
| 251 |
\let\next\relax% |
| 252 |
\else% |
| 253 |
\edef\kmresult@@{\kmresult@@\ifnum#1=\kmbarmove\relax0\else1\fi}% xor |
| 254 |
\kmbarmove=#1\relax% |
| 255 |
\advance\kmbarlength by -1\relax% |
| 256 |
\ifnum\kmbarlength=\kmxvarno\kmbarmove=0\fi% |
| 257 |
\fi% |
| 258 |
\next}% |
| 259 |
\gdef\kmdectoKGtabvert#1{% |
| 260 |
\kmbarnum=#1\relax% decimal number to be converted |
| 261 |
\kmbarlength=\kmvarno\relax% number of bits |
| 262 |
\let\kmresult@=\empty% |
| 263 |
\let\kmresult@@=\empty% |
| 264 |
\let\kmresult@@@=\empty% |
| 265 |
\let\next\kmdectobin@% |
| 266 |
\next% |
| 267 |
\kmbarlength=\kmvarno\relax% number of bits |
| 268 |
\kmbarmove=0\relax% last bit |
| 269 |
\let\next\kmdectoKG@tabvert% |
| 270 |
\expandafter\next\kmresult@\end% |
| 271 |
\kmresult@@@\kmresult@@} |
| 272 |
\gdef\kmdectoKG@tabvert#1{% |
| 273 |
\ifx#1\end% |
| 274 |
\let\next\relax% |
| 275 |
\else% |
| 276 |
\edef\kmresult@@{\kmresult@@\ifnum#1=\kmbarmove\relax0\else1\fi}% xor |
| 277 |
\kmbarmove=#1\relax% |
| 278 |
\advance\kmbarlength by -1\relax% |
| 279 |
\ifnum\kmbarlength=\kmxvarno% |
| 280 |
\kmbarmove=0\relax% |
| 281 |
\let\next\kmdectoKG@@tabvert% |
| 282 |
\fi% |
| 283 |
\fi% |
| 284 |
\next} |
| 285 |
\gdef\kmdectoKG@@tabvert#1{% |
| 286 |
\ifx#1\end% |
| 287 |
\let\next\relax% |
| 288 |
\else% |
| 289 |
\edef\kmresult@@@{\kmresult@@@\ifnum#1=\kmbarmove\relax0\else1\fi}% xor |
| 290 |
\kmbarmove=#1\relax% |
| 291 |
\fi |
| 292 |
\next} |
| 293 |
%% |
| 294 |
%% The macros \kmdectobin converts a decimal number |
| 295 |
%% passed as argument to binary such that the number of |
| 296 |
%% digits (length) is equal to \kmvarno |
| 297 |
%% |
| 298 |
%% e.g. |
| 299 |
%% |
| 300 |
%% \kmvarno=6\relax\kmdectobin{9} |
| 301 |
%% |
| 302 |
%% 001001 |
| 303 |
%% |
| 304 |
\gdef\kmdectobin#1{% |
| 305 |
\kmbarnum=#1\relax% decimal number to be converted |
| 306 |
\kmbarlength=\kmvarno\relax% number of bits |
| 307 |
\let\kmresult@=\empty |
| 308 |
\let\next\kmdectobin@% |
| 309 |
\next% |
| 310 |
\kmresult@} |
| 311 |
\gdef\kmdectobin@{% |
| 312 |
\ifnum\kmbarlength=0% |
| 313 |
\let\next\relax% |
| 314 |
\else% |
| 315 |
\edef\kmresult@{\ifodd\kmbarnum1\else0\fi\kmresult@}% |
| 316 |
\divide \kmbarnum by 2% |
| 317 |
\advance\kmbarlength by -1\relax% |
| 318 |
\fi% |
| 319 |
\next} |
| 320 |
%% |
| 321 |
%% The macros \kmdectoKGdec coverts an index to |
| 322 |
%% pseudo Gray code in decimal (test purpose). |
| 323 |
%% No vertical mode available |
| 324 |
%% |
| 325 |
\gdef\kmdectoKGdec#1{% |
| 326 |
\kmbarnum=#1\relax% decimal number to be converted |
| 327 |
\kmbarstart=0\relax% |
| 328 |
\kmbarmove=0\relax% |
| 329 |
\kmbarlength=1\relax% |
| 330 |
\c@pgfmath@counta=\kmxsize\relax% |
| 331 |
\kmdectoKGdec@% |
| 332 |
\advance\kmbarmove by \kmbarstart% |
| 333 |
\the\kmbarmove} |
| 334 |
\gdef\kmdectoKGdec@{% |
| 335 |
\ifnum\kmbarnum=0% |
| 336 |
\let\next\relax% |
| 337 |
\else% |
| 338 |
\ifodd\kmbarnum\advance\kmbarstart by \kmbarlength\fi% |
| 339 |
\multiply\kmbarlength by 2% |
| 340 |
\divide \kmbarnum by 2% |
| 341 |
\let\next\kmdectoKGdec@@% |
| 342 |
\fi% |
| 343 |
\next} |
| 344 |
\gdef\kmdectoKGdec@@{% |
| 345 |
\ifnum\kmbarnum=0% |
| 346 |
\let\next\relax% |
| 347 |
\else% |
| 348 |
\ifodd\kmbarnum\advance\kmbarmove by \c@pgfmath@counta\fi% |
| 349 |
\multiply\c@pgfmath@counta by 2% |
| 350 |
\divide \kmbarnum by 2% |
| 351 |
\let\next\kmdectoKGdec@% |
| 352 |
\fi% |
| 353 |
\next} |
| 354 |
} % makeatletter |
| 355 |
%% |
| 356 |
%% The macros \kmargumentstring and \kmsetoptstr are needed to |
| 357 |
%% process the variable-length parameters in \karnaughmap: |
| 358 |
%% |
| 359 |
\def\kmargumentstring#1{\gdef\kmstringbuf{#1{}\noexpand\end}} |
| 360 |
%% |
| 361 |
%% The macro \kmsetoptstr reads one token from the list of parameters in |
| 362 |
%% \karnaughmap in the form <[opt]>srt and sets the optional style kmtempsty |
| 363 |
%% and saves its contents of the string srt in macro \kmstr |
| 364 |
%% |
| 365 |
\def\kmsetoptstr{\expandafter\kmgetonetok\kmstringbuf} |
| 366 |
%% |
| 367 |
%% The macros \kmgetonetok, \kmsplittok and \kmoptstrmake are |
| 368 |
%% auxiliary macros used to split the token and save to contents |
| 369 |
%% |
| 370 |
\def\kmgetonetok#1#2\end{\expandafter\kmsplittok#1{}\end \gdef\kmstringbuf{#2\noexpand\end}} |
| 371 |
\def\kmsplittok#1#2\end{\ifx#1[ \kmoptstrmake#2\end \else \kmoptstrmake]#1#2\end \fi} |
| 372 |
\def\kmoptstrmake#1]#2\end{\tikzset{kmtempsty/.style={#1}} \edef\kmstr{#2}}%\typeout{Teste #1 e #2}} |
| 373 |
%%\def\kmoptstrmake#1]#2\end{Test: #1 and #2.\par} |
| 374 |
%% |
| 375 |
%% The macro \karnaughmakemap calls itself recursively until the parameter #1 |
| 376 |
%% equals 1, whereupon it draws one cell contents in a node and, if enabled, |
| 377 |
%% another node with the index. |
| 378 |
%% |
| 379 |
\def\karnaughmakemap#1#2#3#4{{% |
| 380 |
\kmrecursiondepth #1\relax |
| 381 |
\ifnum\kmrecursiondepth>1 |
| 382 |
\divide\kmrecursiondepth by 2 |
| 383 |
\edef\tempx{#3} |
| 384 |
\edef\tempy{#4} |
| 385 |
\kmtemppos=\tempx\relax |
| 386 |
\advance\kmtemppos by \kmrecursiondepth |
| 387 |
\edef\tempxs{\the\kmtemppos} |
| 388 |
\kmtemppos=\tempy\relax |
| 389 |
\advance\kmtemppos by \kmrecursiondepth |
| 390 |
\edef\tempys{\the\kmtemppos} |
| 391 |
|
| 392 |
\ifcase#2 |
| 393 |
%% |
| 394 |
%% The parameter #2 of \karnaughmakemap is needed because the inner Karnaugh |
| 395 |
%% maps need to be mirrored. This is achieved by the following case-statement, |
| 396 |
%% which orders the inner Karnaugh maps properly: |
| 397 |
%% +---+---+ |
| 398 |
%% | 0 | 1 | |
| 399 |
%% +---+---+ |
| 400 |
%% | 2 | 3 | |
| 401 |
%% +---+---+ |
| 402 |
%% |
| 403 |
%% Case 0: top-left Karnaugh map |
| 404 |
\karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 405 |
\karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% |
| 406 |
\karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% |
| 407 |
\karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 408 |
\or |
| 409 |
%% Case 1: top-right Karnaugh map |
| 410 |
\karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% |
| 411 |
\karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 412 |
\karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 413 |
\karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% |
| 414 |
\or |
| 415 |
%% Case 2: bottom-left Karnaugh map |
| 416 |
\karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% |
| 417 |
\karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 418 |
\karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 419 |
\karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% |
| 420 |
\or |
| 421 |
%% Case 3: bottom-right Karnaugh map |
| 422 |
\karnaughmakemap{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 423 |
\karnaughmakemap{\kmrecursiondepth}{2}{\tempx}{\tempy}% |
| 424 |
\karnaughmakemap{\kmrecursiondepth}{1}{\tempxs}{\tempys}% |
| 425 |
\karnaughmakemap{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 426 |
\fi |
| 427 |
\else |
| 428 |
\kmsetoptstr{} % reads argument as <[opt]>str |
| 429 |
\node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (#3\kmunitlength,#4\kmunitlength){\kmstr};% |
| 430 |
\ifenableindices |
| 431 |
\node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}] |
| 432 |
at (#3\kmunitlength,#4\kmunitlength) {\kmcurrentindex}; |
| 433 |
\fi |
| 434 |
\fi}}% |
| 435 |
%% |
| 436 |
%% \karnaughmaketopbar typesets the variable bars of a Karnaugh map that are |
| 437 |
%% located on top of the diagram: |
| 438 |
%% |
| 439 |
\def\karnaughmaketopbar{% |
| 440 |
\kmbarstart=1 |
| 441 |
\kmpoweroftwo{\kmbarstart}{\kmxvarno} % \kmybarstart is the start |
| 442 |
% position for the \foreach |
| 443 |
\kmbarlength=\kmbarstart |
| 444 |
\multiply\kmbarlength by 2 % \kmbarlength is the length of a bar |
| 445 |
\kmbarmove=\kmbarstart |
| 446 |
\multiply\kmbarmove by 4 % This is the move distance for the \draw. |
| 447 |
\kmbarnum=\kmxsize |
| 448 |
\divide\kmbarnum by \kmbarmove % This is the number of repetitions for |
| 449 |
% the \foreach. |
| 450 |
% The highest-order variable bar needs a special treatment: |
| 451 |
\ifnum\kmbarnum=0\kmbarnum=1\divide\kmbarlength by 2\fi |
| 452 |
\advance\kmbarmove by \kmbarstart |
| 453 |
\kmtemplength=\pgfkeysvalueof{/tikz/kmbar top sep} |
| 454 |
\multiply \kmtemplength by \kmxvarno |
| 455 |
\advance \kmtemplength by \pgfkeysvalueof{/tikz/kmbar sep} |
| 456 |
\advance \kmtemplength by \kmysize\kmunitlength |
| 457 |
\kmsetoptstr % reads argument as <[opt]>str |
| 458 |
\ifnum\kmbarnum=1 |
| 459 |
\draw[kmbar,xshift=\kmbarstart\kmunitlength,yshift=\kmtemplength,kmtempsty] |
| 460 |
(0,0) -- node[above, kmbar label,kmtempsty]{\kmstr} (\kmbarlength\kmunitlength,0); |
| 461 |
\else |
| 462 |
\foreach \x in {\kmbarstart,\kmbarmove,...,\kmxsize} |
| 463 |
\draw[kmbar,xshift=\x\kmunitlength,yshift=\kmtemplength,kmtempsty] |
| 464 |
(0,0) -- node[above, kmbar label,kmtempsty]{\kmstr} (\kmbarlength\kmunitlength,0); |
| 465 |
\fi |
| 466 |
} |
| 467 |
%% |
| 468 |
%% \karnaughmakeleftbar typesets the variable bars of a Karnaugh map that are |
| 469 |
%% located on the left of the diagram: |
| 470 |
%% |
| 471 |
\def\karnaughmakeleftbar{% |
| 472 |
\kmbarstart=1 |
| 473 |
\kmpoweroftwo{\kmbarstart}{\kmyvarno} % \kmbarstart is the start |
| 474 |
% position for the \foreach |
| 475 |
\kmbarlength=\kmbarstart |
| 476 |
\multiply\kmbarlength by 2 % \kmbarlength is the length of a bar |
| 477 |
\kmbarmove=\kmbarstart |
| 478 |
\multiply\kmbarmove by 4 % This now is the move distance for the |
| 479 |
% \foreach. |
| 480 |
\kmbarnum=\kmysize |
| 481 |
\divide\kmbarnum by \kmbarmove % This now is the number of |
| 482 |
% repetitions for the \draw. |
| 483 |
%The highest-order variable bar needs a special treatment: |
| 484 |
\ifnum\kmbarnum=0\kmbarstart=0\kmbarnum=1\divide\kmbarlength by 2\fi |
| 485 |
\advance\kmbarmove by \kmbarstart |
| 486 |
\kmtemplength=-\pgfkeysvalueof{/tikz/kmbar left sep} |
| 487 |
\multiply \kmtemplength by \kmyvarno |
| 488 |
\advance \kmtemplength by -\pgfkeysvalueof{/tikz/kmbar sep} |
| 489 |
\kmsetoptstr % reads argument as <[opt]>str |
| 490 |
\ifnum\kmbarnum=1 |
| 491 |
\draw[kmbar,yshift=\kmbarstart\kmunitlength,xshift=\kmtemplength,kmtempsty] |
| 492 |
(0,0) -- node[left,kmbar label,kmtempsty]{\kmstr} (0,\kmbarlength\kmunitlength); |
| 493 |
\else |
| 494 |
\foreach \y in {\kmbarstart,\kmbarmove,...,\kmysize} |
| 495 |
\draw[kmbar,yshift=\y\kmunitlength,xshift=\kmtemplength,kmtempsty] |
| 496 |
(0,0) -- node[left,kmbar label,kmtempsty]{\kmstr} (0,\kmbarlength\kmunitlength); |
| 497 |
\fi |
| 498 |
} |
| 499 |
%% \karnaughmakebars calls \karnaughmaketopbar or \karnaughmakeleftbar |
| 500 |
%% depending on whether \kmvarno is odd or even. |
| 501 |
%% |
| 502 |
\def\karnaughmakebars{% |
| 503 |
\ifnum\kmvarno>0 |
| 504 |
\let\next=\karnaughmakebars |
| 505 |
\ifodd\kmvarno % We have to make a bar at the top |
| 506 |
\advance\kmxvarno by -1 |
| 507 |
\karnaughmaketopbar |
| 508 |
\else % We have to make a bar at the left |
| 509 |
\advance\kmyvarno by -1 |
| 510 |
\karnaughmakeleftbar |
| 511 |
\fi |
| 512 |
\advance\kmvarno by -1 |
| 513 |
\else |
| 514 |
\let\next=\relax |
| 515 |
\fi |
| 516 |
\next |
| 517 |
} |
| 518 |
%% |
| 519 |
%% \karnaughmakelabels is a modification of \karnaughmakebars. |
| 520 |
%% Instead of making the bars it sets two macros, \kmtoplabels |
| 521 |
%% and \kmleftlabels, with the labels for American style maps |
| 522 |
%% |
| 523 |
\def\karnaughmakelabels{% |
| 524 |
\ifnum\kmvarno>0 |
| 525 |
\let\next=\karnaughmakelabels |
| 526 |
\ifodd\kmvarno % We have to make the label for top |
| 527 |
\advance\kmxvarno by -1 |
| 528 |
\kmsetoptstr % reads argument as <[opt]>str |
| 529 |
\ifx\kmtoplabels\empty |
| 530 |
\xdef\kmtoplabels{\kmstr} |
| 531 |
\else |
| 532 |
\xdef\kmtoplabels{\kmtoplabels, \kmstr} |
| 533 |
\fi |
| 534 |
\else % We have to make the label for left |
| 535 |
\advance\kmyvarno by -1 |
| 536 |
\kmsetoptstr % reads argument as <[opt]>str |
| 537 |
\ifx\kmleftlabels\empty |
| 538 |
\xdef\kmleftlabels{\kmstr} |
| 539 |
\else |
| 540 |
\xdef\kmleftlabels{\kmleftlabels, \kmstr} |
| 541 |
\fi |
| 542 |
\fi |
| 543 |
\advance\kmvarno by -1 |
| 544 |
\else |
| 545 |
\let\next=\relax |
| 546 |
\fi |
| 547 |
\next |
| 548 |
} |
| 549 |
|
| 550 |
%% |
| 551 |
%% \karnaughmap is the macro that a user calls if he wants to draw a |
| 552 |
%% Karnaugh map: |
| 553 |
%% |
| 554 |
\def\karnaughmap#1#2#3#4#5{% |
| 555 |
%% |
| 556 |
%% #1 is the number of variables in the Karnaugh map |
| 557 |
%% #2 is the identifier of the function |
| 558 |
%% #3 is the list of identifiers of those variables |
| 559 |
%% #4 is the list of tokens that have to be written into the map |
| 560 |
%% #5 a possibly empty set of TikZ commands that will be drown before |
| 561 |
%% the function values so the values will appear on top of them |
| 562 |
%% |
| 563 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 564 |
\kmyvarno=#1 % \kmyvarno is the number of variable bars at the left |
| 565 |
\divide\kmyvarno by 2 |
| 566 |
\kmxvarno=#1 % \kmxvarno is the number of variable bars on top |
| 567 |
\advance\kmxvarno by -\kmyvarno |
| 568 |
\kmxsize=1 |
| 569 |
\kmpoweroftwo{\kmxsize}{\kmxvarno} |
| 570 |
\kmysize=1 |
| 571 |
\kmpoweroftwo{\kmysize}{\kmyvarno} |
| 572 |
\kmtemppos=\kmxsize |
| 573 |
\advance\kmtemppos by -1 |
| 574 |
\kmtemplength=\pgfkeysvalueof{/tikz/kmbar top sep} |
| 575 |
\foreach \x in {1,...,\kmtemppos} |
| 576 |
\draw[kmlines] (\x\kmunitlength,0) -- (\x\kmunitlength,\kmysize\kmunitlength); |
| 577 |
\kmtemppos=\kmysize |
| 578 |
\advance\kmtemppos by -1 |
| 579 |
\ifnum\kmtemppos>0 |
| 580 |
\foreach \y in {1,...,\kmtemppos} |
| 581 |
\draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength); |
| 582 |
\fi |
| 583 |
\draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength); |
| 584 |
#5 |
| 585 |
\ifAmericanstyle |
| 586 |
\kmtemppos=\kmxsize |
| 587 |
\advance\kmtemppos by -1 |
| 588 |
\pgfmathsetbasenumberlength{\kmxvarno} |
| 589 |
\foreach \x in {0,...,\kmtemppos} |
| 590 |
\node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber}; |
| 591 |
\kmtemppos=\kmysize |
| 592 |
\advance\kmtemppos by -1 |
| 593 |
\ifnum\kmtemppos>0 |
| 594 |
\pgfmathsetbasenumberlength{\kmyvarno} |
| 595 |
\foreach \y in {0,...,\kmtemppos} |
| 596 |
\node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber}; |
| 597 |
\fi |
| 598 |
\kmargumentstring{#3} |
| 599 |
\gdef\kmtoplabels{} |
| 600 |
\gdef\kmleftlabels{} |
| 601 |
\karnaughmakelabels |
| 602 |
\kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length} |
| 603 |
\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}; |
| 604 |
\else |
| 605 |
\ifdisablebars\relax\else |
| 606 |
\node[kmvar,above left] at (0,\kmysize\kmunitlength){#2}; |
| 607 |
\kmargumentstring{#3} |
| 608 |
\karnaughmakebars |
| 609 |
\fi |
| 610 |
\fi |
| 611 |
\ifenableindices |
| 612 |
\ifindexbin |
| 613 |
\gdef\kmcurrentindex{\kmcurrentindexbin} |
| 614 |
\else |
| 615 |
\ifindexGray |
| 616 |
\gdef\kmcurrentindex{\kmcurrentindexGray} |
| 617 |
\else |
| 618 |
\gdef\kmcurrentindex{\kmcurrentindexdec} |
| 619 |
\fi |
| 620 |
\fi |
| 621 |
\fi |
| 622 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 623 |
\kmindexcounter=0 |
| 624 |
\kmargumentstring{#4} |
| 625 |
\karnaughmakemap{\the\kmysize}{0}{0}{0} |
| 626 |
\ifodd\kmvarno |
| 627 |
\divide\kmxsize by 2 |
| 628 |
\karnaughmakemap{\the\kmysize}{1}{\the\kmxsize}{0} |
| 629 |
\fi |
| 630 |
}% |
| 631 |
%% Vertical Mode |
| 632 |
%% The next three macros are similar to the ones for normal mode. |
| 633 |
%% The macro \karnaughmakemapvert calls itself recursively until the parameter #1 |
| 634 |
%% equals 1, whereupon it draws one cell contents in a node and, if enabled, |
| 635 |
%% another node with the index. |
| 636 |
%% |
| 637 |
\def\karnaughmakemapvert#1#2#3#4{{% |
| 638 |
\kmrecursiondepth #1\relax |
| 639 |
\ifnum\kmrecursiondepth>1 |
| 640 |
\divide\kmrecursiondepth by 2 |
| 641 |
\edef\tempx{#3} |
| 642 |
\edef\tempy{#4} |
| 643 |
\kmtemppos=\tempx\relax |
| 644 |
\advance\kmtemppos by \kmrecursiondepth |
| 645 |
\edef\tempxs{\the\kmtemppos} |
| 646 |
\kmtemppos=\tempy\relax |
| 647 |
\advance\kmtemppos by \kmrecursiondepth |
| 648 |
\edef\tempys{\the\kmtemppos} |
| 649 |
|
| 650 |
\ifcase#2 |
| 651 |
%% |
| 652 |
%% The parameter #2 of \karnaughmakemapvert is needed because the inner Karnaugh |
| 653 |
%% maps need to be mirrored. This is achieved by the following case-statement, |
| 654 |
%% which orders the inner Karnaugh maps properly: |
| 655 |
%% +---+---+ |
| 656 |
%% | 0 | 2 | |
| 657 |
%% +---+---+ |
| 658 |
%% | 1 | 3 | |
| 659 |
%% +---+---+ |
| 660 |
%% |
| 661 |
%% Case 0: top-left Karnaugh map |
| 662 |
\karnaughmakemapvert{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 663 |
\karnaughmakemapvert{\kmrecursiondepth}{1}{\tempx}{\tempy}% |
| 664 |
\karnaughmakemapvert{\kmrecursiondepth}{2}{\tempxs}{\tempys}% |
| 665 |
\karnaughmakemapvert{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 666 |
\or |
| 667 |
%% Case 1: bottom-left Karnaugh map |
| 668 |
\karnaughmakemapvert{\kmrecursiondepth}{1}{\tempx}{\tempy}% |
| 669 |
\karnaughmakemapvert{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 670 |
\karnaughmakemapvert{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 671 |
\karnaughmakemapvert{\kmrecursiondepth}{2}{\tempxs}{\tempys}% |
| 672 |
\or |
| 673 |
%% Case 2: top-right Karnaugh map |
| 674 |
\karnaughmakemapvert{\kmrecursiondepth}{2}{\tempxs}{\tempys}% |
| 675 |
\karnaughmakemapvert{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 676 |
\karnaughmakemapvert{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 677 |
\karnaughmakemapvert{\kmrecursiondepth}{1}{\tempx}{\tempy}% |
| 678 |
\or |
| 679 |
%% Case 3: bottom-right Karnaugh map |
| 680 |
\karnaughmakemapvert{\kmrecursiondepth}{3}{\tempxs}{\tempy}% |
| 681 |
\karnaughmakemapvert{\kmrecursiondepth}{2}{\tempxs}{\tempys}% |
| 682 |
\karnaughmakemapvert{\kmrecursiondepth}{1}{\tempx}{\tempy}% |
| 683 |
\karnaughmakemapvert{\kmrecursiondepth}{0}{\tempx}{\tempys}% |
| 684 |
\fi |
| 685 |
\else |
| 686 |
\kmsetoptstr{} % reads argument as <[opt]>str |
| 687 |
\node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (#3\kmunitlength,#4\kmunitlength){\kmstr}; |
| 688 |
\ifenableindices |
| 689 |
\node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}] |
| 690 |
at (#3\kmunitlength,#4\kmunitlength) {\kmcurrentindex}; |
| 691 |
\fi |
| 692 |
\fi}}% |
| 693 |
%% |
| 694 |
%% \karnaughmakebarsvert calls \karnaughmaketopbar or \karnaughmakeleftbar |
| 695 |
%% depending on whether \kmvarno is odd or even. |
| 696 |
%% |
| 697 |
\def\karnaughmakebarsvert{% |
| 698 |
\ifnum\kmvarno>0 |
| 699 |
\let\next=\karnaughmakebarsvert |
| 700 |
\ifodd\kmvarno % We have to make a bar at the left |
| 701 |
\advance\kmyvarno by -1 |
| 702 |
\karnaughmakeleftbar |
| 703 |
\else % We have to make a bar at the top |
| 704 |
\advance\kmxvarno by -1 |
| 705 |
\karnaughmaketopbar |
| 706 |
\fi |
| 707 |
\advance\kmvarno by -1 |
| 708 |
\else |
| 709 |
\let\next=\relax |
| 710 |
\fi |
| 711 |
\next |
| 712 |
} |
| 713 |
|
| 714 |
%% \karnaughmakelabelsvert is a modification of \karnaughmakebarsvert. |
| 715 |
%% Instead of making the bars it sets two macros, \kmtoplabels |
| 716 |
%% and \kmleftlabels, with the labels for American style maps |
| 717 |
%% |
| 718 |
\def\karnaughmakelabelsvert{% |
| 719 |
\ifnum\kmvarno>0 |
| 720 |
\let\next=\karnaughmakelabelsvert |
| 721 |
\ifodd\kmvarno % We have to make the label for left |
| 722 |
\advance\kmyvarno by -1 |
| 723 |
\kmsetoptstr % reads argument as <[opt]>str |
| 724 |
\ifx\kmleftlabels\empty |
| 725 |
\xdef\kmleftlabels{\kmstr} |
| 726 |
\else |
| 727 |
\xdef\kmleftlabels{\kmleftlabels, \kmstr} |
| 728 |
\fi |
| 729 |
\else % We have to make the label for top |
| 730 |
\advance\kmxvarno by -1 |
| 731 |
\kmsetoptstr % reads argument as <[opt]>str |
| 732 |
\ifx\kmtoplabels\empty |
| 733 |
\xdef\kmtoplabels{\kmstr} |
| 734 |
\else |
| 735 |
\xdef\kmtoplabels{\kmtoplabels, \kmstr} |
| 736 |
\fi |
| 737 |
\fi |
| 738 |
\advance\kmvarno by -1 |
| 739 |
\else |
| 740 |
\let\next=\relax |
| 741 |
\fi |
| 742 |
\next |
| 743 |
} |
| 744 |
%% |
| 745 |
%% \karnaughmapvert is the macro that a user calls if he wants to draw a |
| 746 |
%% Karnaugh map in vertical mode (not be confused with TeX vertical mode): |
| 747 |
%% |
| 748 |
\def\karnaughmapvert#1#2#3#4#5{% |
| 749 |
%% |
| 750 |
%% #1 is the number of variables in the Karnaugh map |
| 751 |
%% #2 is the identifier of the function |
| 752 |
%% #3 is the list of identifiers of those variables |
| 753 |
%% #4 is the list of tokens that have to be written into the map |
| 754 |
%% #5 a possibly empty set of TikZ commands that will be drown before |
| 755 |
%% the function values so the values will appear on top of them |
| 756 |
%% |
| 757 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 758 |
\kmxvarno=#1 % \kmxvarno is the number of variable bars on top |
| 759 |
\divide\kmxvarno by 2 |
| 760 |
\kmyvarno=#1 % \kmyvarno is the number of variable bars at the left |
| 761 |
\advance\kmyvarno by -\kmxvarno |
| 762 |
\kmxsize=1 |
| 763 |
\kmpoweroftwo{\kmxsize}{\kmxvarno} |
| 764 |
\kmysize=1 |
| 765 |
\kmpoweroftwo{\kmysize}{\kmyvarno} |
| 766 |
\kmtemppos=\kmxsize |
| 767 |
\advance\kmtemppos by -1 |
| 768 |
\ifnum\kmtemppos>0 |
| 769 |
\foreach \x in {1,...,\kmtemppos} |
| 770 |
\draw[kmlines] (\x\kmunitlength,0) -- (\x\kmunitlength,\kmysize\kmunitlength); |
| 771 |
\fi |
| 772 |
\kmtemppos=\kmysize |
| 773 |
\advance\kmtemppos by -1 |
| 774 |
\foreach \y in {1,...,\kmtemppos} |
| 775 |
\draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength); |
| 776 |
\draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength); |
| 777 |
#5 |
| 778 |
\ifAmericanstyle |
| 779 |
\kmtemppos=\kmxsize |
| 780 |
\advance\kmtemppos by -1 |
| 781 |
\ifnum\kmtemppos>0 |
| 782 |
\pgfmathsetbasenumberlength{\kmxvarno} |
| 783 |
\foreach \x in {0,...,\kmtemppos} |
| 784 |
\node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber}; |
| 785 |
\fi |
| 786 |
\kmtemppos=\kmysize |
| 787 |
\advance\kmtemppos by -1 |
| 788 |
\pgfmathsetbasenumberlength{\kmyvarno} |
| 789 |
\foreach \y in {0,...,\kmtemppos} |
| 790 |
\node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber}; |
| 791 |
\kmargumentstring{#3} |
| 792 |
\gdef\kmtoplabels{} |
| 793 |
\gdef\kmleftlabels{} |
| 794 |
\karnaughmakelabelsvert |
| 795 |
\kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length} |
| 796 |
\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}; |
| 797 |
\else |
| 798 |
\ifdisablebars\relax\else |
| 799 |
\node[kmvar,above left] at (0,\kmysize\kmunitlength){#2}; |
| 800 |
\kmargumentstring{#3} |
| 801 |
\karnaughmakebarsvert |
| 802 |
\fi |
| 803 |
\fi |
| 804 |
\ifenableindices |
| 805 |
\ifindexbin |
| 806 |
\gdef\kmcurrentindex{\kmcurrentindexbin} |
| 807 |
\else |
| 808 |
\ifindexGray |
| 809 |
\gdef\kmcurrentindex{\kmcurrentindexGrayvert} |
| 810 |
\else |
| 811 |
\gdef\kmcurrentindex{\kmcurrentindexdec} |
| 812 |
\fi |
| 813 |
\fi |
| 814 |
\fi |
| 815 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 816 |
\kmindexcounter=0 |
| 817 |
\kmargumentstring{#4} |
| 818 |
\ifodd\kmvarno |
| 819 |
\divide\kmysize by 2 |
| 820 |
\karnaughmakemapvert{\the\kmxsize}{0}{0}{\the\kmysize} |
| 821 |
\karnaughmakemapvert{\the\kmxsize}{1}{0}{0} |
| 822 |
\else |
| 823 |
\karnaughmakemapvert{\the\kmxsize}{0}{0}{0} |
| 824 |
\fi |
| 825 |
}% |
| 826 |
%% |
| 827 |
%% Table Mode |
| 828 |
%% Non-recursive mode. Input is given is Karnaugh map order and not in truth table |
| 829 |
%% order. It should allow for bigger maps because the algorithm employed is not the |
| 830 |
%% time and memory consuming recursive algorithm of the old macros. |
| 831 |
%% |
| 832 |
%% \karnaughmakebarstab calls \karnaughmaketopbar or \karnaughmakeleftbar |
| 833 |
%% depending on whether \kmvarno is in the first or second half. |
| 834 |
%% |
| 835 |
\def\karnaughmakebarstab{% |
| 836 |
\ifnum\kmvarno>0 |
| 837 |
\let\next=\karnaughmakebarstab |
| 838 |
\ifnum\kmyvarno>0 % We have to make a bar at the left |
| 839 |
\advance\kmyvarno by -1 |
| 840 |
\karnaughmakeleftbar |
| 841 |
\else % We have to make a bar at the top |
| 842 |
\advance\kmxvarno by -1 |
| 843 |
\karnaughmaketopbar |
| 844 |
\fi |
| 845 |
\advance\kmvarno by -1 |
| 846 |
\else |
| 847 |
\let\next=\relax |
| 848 |
\fi |
| 849 |
\next |
| 850 |
} |
| 851 |
%% |
| 852 |
%% \karnaughmakelabelstab is a modification of \karnaughmakebarstab. |
| 853 |
%% Instead of making the bars it sets two macros, \kmtoplabels |
| 854 |
%% and \kmleftlabels, with the labels for American style maps |
| 855 |
%% |
| 856 |
\def\karnaughmakelabelstab{% |
| 857 |
\ifnum\kmvarno>0 |
| 858 |
\let\next=\karnaughmakelabelstab |
| 859 |
\ifnum\kmyvarno>0 % We have to make a bar at the left |
| 860 |
\advance\kmyvarno by -1 |
| 861 |
\kmsetoptstr % reads argument as <[opt]>str |
| 862 |
\ifx\kmleftlabels\empty |
| 863 |
\xdef\kmleftlabels{\kmstr} |
| 864 |
\else |
| 865 |
\xdef\kmleftlabels{\kmleftlabels, \kmstr} |
| 866 |
\fi |
| 867 |
\else % We have to make the label for top |
| 868 |
\advance\kmxvarno by -1 |
| 869 |
\kmsetoptstr % reads argument as <[opt]>str |
| 870 |
\ifx\kmtoplabels\empty |
| 871 |
\xdef\kmtoplabels{\kmstr} |
| 872 |
\else |
| 873 |
\xdef\kmtoplabels{\kmtoplabels, \kmstr} |
| 874 |
\fi |
| 875 |
\fi |
| 876 |
\advance\kmvarno by -1 |
| 877 |
\else |
| 878 |
\let\next=\relax |
| 879 |
\fi |
| 880 |
\next |
| 881 |
} |
| 882 |
%% \karnaughmap is the macro that a user calls if he wants to draw a |
| 883 |
%% Karnaugh map: |
| 884 |
%% |
| 885 |
\def\karnaughmaptab#1#2#3#4#5{% |
| 886 |
%% |
| 887 |
%% #1 is the number of variables in the Karnaugh map |
| 888 |
%% #2 is the identifier of the function |
| 889 |
%% #3 is the list of identifiers of those variables |
| 890 |
%% #4 is the list of tokens that have to be written into the map |
| 891 |
%% #5 a possibly empty set of TikZ commands that will be drown before |
| 892 |
%% the function values so the values will appear on top of them |
| 893 |
%% |
| 894 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 895 |
\kmyvarno=#1 % \kmyvarno is the number of variable bars at the left |
| 896 |
\divide\kmyvarno by 2 |
| 897 |
\kmxvarno=#1 % \kmxvarno is the number of variable bars on top |
| 898 |
\advance\kmxvarno by -\kmyvarno |
| 899 |
\kmxsize=1 |
| 900 |
\kmpoweroftwo{\kmxsize}{\kmxvarno} |
| 901 |
\kmysize=1 |
| 902 |
\kmpoweroftwo{\kmysize}{\kmyvarno} |
| 903 |
\kmtemppos=\kmxsize |
| 904 |
\advance\kmtemppos by -1 |
| 905 |
\kmtemplength=\pgfkeysvalueof{/tikz/kmbar top sep} |
| 906 |
\foreach \x in {1,...,\kmtemppos} |
| 907 |
\draw[kmlines] (\x\kmunitlength,0) -- (\x\kmunitlength,\kmysize\kmunitlength); |
| 908 |
\kmtemppos=\kmysize |
| 909 |
\advance\kmtemppos by -1 |
| 910 |
\ifnum\kmtemppos>0 |
| 911 |
\foreach \y in {1,...,\kmtemppos} |
| 912 |
\draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength); |
| 913 |
\fi |
| 914 |
\draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength); |
| 915 |
#5 |
| 916 |
\ifAmericanstyle |
| 917 |
\kmtemppos=\kmxsize |
| 918 |
\advance\kmtemppos by -1 |
| 919 |
\pgfmathsetbasenumberlength{\kmxvarno} |
| 920 |
\foreach \x in {0,...,\kmtemppos} |
| 921 |
\node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber}; |
| 922 |
\kmtemppos=\kmysize |
| 923 |
\advance\kmtemppos by -1 |
| 924 |
\ifnum\kmtemppos>0 |
| 925 |
\pgfmathsetbasenumberlength{\kmyvarno} |
| 926 |
\foreach \y in {0,...,\kmtemppos} |
| 927 |
\node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber}; |
| 928 |
\fi |
| 929 |
\kmargumentstring{#3} |
| 930 |
\gdef\kmtoplabels{} |
| 931 |
\gdef\kmleftlabels{} |
| 932 |
\karnaughmakelabelstab |
| 933 |
\kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length} |
| 934 |
\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}; |
| 935 |
\else |
| 936 |
\ifdisablebars\relax\else |
| 937 |
\node[kmvar,above left] at (0,\kmysize\kmunitlength){#2}; |
| 938 |
\kmargumentstring{#3} |
| 939 |
\karnaughmakebarstab |
| 940 |
\fi |
| 941 |
\fi |
| 942 |
\ifenableindices |
| 943 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 944 |
\ifindexbin |
| 945 |
\gdef\kmcurrentindex{\kmcurrentindexbin} |
| 946 |
\else |
| 947 |
\ifindexGray |
| 948 |
\gdef\kmcurrentindex{\kmcurrentindexGraytab} |
| 949 |
\kmyvarno=#1 % \kmyvarno is the number of variable bars at the left |
| 950 |
\divide\kmyvarno by 2 |
| 951 |
\kmxvarno=#1 % \kmxvarno is the number of variable bars on top |
| 952 |
\advance\kmxvarno by -\kmyvarno |
| 953 |
\else |
| 954 |
\gdef\kmcurrentindex{\kmcurrentindexdec} |
| 955 |
\fi |
| 956 |
\fi |
| 957 |
\fi |
| 958 |
\kmindexcounter=0 |
| 959 |
\kmargumentstring{#4} |
| 960 |
\advance\kmxsize by -1 |
| 961 |
\advance\kmysize by -1 |
| 962 |
\foreach \y in {\kmysize,...,0} { |
| 963 |
\foreach \x in {0,...,\kmxsize} { |
| 964 |
\kmsetoptstr{} |
| 965 |
\node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (\x\kmunitlength,\y\kmunitlength){\kmstr};% |
| 966 |
\ifenableindices |
| 967 |
\node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}] at (\x\kmunitlength,\y\kmunitlength) {\kmcurrentindex}; |
| 968 |
\fi |
| 969 |
} |
| 970 |
} |
| 971 |
}% |
| 972 |
%% |
| 973 |
%% Vertical Table Mode |
| 974 |
%% The next three macros are similar to the ones for normal mode. |
| 975 |
%% |
| 976 |
%% \karnaughmakebarsvert calls \karnaughmaketopbar or \karnaughmakeleftbar |
| 977 |
%% depending on whether \kmvarno is in the first or second half. |
| 978 |
%% |
| 979 |
\def\karnaughmakebarstabvert{% |
| 980 |
\ifnum\kmvarno>0 |
| 981 |
\let\next=\karnaughmakebarstabvert |
| 982 |
\ifnum\kmxvarno>0 % We have to make a bar at the top |
| 983 |
\advance\kmxvarno by -1 |
| 984 |
\karnaughmaketopbar |
| 985 |
\else % We have to make a bar at the left |
| 986 |
\advance\kmyvarno by -1 |
| 987 |
\karnaughmakeleftbar |
| 988 |
\fi |
| 989 |
\advance\kmvarno by -1 |
| 990 |
\else |
| 991 |
\let\next=\relax |
| 992 |
\fi |
| 993 |
\next |
| 994 |
} |
| 995 |
|
| 996 |
%% \karnaughmakelabelstabvert is a modification of \karnaughmakebarstabvert. |
| 997 |
%% Instead of making the bars it sets two macros, \kmtoplabels |
| 998 |
%% and \kmleftlabels, with the labels for American style maps |
| 999 |
%% |
| 1000 |
\def\karnaughmakelabelstabvert{% |
| 1001 |
\ifnum\kmvarno>0 |
| 1002 |
\let\next=\karnaughmakelabelstabvert |
| 1003 |
\ifnum\kmxvarno>0 % We have to make a bar at the top |
| 1004 |
\advance\kmxvarno by -1 |
| 1005 |
\kmsetoptstr % reads argument as <[opt]>str |
| 1006 |
\ifx\kmtoplabels\empty |
| 1007 |
\xdef\kmtoplabels{\kmstr} |
| 1008 |
\else |
| 1009 |
\xdef\kmtoplabels{\kmtoplabels, \kmstr} |
| 1010 |
\fi |
| 1011 |
\else % We have to make the label for left |
| 1012 |
\advance\kmyvarno by -1 |
| 1013 |
\kmsetoptstr % reads argument as <[opt]>str |
| 1014 |
\ifx\kmleftlabels\empty |
| 1015 |
\xdef\kmleftlabels{\kmstr} |
| 1016 |
\else |
| 1017 |
\xdef\kmleftlabels{\kmleftlabels, \kmstr} |
| 1018 |
\fi |
| 1019 |
\fi |
| 1020 |
\advance\kmvarno by -1 |
| 1021 |
\else |
| 1022 |
\let\next=\relax |
| 1023 |
\fi |
| 1024 |
\next |
| 1025 |
} |
| 1026 |
%% |
| 1027 |
%% \karnaughmaptabvert is the macro that a user calls if he wants to draw a |
| 1028 |
%% Karnaugh map in vertical mode (not be confused with TeX vertical mode): |
| 1029 |
%% |
| 1030 |
\def\karnaughmaptabvert#1#2#3#4#5{% |
| 1031 |
%% |
| 1032 |
%% #1 is the number of variables in the Karnaugh map |
| 1033 |
%% #2 is the identifier of the function |
| 1034 |
%% #3 is the list of identifiers of those variables |
| 1035 |
%% #4 is the list of tokens that have to be written into the map |
| 1036 |
%% #5 a possibly empty set of TikZ commands that will be drown before |
| 1037 |
%% the function values so the values will appear on top of them |
| 1038 |
%% |
| 1039 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 1040 |
\kmxvarno=#1 % \kmxvarno is the number of variable bars on top |
| 1041 |
\divide\kmxvarno by 2 |
| 1042 |
\kmyvarno=#1 % \kmyvarno is the number of variable bars at the left |
| 1043 |
\advance\kmyvarno by -\kmxvarno |
| 1044 |
\kmxsize=1 |
| 1045 |
\kmpoweroftwo{\kmxsize}{\kmxvarno} |
| 1046 |
\kmysize=1 |
| 1047 |
\kmpoweroftwo{\kmysize}{\kmyvarno} |
| 1048 |
\kmtemppos=\kmxsize |
| 1049 |
\advance\kmtemppos by -1 |
| 1050 |
\ifnum\kmtemppos>0 |
| 1051 |
\foreach \x in {1,...,\kmtemppos} |
| 1052 |
\draw[kmlines] (\x\kmunitlength,0) -- (\x\kmunitlength,\kmysize\kmunitlength); |
| 1053 |
\fi |
| 1054 |
\kmtemppos=\kmysize |
| 1055 |
\advance\kmtemppos by -1 |
| 1056 |
\foreach \y in {1,...,\kmtemppos} |
| 1057 |
\draw[kmlines] (0,\y\kmunitlength) -- (\kmxsize\kmunitlength,\y\kmunitlength); |
| 1058 |
\draw[kmbox] (0,0) rectangle (\kmxsize\kmunitlength,\kmysize\kmunitlength); |
| 1059 |
#5 |
| 1060 |
\ifAmericanstyle |
| 1061 |
\kmtemppos=\kmxsize |
| 1062 |
\advance\kmtemppos by -1 |
| 1063 |
\ifnum\kmtemppos>0 |
| 1064 |
\pgfmathsetbasenumberlength{\kmxvarno} |
| 1065 |
\foreach \x in {0,...,\kmtemppos} |
| 1066 |
\node[kmlabel top] at (0.5\kmunitlength+\x\kmunitlength,\kmysize\kmunitlength) {\pgfmathdectoGray\mynumber{\x}\mynumber}; |
| 1067 |
\fi |
| 1068 |
\kmtemppos=\kmysize |
| 1069 |
\advance\kmtemppos by -1 |
| 1070 |
\pgfmathsetbasenumberlength{\kmyvarno} |
| 1071 |
\foreach \y in {0,...,\kmtemppos} |
| 1072 |
\node[kmlabel left] at (0,1\kmysize\kmunitlength-0.5\kmunitlength-\y\kmunitlength) {\pgfmathdectoGray\mynumber{\y}\mynumber}; |
| 1073 |
\kmargumentstring{#3} |
| 1074 |
\gdef\kmtoplabels{} |
| 1075 |
\gdef\kmleftlabels{} |
| 1076 |
\karnaughmakelabelstabvert |
| 1077 |
\kmtemplength=\pgfkeysvalueof{/tikz/kmsep line length} |
| 1078 |
\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}; |
| 1079 |
\else |
| 1080 |
\ifdisablebars\relax\else |
| 1081 |
\node[kmvar,above left] at (0,\kmysize\kmunitlength){#2}; |
| 1082 |
\kmargumentstring{#3} |
| 1083 |
\karnaughmakebarstabvert |
| 1084 |
\fi |
| 1085 |
\fi |
| 1086 |
\ifenableindices |
| 1087 |
\kmvarno=#1 % \kmvarno is the total number of variables |
| 1088 |
\ifindexbin |
| 1089 |
\gdef\kmcurrentindex{\kmcurrentindexbin} |
| 1090 |
\else |
| 1091 |
\ifindexGray |
| 1092 |
\gdef\kmcurrentindex{\kmcurrentindexGraytabvert} |
| 1093 |
\kmyvarno=#1 % \kmyvarno is the number of variable bars at the left |
| 1094 |
\divide\kmyvarno by 2 |
| 1095 |
\kmxvarno=#1 % \kmxvarno is the number of variable bars on top |
| 1096 |
\advance\kmxvarno by -\kmyvarno |
| 1097 |
\else |
| 1098 |
\gdef\kmcurrentindex{\kmcurrentindexdec} |
| 1099 |
\fi |
| 1100 |
\fi |
| 1101 |
\fi |
| 1102 |
\kmindexcounter=0 |
| 1103 |
\kmargumentstring{#4} |
| 1104 |
\advance\kmxsize by -1 |
| 1105 |
\advance\kmysize by -1 |
| 1106 |
\foreach \x in {0,...,\kmxsize} { |
| 1107 |
\foreach \y in {\kmysize,...,0} { |
| 1108 |
\kmsetoptstr{} |
| 1109 |
\node[kmcell,kmtempsty,shift={(0.5\kmunitlength,0.5\kmunitlength)}] at (\x\kmunitlength,\y\kmunitlength){\kmstr};% |
| 1110 |
\ifenableindices |
| 1111 |
\node[kmindex,shift={(\pgfkeysvalueof{/tikz/kmindex posx},\pgfkeysvalueof{/tikz/kmindex posy})}] at (\x\kmunitlength,\y\kmunitlength) {\kmcurrentindex}; |
| 1112 |
\fi |
| 1113 |
} |
| 1114 |
} |
| 1115 |
}% |
| 1116 |
|
| 1117 |
\endinput |