summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/karnaugh-map/karnaugh-map.sty
blob: 72e2333b568b5d7adc9155e0c4b4ec89439a5089 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
%%
%% This is file `karnaugh-map.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% karnaugh-map.dtx  (with options: `package')
%% 
%% This generated file originates from the karnaugh-map package!
%% 

%%
%% Copyright (C) 2015-2022 Mattias Jacobsson and contributors
%% This work, karnaugh-map, is written from the ground up by Mattias Jacobsson. However the general implementation idea is based on the work published on [TeX - LaTeX Stack Exchange](https://tex.stackexchange.com) by [Ignasi](https://tex.stackexchange.com/users/1952/ignasi) found [here](https://tex.stackexchange.com/a/140581) and [here](https://tex.stackexchange.com/a/36879) which is licensed under [CC BY-SA](https://creativecommons.org/licenses/by-sa/3.0/). karnaugh-map is therefore licensed under [CC BY-SA](https://creativecommons.org/licenses/by-sa/3.0/). Contributors include [Oscar Gustafsson](https://github.com/oscargus).
%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{karnaugh-map}[2022/01/15 v2.0 Draw Karnaugh Maps]

%%
%% Dependencies
%%

%% parsing arguments and options
\RequirePackage{kvoptions}
\RequirePackage{keyval}
\RequirePackage{xparse}

%% working with strings
\RequirePackage{xstring}

%% drawing
\RequirePackage{tikz}
\usetikzlibrary{calc,matrix}

%%
%% Helpers
%%

%% update color
\newcommand{\@karnaughmap@func@updatecolor@}{%
  % jump to next color
  \advance\@karnaughmap@var@colorindex@ by 1\relax
  \advance\@karnaughmap@var@colorindexnext@ by 1\relax
  % store color
  \StrBetween[\@karnaughmap@var@colorindex@,\@karnaughmap@var@colorindexnext@]{\@karnaughmap@var@colors@}{,}{,}[\@karnaughmap@var@color@]
  % if color is empty (eg. when there are more implicants than colors) return cyan
  \IfStrEq{\@karnaughmap@var@color@}{}{%
    \renewcommand{\@karnaughmap@var@color@}{cyan}
  }{}
}

%% convert decimal to binary 6-bit
\newcommand{\@karnaughmap@func@decimaltobin@}[1]{%
  \ifnum#1=0 000000\fi
  \ifnum#1=1 000001\fi
  \ifnum#1=2 000010\fi
  \ifnum#1=3 000011\fi
  \ifnum#1=4 000100\fi
  \ifnum#1=5 000101\fi
  \ifnum#1=6 000110\fi
  \ifnum#1=7 000111\fi
  \ifnum#1=8 001000\fi
  \ifnum#1=9 001001\fi
  \ifnum#1=10 001010\fi
  \ifnum#1=11 001011\fi
  \ifnum#1=12 001100\fi
  \ifnum#1=13 001101\fi
  \ifnum#1=14 001110\fi
  \ifnum#1=15 001111\fi
  \ifnum#1=16 010000\fi
  \ifnum#1=17 010001\fi
  \ifnum#1=18 010010\fi
  \ifnum#1=19 010011\fi
  \ifnum#1=20 010100\fi
  \ifnum#1=21 010101\fi
  \ifnum#1=22 010110\fi
  \ifnum#1=23 010111\fi
  \ifnum#1=24 011000\fi
  \ifnum#1=25 011001\fi
  \ifnum#1=26 011010\fi
  \ifnum#1=27 011011\fi
  \ifnum#1=28 011100\fi
  \ifnum#1=29 011101\fi
  \ifnum#1=30 011110\fi
  \ifnum#1=31 011111\fi
  \ifnum#1=32 100000\fi
  \ifnum#1=33 100001\fi
  \ifnum#1=34 100010\fi
  \ifnum#1=35 100011\fi
  \ifnum#1=36 100100\fi
  \ifnum#1=37 100101\fi
  \ifnum#1=38 100110\fi
  \ifnum#1=39 100111\fi
  \ifnum#1=40 101000\fi
  \ifnum#1=41 101001\fi
  \ifnum#1=42 101010\fi
  \ifnum#1=43 101011\fi
  \ifnum#1=44 101100\fi
  \ifnum#1=45 101101\fi
  \ifnum#1=46 101110\fi
  \ifnum#1=47 101111\fi
  \ifnum#1=48 110000\fi
  \ifnum#1=49 110001\fi
  \ifnum#1=50 110010\fi
  \ifnum#1=51 110011\fi
  \ifnum#1=52 110100\fi
  \ifnum#1=53 110101\fi
  \ifnum#1=54 110110\fi
  \ifnum#1=55 110111\fi
  \ifnum#1=56 111000\fi
  \ifnum#1=57 111001\fi
  \ifnum#1=58 111010\fi
  \ifnum#1=59 111011\fi
  \ifnum#1=60 111100\fi
  \ifnum#1=61 111101\fi
  \ifnum#1=62 111110\fi
  \ifnum#1=63 111111\fi
}

%% command raises an error if executed outside the karnaugh-map environment
\newcommand{\@karnaughmap@func@bailoutsideenvironment@}[0]{%
  \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=000
    \PackageError{karnaugh-map}{%
      Command can not be used outside karnaugh-map environment%
    }{%
      Do not use this command outside the karnaugh-map environment.%
    }
  \fi
}

%% store map size
\newcommand{\@karnaughmap@var@mapsizex@}{0}
\newcommand{\@karnaughmap@var@mapsizey@}{0}
\newcommand{\@karnaughmap@var@mapsizez@}{0}

%% store map labels
\newcommand{\@karnaughmap@var@labelA@}{$X_0$}
\newcommand{\@karnaughmap@var@labelB@}{$X_1$}
\newcommand{\@karnaughmap@var@labelC@}{$X_2$}
\newcommand{\@karnaughmap@var@labelD@}{$X_3$}
\newcommand{\@karnaughmap@var@labelE@}{$X_4$}
\newcommand{\@karnaughmap@var@labelF@}{$X_5$}
\newcommand{\@karnaughmap@var@labelX@}{$X_1$$X_0$}
\newcommand{\@karnaughmap@var@labelY@}{$X_3$$X_2$}
\newcommand{\@karnaughmap@var@labelZ@}{$X_5$$X_4$}
%% if map labels are defined using API version unknown(0), v1(1) or v2(2)
\newcommand{\@karnaughmap@var@labeltype@}{0}

%% store colors
\newcommand{\@karnaughmap@var@colors@}{,red,green,yellow,cyan,blue,magenta,}
\newcommand{\@karnaughmap@var@color@}{}

%% declare package and environment options
%% package options
\SetupKeyvalOptions{
  family=@karnaughmap@option,
  prefix=@karnaughmap@option@,
}
\DeclareStringOption[middle]{label}[middle]
\DeclareStringOption[]{implicantcolors}[]
\ProcessKeyvalOptions*
%% environment options
\define@key{karnaugh-map}{label}[middle]{\renewcommand{\@karnaughmap@option@label}{#1}}
\define@key{karnaugh-map}{implicantcolors}[]{\renewcommand{\@karnaughmap@option@implicantcolors}{#1}}

%% render in black and white or color default to '0'(false/color)
\newcommand{\@karnaughmap@var@bw@}{0}

%%
%% Environment
%%

\NewDocumentEnvironment{karnaugh-map}{D(){} s O{4} O{4} O{1}} {%
  \begingroup
    % parse environment options
    \setkeys{karnaugh-map}{#1}%
    %
    % check if implicant colors are specified
    \IfStrEq{\@karnaughmap@option@implicantcolors}{}{}{%
      % ensure no empty colors are specified
      \IfSubStr{\@karnaughmap@option@implicantcolors}{,,}{%
        \PackageError{karnaugh-map}{%
          Incorrect usage of option implicantcolors%
        }{%
          The option implicantcolors is a comma separated list of colors with no gap. You used two consecutive commas.%
        }%
      }{}%
      % add comma at beginning and end. then remove duplicates
      \StrSubstitute{,\@karnaughmap@option@implicantcolors,}{,,}{,}[\@karnaughmap@var@colors@]%
    }%
    %
    % determinate if markings should be color or black and white
    \IfBooleanTF{#2}{%
      % should be black and white
      \renewcommand{\@karnaughmap@var@bw@}{1}%
    }{%
      % should be color
      \renewcommand{\@karnaughmap@var@bw@}{0}%
    }%
    %
    % store map size {[START]
      \renewcommand{\@karnaughmap@var@mapsizex@}{#3}%
      \renewcommand{\@karnaughmap@var@mapsizey@}{#4}%
      \renewcommand{\@karnaughmap@var@mapsizez@}{#5}%
    % [END]}
    %
    % only 9 arguments are directly supported. jump to helper function
    % to parse remaining arguments and finish initialization.
    \@karnaughmap@func@karnaughstart@%
}{
    \end{tikzpicture}
  \endgroup
}
\DeclareDocumentCommand{\@karnaughmap@func@karnaughstart@}{oooooo} {%
  % parse label args {[START]
    % try to maintain backwards compatibility with v1 while enabling users to separatly
    % specify each input label variable
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=221
      % no difference
      \IfValueT{#1}{\renewcommand{\@karnaughmap@var@labelA@}{#1}}%
      \IfValueT{#2}{\renewcommand{\@karnaughmap@var@labelB@}{#2}}%
      \renewcommand{\@karnaughmap@var@labelX@}{\@karnaughmap@var@labelA@}%
      \renewcommand{\@karnaughmap@var@labelY@}{\@karnaughmap@var@labelB@}%
      \renewcommand{\@karnaughmap@var@labeltype@}{2}%
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=241
      \IfValueT{#1}{\IfValueT{#2}{\IfValueF{#3}{%
        % API v1
        \renewcommand{\@karnaughmap@var@labelX@}{#1}%
        \renewcommand{\@karnaughmap@var@labelY@}{#2}%
        \renewcommand{\@karnaughmap@var@labeltype@}{1}%
      }}}%
      \ifnum\@karnaughmap@var@labeltype@=0
        % API v2
        \IfValueT{#1}{\renewcommand{\@karnaughmap@var@labelA@}{#1}}%
        \IfValueT{#2}{\renewcommand{\@karnaughmap@var@labelB@}{#2}}%
        \IfValueT{#3}{\renewcommand{\@karnaughmap@var@labelC@}{#3}}%
        \renewcommand{\@karnaughmap@var@labelX@}{\@karnaughmap@var@labelA@}%
        \renewcommand{\@karnaughmap@var@labelY@}{\@karnaughmap@var@labelC@\@karnaughmap@var@labelB@}%
        \renewcommand{\@karnaughmap@var@labeltype@}{2}%
      \fi
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=421
      \IfValueT{#1}{\IfValueT{#2}{\IfValueF{#3}{%
        % API v1
        \renewcommand{\@karnaughmap@var@labelX@}{#1}%
        \renewcommand{\@karnaughmap@var@labelY@}{#2}%
        \renewcommand{\@karnaughmap@var@labeltype@}{1}%
      }}}%
      \ifnum\@karnaughmap@var@labeltype@=0
        % API v2
        \IfValueT{#1}{\renewcommand{\@karnaughmap@var@labelA@}{#1}}%
        \IfValueT{#2}{\renewcommand{\@karnaughmap@var@labelB@}{#2}}%
        \IfValueT{#3}{\renewcommand{\@karnaughmap@var@labelC@}{#3}}%
        \renewcommand{\@karnaughmap@var@labelX@}{\@karnaughmap@var@labelB@\@karnaughmap@var@labelA@}%
        \renewcommand{\@karnaughmap@var@labelY@}{\@karnaughmap@var@labelC@}%
        \renewcommand{\@karnaughmap@var@labeltype@}{2}%
      \fi
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=441
      \IfValueT{#1}{\IfValueT{#2}{\IfValueF{#3}{%
        % API v1
        \renewcommand{\@karnaughmap@var@labelX@}{#1}%
        \renewcommand{\@karnaughmap@var@labelY@}{#2}%
        \renewcommand{\@karnaughmap@var@labeltype@}{1}%
      }}}%
      \ifnum\@karnaughmap@var@labeltype@=0
        % API v2
        \IfValueT{#1}{\renewcommand{\@karnaughmap@var@labelA@}{#1}}%
        \IfValueT{#2}{\renewcommand{\@karnaughmap@var@labelB@}{#2}}%
        \IfValueT{#3}{\renewcommand{\@karnaughmap@var@labelC@}{#3}}%
        \IfValueT{#4}{\renewcommand{\@karnaughmap@var@labelD@}{#4}}%
        \renewcommand{\@karnaughmap@var@labelX@}{\@karnaughmap@var@labelB@\@karnaughmap@var@labelA@}%
        \renewcommand{\@karnaughmap@var@labelY@}{\@karnaughmap@var@labelD@\@karnaughmap@var@labelC@}%
        \renewcommand{\@karnaughmap@var@labeltype@}{2}%
      \fi
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=442
      \IfValueT{#1}{\IfValueT{#2}{\IfValueT{#3}{\IfValueF{#4}{%
        % API v1
        \renewcommand{\@karnaughmap@var@labelX@}{#1}%
        \renewcommand{\@karnaughmap@var@labelY@}{#2}%
        \renewcommand{\@karnaughmap@var@labelZ@}{#3}%
        \renewcommand{\@karnaughmap@var@labeltype@}{1}%
      }}}}%
      \ifnum\@karnaughmap@var@labeltype@=0
        % API v2
        \IfValueT{#1}{\renewcommand{\@karnaughmap@var@labelA@}{#1}}%
        \IfValueT{#2}{\renewcommand{\@karnaughmap@var@labelB@}{#2}}%
        \IfValueT{#3}{\renewcommand{\@karnaughmap@var@labelC@}{#3}}%
        \IfValueT{#4}{\renewcommand{\@karnaughmap@var@labelD@}{#4}}%
        \IfValueT{#5}{\renewcommand{\@karnaughmap@var@labelE@}{#5}}%
        \renewcommand{\@karnaughmap@var@labelX@}{\@karnaughmap@var@labelB@\@karnaughmap@var@labelA@}%
        \renewcommand{\@karnaughmap@var@labelY@}{\@karnaughmap@var@labelD@\@karnaughmap@var@labelC@}%
        \renewcommand{\@karnaughmap@var@labelZ@}{\@karnaughmap@var@labelE@}%
        \renewcommand{\@karnaughmap@var@labeltype@}{2}%
      \fi
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=444
      \IfValueT{#1}{\IfValueT{#2}{\IfValueT{#3}{\IfValueF{#4}{%
        % API v1
        \renewcommand{\@karnaughmap@var@labelX@}{#1}%
        \renewcommand{\@karnaughmap@var@labelY@}{#2}%
        \renewcommand{\@karnaughmap@var@labelZ@}{#3}%
        \renewcommand{\@karnaughmap@var@labeltype@}{1}%
      }}}}%
      \ifnum\@karnaughmap@var@labeltype@=0
        % API v2
        \IfValueT{#1}{\renewcommand{\@karnaughmap@var@labelA@}{#1}}%
        \IfValueT{#2}{\renewcommand{\@karnaughmap@var@labelB@}{#2}}%
        \IfValueT{#3}{\renewcommand{\@karnaughmap@var@labelC@}{#3}}%
        \IfValueT{#4}{\renewcommand{\@karnaughmap@var@labelD@}{#4}}%
        \IfValueT{#5}{\renewcommand{\@karnaughmap@var@labelE@}{#5}}%
        \IfValueT{#6}{\renewcommand{\@karnaughmap@var@labelF@}{#6}}%
        \renewcommand{\@karnaughmap@var@labelX@}{\@karnaughmap@var@labelB@\@karnaughmap@var@labelA@}%
        \renewcommand{\@karnaughmap@var@labelY@}{\@karnaughmap@var@labelD@\@karnaughmap@var@labelC@}%
        \renewcommand{\@karnaughmap@var@labelZ@}{\@karnaughmap@var@labelF@\@karnaughmap@var@labelE@}%
        \renewcommand{\@karnaughmap@var@labeltype@}{2}%
      \fi
    \fi
    % warn when using API v1
    \ifnum\@karnaughmap@var@labeltype@=1
      \PackageWarning{karnaugh-map}{%
        The API for specifying variable labels have been updated. You are currently using the old API(v1).
        In the new API each variable label is specified separately. See the documentation for more details.
        Backward compatibility may disappear in a future release.%
      }%
    \fi
  % [END]}
  %
  % find matching matrix template and alignment parameters {[START]
    \newcommand{\@karnaughmap@local@matrixtemplate@}{0}% '0' is considered as missing matrix template
    \newcommand{\@karnaughmap@local@maprealignmentx@}{0}%
    \newcommand{\@karnaughmap@local@maprealignmenty@}{0}%
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=221
      \renewcommand{\@karnaughmap@local@matrixtemplate@}{%
                     \&                       0 \&                       1 \& \phantom{0} \\
                   0 \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \&             \\
                   1 \& |(000010)|  \phantom{0} \& |(000011)|  \phantom{0} \&             \\
        \phantom{0}  \&                         \&                         \&             \\
      }%
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=241
      \renewcommand{\@karnaughmap@local@matrixtemplate@}{%
                     \&                       0 \&                       1 \& \phantom{00} \\
                  00 \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \&              \\
                  01 \& |(000010)|  \phantom{0} \& |(000011)|  \phantom{0} \&              \\
                  11 \& |(000110)|  \phantom{0} \& |(000111)|  \phantom{0} \&              \\
                  10 \& |(000100)|  \phantom{0} \& |(000101)|  \phantom{0} \&              \\
        \phantom{00} \&                         \&                         \&              \\
      }%
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=421
      \renewcommand{\@karnaughmap@local@matrixtemplate@}{%
                     \&                      00 \&                      01 \&                      11 \&                      10 \& \phantom{00} \\
                  0  \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \& |(000011)|  \phantom{0} \& |(000010)|  \phantom{0} \&              \\
                  1  \& |(000100)|  \phantom{0} \& |(000101)|  \phantom{0} \& |(000111)|  \phantom{0} \& |(000110)|  \phantom{0} \&              \\
        \phantom{00} \&                         \&                         \&                         \&                         \&              \\
      }%
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=441
      \renewcommand{\@karnaughmap@local@matrixtemplate@}{%
                     \&                      00 \&                      01 \&                      11 \&                      10 \& \phantom{00} \\
                  00 \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \& |(000011)|  \phantom{0} \& |(000010)|  \phantom{0} \&              \\
                  01 \& |(000100)|  \phantom{0} \& |(000101)|  \phantom{0} \& |(000111)|  \phantom{0} \& |(000110)|  \phantom{0} \&              \\
                  11 \& |(001100)|  \phantom{0} \& |(001101)|  \phantom{0} \& |(001111)|  \phantom{0} \& |(001110)|  \phantom{0} \&              \\
                  10 \& |(001000)|  \phantom{0} \& |(001001)|  \phantom{0} \& |(001011)|  \phantom{0} \& |(001010)|  \phantom{0} \&              \\
        \phantom{00} \&                         \&                         \&                         \&                         \&              \\
      }%
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=442
      \renewcommand{\@karnaughmap@local@matrixtemplate@}{%
                     \&                      00 \&                      01 \&                      11 \&                      10 \& \phantom{00} \&                      00 \&                      01 \&                      11 \&                      10 \& \phantom{00} \\
                  00 \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \& |(000011)|  \phantom{0} \& |(000010)|  \phantom{0} \&              \& |(010000)|  \phantom{0} \& |(010001)|  \phantom{0} \& |(010011)|  \phantom{0} \& |(010010)|  \phantom{0} \&              \\
                  01 \& |(000100)|  \phantom{0} \& |(000101)|  \phantom{0} \& |(000111)|  \phantom{0} \& |(000110)|  \phantom{0} \&              \& |(010100)|  \phantom{0} \& |(010101)|  \phantom{0} \& |(010111)|  \phantom{0} \& |(010110)|  \phantom{0} \&              \\
                  11 \& |(001100)|  \phantom{0} \& |(001101)|  \phantom{0} \& |(001111)|  \phantom{0} \& |(001110)|  \phantom{0} \&              \& |(011100)|  \phantom{0} \& |(011101)|  \phantom{0} \& |(011111)|  \phantom{0} \& |(011110)|  \phantom{0} \&              \\
                  10 \& |(001000)|  \phantom{0} \& |(001001)|  \phantom{0} \& |(001011)|  \phantom{0} \& |(001010)|  \phantom{0} \&              \& |(011000)|  \phantom{0} \& |(011001)|  \phantom{0} \& |(011011)|  \phantom{0} \& |(011010)|  \phantom{0} \&              \\
        \phantom{00} \&                         \&                         \&                         \&                         \&              \&                         \&                         \&                         \&                         \&              \\
      }%
      \renewcommand{\@karnaughmap@local@maprealignmentx@}{2.5}%
    \fi
    \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=444
      \renewcommand{\@karnaughmap@local@matrixtemplate@}{%
                     \&                      00 \&                      01 \&                      11 \&                      10 \& \phantom{00} \&                      00 \&                      01 \&                      11 \&                      10 \& \phantom{00} \\
                  00 \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \& |(000011)|  \phantom{0} \& |(000010)|  \phantom{0} \&              \& |(010000)|  \phantom{0} \& |(010001)|  \phantom{0} \& |(010011)|  \phantom{0} \& |(010010)|  \phantom{0} \&              \\
                  01 \& |(000100)|  \phantom{0} \& |(000101)|  \phantom{0} \& |(000111)|  \phantom{0} \& |(000110)|  \phantom{0} \&              \& |(010100)|  \phantom{0} \& |(010101)|  \phantom{0} \& |(010111)|  \phantom{0} \& |(010110)|  \phantom{0} \&              \\
                  11 \& |(001100)|  \phantom{0} \& |(001101)|  \phantom{0} \& |(001111)|  \phantom{0} \& |(001110)|  \phantom{0} \&              \& |(011100)|  \phantom{0} \& |(011101)|  \phantom{0} \& |(011111)|  \phantom{0} \& |(011110)|  \phantom{0} \&              \\
                  10 \& |(001000)|  \phantom{0} \& |(001001)|  \phantom{0} \& |(001011)|  \phantom{0} \& |(001010)|  \phantom{0} \&              \& |(011000)|  \phantom{0} \& |(011001)|  \phantom{0} \& |(011011)|  \phantom{0} \& |(011010)|  \phantom{0} \&              \\
        \phantom{00} \&                         \&                         \&                         \&                         \&              \&                         \&                         \&                         \&                         \&              \\
                  00 \& |(100000)|  \phantom{0} \& |(100001)|  \phantom{0} \& |(100011)|  \phantom{0} \& |(100010)|  \phantom{0} \&              \& |(110000)|  \phantom{0} \& |(110001)|  \phantom{0} \& |(110011)|  \phantom{0} \& |(110010)|  \phantom{0} \&              \\
                  01 \& |(100100)|  \phantom{0} \& |(100101)|  \phantom{0} \& |(100111)|  \phantom{0} \& |(100110)|  \phantom{0} \&              \& |(110100)|  \phantom{0} \& |(110101)|  \phantom{0} \& |(110111)|  \phantom{0} \& |(110110)|  \phantom{0} \&              \\
                  11 \& |(101100)|  \phantom{0} \& |(101101)|  \phantom{0} \& |(101111)|  \phantom{0} \& |(101110)|  \phantom{0} \&              \& |(111100)|  \phantom{0} \& |(111101)|  \phantom{0} \& |(111111)|  \phantom{0} \& |(111110)|  \phantom{0} \&              \\
                  10 \& |(101000)|  \phantom{0} \& |(101001)|  \phantom{0} \& |(101011)|  \phantom{0} \& |(101010)|  \phantom{0} \&              \& |(111000)|  \phantom{0} \& |(111001)|  \phantom{0} \& |(111011)|  \phantom{0} \& |(111010)|  \phantom{0} \&              \\
        \phantom{00} \&                         \&                         \&                         \&                         \&              \&                         \&                         \&                         \&                         \&              \\
      }%
      \renewcommand{\@karnaughmap@local@maprealignmentx@}{2.5}%
      \renewcommand{\@karnaughmap@local@maprealignmenty@}{-2.5}%
    \fi
  % [END]}
  % test if a matrix template is found or not(aka "\@karnaughmap@local@matrixtemplate@" equals to '0')
  \ifnum0=\@karnaughmap@local@matrixtemplate@
    % print error if no template could be found
    \PackageError{karnaugh-map}{%
      Can not find a template fitting your specification (\@karnaughmap@var@mapsizex@\space x \@karnaughmap@var@mapsizey@\space x \@karnaughmap@var@mapsizez@)%
    }{%
      Existing templates have the following dimensions: 2x2x1, 2x4x1, 4x2x1, 4x4x1, 4x4x2, and 4x4x4.
    }%
  \fi
  \begin{tikzpicture}
    % grid {[START]
      % for all dimensions
      \draw[color=black, ultra thin] (0,0) grid (\@karnaughmap@var@mapsizex@,\@karnaughmap@var@mapsizey@);
      % when there are 2 sub maps
      \ifnum\@karnaughmap@var@mapsizez@=2
        \draw[color=black, ultra thin] (5,0) grid (9,4);
      \fi
      % when there are 4 sub maps
      \ifnum\@karnaughmap@var@mapsizez@=4
        \draw[color=black, ultra thin] (5,0) grid (9,4);
        \draw[color=black, ultra thin] (0,-5) grid (4,-1);
        \draw[color=black, ultra thin] (5,-5) grid (9,-1);
      \fi
    % [END]}
    % labels {[START]
      \IfStrEq{\@karnaughmap@option@label}{corner}{%
        % if variables should be positioned at the corner
        %
        % for all dimensions
        \draw[color=black, ultra thin] (0,\@karnaughmap@var@mapsizey@) --
          node[pos=0.5, above right, anchor=south, rotate=-45] {\small{\@karnaughmap@var@labelX@}}
          node[pos=0.5, below left, anchor=north, rotate=-45] {\small{\@karnaughmap@var@labelY@}}
          ++(135:1);
        % when there are 2 sub maps
        \ifnum\@karnaughmap@var@mapsizez@=2
          % extra sub map labels
          \node[below] at (2,-0.1) {\small{\@karnaughmap@var@labelZ@$=0$}};
          \node[below] at (7,-0.1) {\small{\@karnaughmap@var@labelZ@$=1$}};
        \fi
        % when there are 4 sub maps
        \ifnum\@karnaughmap@var@mapsizez@=4
          % extra sub map labels
          \node[below] at (2,-0.1) {\small{\@karnaughmap@var@labelZ@$=00$}};
          \node[below] at (7,-0.1) {\small{\@karnaughmap@var@labelZ@$=01$}};
          \node[below] at (2,-5.1) {\small{\@karnaughmap@var@labelZ@$=10$}};
          \node[below] at (7,-5.1) {\small{\@karnaughmap@var@labelZ@$=11$}};
        \fi
      }{%
        % if variables should be positioned in the middle(top and side). Default.
        %
        % for all dimensions
        \node[above] at (\@karnaughmap@var@mapsizex@*0.5,\@karnaughmap@var@mapsizey@+0.9) {\small{\@karnaughmap@var@labelX@}};
        \node[left] at (-0.9,\@karnaughmap@var@mapsizey@*0.5) {\small{\@karnaughmap@var@labelY@}};
        % when there are 2 sub maps
        \ifnum\@karnaughmap@var@mapsizez@=2
          \node[above] at (7,4.9) {\small{\@karnaughmap@var@labelX@}};
          % extra sub map labels
          \node[below] at (2,-0.1) {\small{\@karnaughmap@var@labelZ@$=0$}};
          \node[below] at (7,-0.1) {\small{\@karnaughmap@var@labelZ@$=1$}};
        \fi
        % when there are 4 sub maps
        \ifnum\@karnaughmap@var@mapsizez@=4
          \node[above] at (7,4.9) {\small{\@karnaughmap@var@labelX@}};
          \node[left] at (-0.9,-3) {\small{\@karnaughmap@var@labelY@}};
          % extra sub map labels
          \node[below] at (2,-0.1) {\small{\@karnaughmap@var@labelZ@$=00$}};
          \node[below] at (7,-0.1) {\small{\@karnaughmap@var@labelZ@$=01$}};
          \node[below] at (2,-5.1) {\small{\@karnaughmap@var@labelZ@$=10$}};
          \node[below] at (7,-5.1) {\small{\@karnaughmap@var@labelZ@$=11$}};
        \fi
      }
    % [END]}
    % data
    \matrix[
      matrix of nodes,
      ampersand replacement=\&,
      column sep={1cm,between origins},
      row sep={1cm,between origins},
    ] at (\@karnaughmap@var@mapsizex@*0.5+\@karnaughmap@local@maprealignmentx@,\@karnaughmap@var@mapsizey@*0.5+\@karnaughmap@local@maprealignmenty@) {
      \@karnaughmap@local@matrixtemplate@%
    };
}

%%
%% Commands for filling out the cells
%%

%% store already used cells to avoid double filled cells and for auto completion
\newcommand{\@karnaughmap@var@usedcells@}{,}

\DeclareDocumentCommand{\autoterms}{O{-}} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  \begingroup
    % calculate max cell number {[START]
      \newcount\@karnaughmap@local@max@\relax
      \@karnaughmap@local@max@=\@karnaughmap@var@mapsizex@\relax
      \multiply\@karnaughmap@local@max@ by \@karnaughmap@var@mapsizey@\relax
      \multiply\@karnaughmap@local@max@ by \@karnaughmap@var@mapsizez@\relax
      \advance\@karnaughmap@local@max@ by -1\relax
    % [END]}
    % fill terms
    \foreach \cell in {0,1,2,...,\@karnaughmap@local@max@} {%
      \IfSubStr{\@karnaughmap@var@usedcells@}{,\cell,}{}{%
        \path (\@karnaughmap@func@decimaltobin@{\cell}) node {#1};
      }
    }
  \endgroup
  % update \@karnaughmap@var@usedcells@ (all cells are used now)
  \renewcommand{\@karnaughmap@var@usedcells@}{,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,}
}
\DeclareDocumentCommand{\indeterminants}{m} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  \terms{#1}{-}
}
\DeclareDocumentCommand{\manualterms}{m} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  \foreach \cellinfo [count=\cell from 0] in {#1} {%
    % only write to cell if it is empty otherwise fail silently
    \IfSubStr{\@karnaughmap@var@usedcells@}{,\cell,}{}{%
      \path (\@karnaughmap@func@decimaltobin@{\cell}) node {\cellinfo};
    }
  }
  % update \@karnaughmap@var@usedcells@ (previous cells + all cells up to \@karnaughmap@local@cellcount@ are used now) {[START]
    \newcommand{\@karnaughmap@local@tmpusedcells@}{}
    \newcount\@karnaughmap@local@cellcount@\relax
    % count number of cells in #1 {[START]
      \StrCount{#1}{,}[\@karnaughmap@local@tmpusedcells@]
      \@karnaughmap@local@cellcount@=\@karnaughmap@local@tmpusedcells@\relax
      \advance\@karnaughmap@local@cellcount@ by 1\relax
      \multiply\@karnaughmap@local@cellcount@ by 2\relax
    % [END]}
    % create sequence for \@karnaughmap@local@tmpusedcells@
    \StrLeft{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,}{\@karnaughmap@local@cellcount@}[\@karnaughmap@local@tmpusedcells@]
    % update \@karnaughmap@var@usedcells@ (append \@karnaughmap@local@tmpusedcells@)
    \expandafter\def\expandafter\@karnaughmap@var@usedcells@\expandafter{\@karnaughmap@var@usedcells@\@karnaughmap@local@tmpusedcells@}
  % [END]}
}
\DeclareDocumentCommand{\maxterms}{m} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  \terms{#1}{0}
}
\DeclareDocumentCommand{\minterms}{m} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  \terms{#1}{1}
}
\DeclareDocumentCommand{\terms}{m m} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  \foreach \cell in {#1} {%
    % only write to cell if it is empty otherwise fail silently
    \IfSubStr{\@karnaughmap@var@usedcells@}{,\cell,}{}{%
      \path (\@karnaughmap@func@decimaltobin@{\cell}) node {#2};
    }
  }
  % update \@karnaughmap@var@usedcells@
  \expandafter\def\expandafter\@karnaughmap@var@usedcells@\expandafter{\@karnaughmap@var@usedcells@#1,}
}

%%
%% Commands for marking the cells
%%

%% keep track of used colors
\newcount\@karnaughmap@var@colorindex@\relax
\newcount\@karnaughmap@var@colorindexnext@\relax
\@karnaughmap@var@colorindex@=0\relax
\@karnaughmap@var@colorindexnext@=1\relax

\DeclareDocumentCommand{\implicant}{m m O{0}} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  % fetch new color
  \@karnaughmap@func@updatecolor@{}
  %
  \begingroup
    % loop through specified sub maps
    \foreach \map in {#3} {%
      % make sure we don't try to draw on non existing sub maps
      \ifnum\map<\@karnaughmap@var@mapsizez@
        % calculate cell number for the specified sub maps {[START]
          \newcount\@karnaughmap@local@northwest@\relax
          \newcount\@karnaughmap@local@southeast@\relax
          \@karnaughmap@local@northwest@=\map\relax
          \@karnaughmap@local@southeast@=\map\relax
          \multiply\@karnaughmap@local@northwest@ by 16\relax
          \multiply\@karnaughmap@local@southeast@ by 16\relax
          \advance\@karnaughmap@local@northwest@ by #1\relax
          \advance\@karnaughmap@local@southeast@ by #2\relax
        % [END]}
        % only fill marking when \@karnaughmap@var@bw@ = '0'
        \ifnum0=\@karnaughmap@var@bw@
          \fill[
            rounded corners=3pt,
            fill=\@karnaughmap@var@color@,
            fill opacity=0.25,
          ] {
            ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@northwest@}.center)+(-0.3,0.3)$)
            rectangle
            ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@southeast@}.center)+(0.3,-0.3)$)
          };
        \fi
        \draw[
          rounded corners=3pt,
          draw opacity=1.0,
        ] {
          ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@northwest@}.center)+(-0.3,0.3)$)
          rectangle
          ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@southeast@}.center)+(0.3,-0.3)$)
        };
      \else
        \PackageWarning{karnaugh-map}{%
          You can only draw on existing sub maps.
          Ignoring instruction to draw on non existing sub map number \map%
        }
      \fi
    }
  \endgroup
}
\DeclareDocumentCommand{\implicantedge}{m m m m O{0}} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  % fetch new color
  \@karnaughmap@func@updatecolor@{}
  %
  \begingroup
    % helper variables {[START]
      \newcommand{\@karnaughmap@local@orientation@}{0} % '0' is a vertical and '1' is a horizontal implicant
      \newcommand{\@karnaughmap@local@coordinateone@}{0}
      \newcommand{\@karnaughmap@local@coordinatetwo@}{0}
      \newcommand{\@karnaughmap@local@mirror@}{1} % '1' or '-1' to mirror
      \newcommand{\@karnaughmap@local@bordercontent@}{}
      \newcommand{\@karnaughmap@local@fillcontent@}{}
    % [END]}
    % determinate if this is an horizontal or vertical implicant {[START]
      \newcount\@karnaughmap@local@testcaseone@\relax
      \newcount\@karnaughmap@local@testcasetwo@\relax
      \@karnaughmap@local@testcaseone@=#1\relax
      \@karnaughmap@local@testcasetwo@=#1\relax
      \advance\@karnaughmap@local@testcaseone@ by -#2\relax
      \advance\@karnaughmap@local@testcasetwo@ by -#3\relax
      \ifnum\@karnaughmap@local@testcaseone@<0 \multiply\@karnaughmap@local@testcaseone@ by -1\relax\fi
      \ifnum\@karnaughmap@local@testcasetwo@<0 \multiply\@karnaughmap@local@testcasetwo@ by -1\relax\fi
      % test case one
      \ifnum\@karnaughmap@local@testcaseone@<\@karnaughmap@var@mapsizex@
        % this is a vertical implicant
        \renewcommand{\@karnaughmap@local@orientation@}{0}
      \else
        % this is a horizontal implicant
        \renewcommand{\@karnaughmap@local@orientation@}{1}
      \fi
      % test case two
      \ifnum\@karnaughmap@local@testcasetwo@<\@karnaughmap@var@mapsizex@
        % this is a vertical implicant
        \renewcommand{\@karnaughmap@local@orientation@}{1}
      \fi
    % [END]}
    % loop through specified sub maps
    \foreach \map in {#5} {%
      % make sure we don't try to draw on non existing sub maps
      \ifnum\map<\@karnaughmap@var@mapsizez@
        % loop through both parts of the marking(ie. left and right part)
        \foreach \i in {0,1} {%
          % set parameters depending on the part of the marking(ie. left and right part) {[START]
            \ifnum\i=0
              \renewcommand{\@karnaughmap@local@coordinateone@}{#1}
              \renewcommand{\@karnaughmap@local@coordinatetwo@}{#2}
              \renewcommand{\@karnaughmap@local@mirror@}{1}
            \else
              \renewcommand{\@karnaughmap@local@coordinateone@}{#3}
              \renewcommand{\@karnaughmap@local@coordinatetwo@}{#4}
              \renewcommand{\@karnaughmap@local@mirror@}{-1}
            \fi
          % [END]}
          % calculate cell numbers for the specified sub map {[START]
            \newcount\@karnaughmap@local@coordinateonecounter@\relax
            \newcount\@karnaughmap@local@coordinatetwocounter@\relax
            \@karnaughmap@local@coordinateonecounter@=\map\relax
            \@karnaughmap@local@coordinatetwocounter@=\map\relax
            \multiply\@karnaughmap@local@coordinateonecounter@ by 16\relax
            \multiply\@karnaughmap@local@coordinatetwocounter@ by 16\relax
            \advance\@karnaughmap@local@coordinateonecounter@ by \@karnaughmap@local@coordinateone@\relax
            \advance\@karnaughmap@local@coordinatetwocounter@ by \@karnaughmap@local@coordinatetwo@\relax
            \renewcommand{\@karnaughmap@local@coordinateone@}{\@karnaughmap@local@coordinateonecounter@}
            \renewcommand{\@karnaughmap@local@coordinatetwo@}{\@karnaughmap@local@coordinatetwocounter@}
          % [END]}
          % select drawing content depending on orientation {[START]
            \ifnum\@karnaughmap@local@orientation@=0
              % this is a vertical implicant
              \renewcommand{\@karnaughmap@local@fillcontent@}{%
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(-.3,.6*\@karnaughmap@local@mirror@)$)
                --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(.3,.6*\@karnaughmap@local@mirror@)$)
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(.3,-.3*\@karnaughmap@local@mirror@)$) }
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(-.3,-.3*\@karnaughmap@local@mirror@)$) }
                -- cycle
              }
              \renewcommand{\@karnaughmap@local@bordercontent@}{%
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(.3,.6*\@karnaughmap@local@mirror@)$)
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(.3,-.3*\@karnaughmap@local@mirror@)$) }
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(-.3,-.3*\@karnaughmap@local@mirror@)$) }
                --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(-.3,.6*\@karnaughmap@local@mirror@)$)
              }
            \else
              % this is a horizontal implicant
              \renewcommand{\@karnaughmap@local@fillcontent@}{%
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(-.6*\@karnaughmap@local@mirror@,-.3)$)
                --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(-.6*\@karnaughmap@local@mirror@,.3)$)
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(.3*\@karnaughmap@local@mirror@,.3)$) }
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(.3*\@karnaughmap@local@mirror@,-.3)$) }
                -- cycle
              }
              \renewcommand{\@karnaughmap@local@bordercontent@}{%
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(-.6*\@karnaughmap@local@mirror@,.3)$)
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinateone@}.center)+(.3*\@karnaughmap@local@mirror@,.3)$) }
                { [rounded corners=3pt] --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(.3*\@karnaughmap@local@mirror@,-.3)$) }
                --
                ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinatetwo@}.center)+(-.6*\@karnaughmap@local@mirror@,-.3)$)
              }
            \fi
          % [END]}
          % draw
          % only fill marking when \@karnaughmap@var@bw@ = '0'
          \ifnum0=\@karnaughmap@var@bw@
            \fill[
              sharp corners,
              fill=\@karnaughmap@var@color@,
              fill opacity=0.25,
            ] {
              \@karnaughmap@local@fillcontent@%
            };
          \fi
          \draw[
            sharp corners,
            draw opacity=1.0,
          ] {
            \@karnaughmap@local@bordercontent@%
          };
        }
      \else
        \PackageWarning{karnaugh-map}{%
          You can only draw on existing sub maps.
          Ignoring instruction to draw on non existing sub map number \map%
        }
      \fi
    }
  \endgroup
}
\DeclareDocumentCommand{\implicantcorner}{O{0}} {%
  % bail if outside environment karnaugh-map
  \@karnaughmap@func@bailoutsideenvironment@{}
  %
  % make sure "\implicantcorner" only are used on 4x4 maps
  \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@=44
    % fetch new color
    \@karnaughmap@func@updatecolor@{}
    % loop through specified sub maps
    \foreach \map in {#1} {%
      % make sure we don't try to draw on non existing sub maps
      \ifnum\map<\@karnaughmap@var@mapsizez@
        % loop through the four corners
        \foreach \corner in {0,2,8,10} {%
          % calculate corner's properties {[START]
            \newcount\@karnaughmap@local@coordinate@\relax
            \@karnaughmap@local@coordinate@=\map\relax
            \multiply\@karnaughmap@local@coordinate@ by 16\relax
            \advance\@karnaughmap@local@coordinate@ by \corner\relax
            \newcommand{\@karnaughmap@local@mirrorx@}{0} % '1' or '-1' to mirror
            \newcommand{\@karnaughmap@local@mirrory@}{0} % '1' or '-1' to mirror
            \ifnum\corner=0 \renewcommand{\@karnaughmap@local@mirrorx@}{1}\renewcommand{\@karnaughmap@local@mirrory@}{1}\fi
            \ifnum\corner=2 \renewcommand{\@karnaughmap@local@mirrorx@}{-1}\renewcommand{\@karnaughmap@local@mirrory@}{1}\fi
            \ifnum\corner=8 \renewcommand{\@karnaughmap@local@mirrorx@}{1}\renewcommand{\@karnaughmap@local@mirrory@}{-1}\fi
            \ifnum\corner=10 \renewcommand{\@karnaughmap@local@mirrorx@}{-1}\renewcommand{\@karnaughmap@local@mirrory@}{-1}\fi
          % [END]}
          % draw
          % only fill marking when \@karnaughmap@var@bw@ = '0'
          \ifnum0=\@karnaughmap@var@bw@
            \fill[
              sharp corners,
              fill=\@karnaughmap@var@color@,
              fill opacity=0.25,
            ]
            ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinate@}.center)+(-.6*\@karnaughmap@local@mirrorx@,.6*\@karnaughmap@local@mirrory@)$)
            --
            ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinate@}.center)+(.3*\@karnaughmap@local@mirrorx@,.6*\@karnaughmap@local@mirrory@)$)
            { [rounded corners=3pt] --
            ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinate@}.center)+(.3*\@karnaughmap@local@mirrorx@,-.3*\@karnaughmap@local@mirrory@)$) }
            --
            ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinate@}.center)+(-.6*\@karnaughmap@local@mirrorx@,-.3*\@karnaughmap@local@mirrory@)$)
            -- cycle;
          \fi
          \draw[
            sharp corners,
            draw opacity=1.0,
          ]
          ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinate@}.center)+(.3*\@karnaughmap@local@mirrorx@,.6*\@karnaughmap@local@mirrory@)$)
          { [rounded corners=3pt] --
          ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinate@}.center)+(.3*\@karnaughmap@local@mirrorx@,-.3*\@karnaughmap@local@mirrory@)$) }
          --
          ($(\@karnaughmap@func@decimaltobin@{\@karnaughmap@local@coordinate@}.center)+(-.6*\@karnaughmap@local@mirrorx@,-.3*\@karnaughmap@local@mirrory@)$);
        }
      \else
        \PackageWarning{karnaugh-map}{%
          You can only draw on existing sub maps.
          Ignoring instruction to draw on non existing sub map number \map%
        }
      \fi
    }
  \else
    % print error if "\implicantcorner" are used on non 4x4 map
    \PackageError{karnaugh-map}{%
      \protect\implicantcorner\space can only be used on 4x4 maps%
    }{%
      You are trying to use \protect\implicantcorner\space on non 4x4 map.%
    }
  \fi
}
\endinput
%%
%% End of file `karnaugh-map.sty'.