# 5 Variable Karnaugh Map

I was trying to draw in Lyx a Karnaugh map in 5 variables, so I looked around the site and looked for some sort of solution and I came across the karnaugh-map package. And I tried to draw the map for the function Σ(1,2,4,7,12,18,22,24,26,27,31) and this is what I got

This package draws incredibly K-maps of 2,3,4 variables. However, at 5 variables it basically duplicates the 4 variable map, which causes a big problem - The order isn't correct. The first row for example is 0132 5687 which is problematic as there's more than a one digit difference between 2 and 5. I need it in the format 0132 6754 (the way I was taught) meaning the right table is in the order 10 11 01 00 instead.

Another smaller problem I have is that it auto-generates only 2 digits at the top instead of 3 digits. This is okay as I'm supposed to adress the third variable at the bottom (as I did).

Is there anything I can do to adress my (main) problem? Thanks in advance.

EDIT: The code I used to draw this diagram

begin{karnaugh-map}[4][4][2][d,e][a,b][c ]
\minterms{1,2,4,7,12,18,22,24,26,27,31}
\maxterms{0,3,5,6,8,9,10,11,13,14,15,16,17,18,19,20,21,23,25,28,29,30}
\autoterms[X]
\implicant{4}{6}
\implicant{1}{5}
\implicant{3}{6}
\implicant{29}{29}
\end{karnaugh-map}

• Have you tried anything? – Sebastiano Jul 12 '18 at 20:42
• I know nothing about such maps, but anyways it would be nice if you could add the code used to generate the diagrams. – Torbjørn T. Jul 12 '18 at 20:42
• @Sebastiano I found a solution which does this diagram manually (tex.stackexchange.com/questions/140567/…) and I tried to alter it so it fits my case. But considering I have near zero knowledge at tikz that didn't work – Theorem Jul 12 '18 at 20:52
• @TorbjørnT. most of the "code" is in the package itself, so the only part which I had to write is pretty small (and I edited it in) – Theorem Jul 12 '18 at 20:53
• @Theorem Very good. This could be a starting point. On your code there is a missing \backslash for begin. – Sebastiano Jul 12 '18 at 20:53

Not sure I fully understood you, but the order of the columns is hardcoded in the definition of the karnaugh-map environment, so in order to change it you need to change that definition. Add the following code block after \usepackage{karnaugh-map}, in Document --> Settings --> LaTeX preamble:

\RenewDocumentEnvironment{karnaugh-map}{s O{4} O{4} O{1} O{$X_1X_0$} O{$X_3X_2$} O{$X_5X_4$}} {%
\begingroup
% store map size {[START]
\renewcommand{\@karnaughmap@var@mapsizex@}{#2}%
\renewcommand{\@karnaughmap@var@mapsizey@}{#3}%
\renewcommand{\@karnaughmap@var@mapsizez@}{#4}%
% [END]}
% determinate if markings should be color or black and white
\IfBooleanTF{#1}{%
% should be black and white
\renewcommand{\@karnaughmap@var@bw@}{1}%
}{%
% should be color
\renewcommand{\@karnaughmap@var@bw@}{0}%
}%
%
% 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} \&                      10 \&                       11 \&                       01 \&                      00 \& \phantom{00} \\
00 \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \& |(000011)|  \phantom{0} \& |(000010)|  \phantom{0} \&              \& |(010010)|  \phantom{0} \&  |(010011)|  \phantom{0} \&  |(010001)|  \phantom{0} \& |(010000)|  \phantom{0} \&              \\
01 \& |(000100)|  \phantom{0} \& |(000101)|  \phantom{0} \& |(000111)|  \phantom{0} \& |(000110)|  \phantom{0} \&              \& |(010110)|  \phantom{0} \&  |(010111)|  \phantom{0} \&  |(010101)|  \phantom{0} \& |(010100)|  \phantom{0} \&              \\
11 \& |(001100)|  \phantom{0} \& |(001101)|  \phantom{0} \& |(001111)|  \phantom{0} \& |(001110)|  \phantom{0} \&              \& |(011110)|  \phantom{0} \&  |(011111)|  \phantom{0} \&  |(011101)|  \phantom{0} \& |(011100)|  \phantom{0} \&              \\
10 \& |(001000)|  \phantom{0} \& |(001001)|  \phantom{0} \& |(001011)|  \phantom{0} \& |(001010)|  \phantom{0} \&              \& |(011010)|  \phantom{0} \&  |(011011)|  \phantom{0} \&  |(011001)|  \phantom{0} \& |(011000)|  \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
% 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
% labels
% for all dimensions
\node[above] at (\@karnaughmap@var@mapsizex@*0.5,\@karnaughmap@var@mapsizey@+0.9) {\small{#5}};
\node[left] at (-0.9,\@karnaughmap@var@mapsizey@*0.5) {\small{#6}};
% when there are 2 sub maps
\ifnum\@karnaughmap@var@mapsizez@=2
\node[above] at (7,4.9) {\small{#5}};
% extra sub maps labels
\node[below] at (2,-0.1) {\small{#7$=0$}};
\node[below] at (7,-0.1) {\small{#7$=1$}};
\fi
% when there are 4 sub maps
\ifnum\@karnaughmap@var@mapsizez@=4
\node[above] at (7,4.9) {\small{#5}};
\node[left] at (-0.9,-3) {\small{#6}};
% extra sub maps labels
\node[below] at (2,-0.1) {\small{#7$=00$}};
\node[below] at (7,-0.1) {\small{#7$=01$}};
\node[below] at (2,-5.1) {\small{#7$=10$}};
\node[below] at (7,-5.1) {\small{#7$=11$}};
\fi
% 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@%
};
}{
\end{tikzpicture}
\endgroup
}


The output will then be

What I did was to rearrange the columns in the "template" matrices found in that definition. I only changed the one under \ifnum\@karnaughmap@var@mapsizex@\@karnaughmap@var@mapsizey@\@karnaughmap@var@mapsizez@=442 though, so if you want a 4x4x4 setup more modifications are needed.

Complete LaTeX example code:

\documentclass[tikz,border=5]{standalone}
\usepackage{karnaugh-map}
\makeatletter
\RenewDocumentEnvironment{karnaugh-map}{s O{4} O{4} O{1} O{$X_1X_0$} O{$X_3X_2$} O{$X_5X_4$}} {%
\begingroup
% store map size {[START]
\renewcommand{\@karnaughmap@var@mapsizex@}{#2}%
\renewcommand{\@karnaughmap@var@mapsizey@}{#3}%
\renewcommand{\@karnaughmap@var@mapsizez@}{#4}%
% [END]}
% determinate if markings should be color or black and white
\IfBooleanTF{#1}{%
% should be black and white
\renewcommand{\@karnaughmap@var@bw@}{1}%
}{%
% should be color
\renewcommand{\@karnaughmap@var@bw@}{0}%
}%
%
% 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} \&                      10 \&                       11 \&                       01 \&                      00 \& \phantom{00} \\
00 \& |(000000)|  \phantom{0} \& |(000001)|  \phantom{0} \& |(000011)|  \phantom{0} \& |(000010)|  \phantom{0} \&              \& |(010010)|  \phantom{0} \&  |(010011)|  \phantom{0} \&  |(010001)|  \phantom{0} \& |(010000)|  \phantom{0} \&              \\
01 \& |(000100)|  \phantom{0} \& |(000101)|  \phantom{0} \& |(000111)|  \phantom{0} \& |(000110)|  \phantom{0} \&              \& |(010110)|  \phantom{0} \&  |(010111)|  \phantom{0} \&  |(010101)|  \phantom{0} \& |(010100)|  \phantom{0} \&              \\
11 \& |(001100)|  \phantom{0} \& |(001101)|  \phantom{0} \& |(001111)|  \phantom{0} \& |(001110)|  \phantom{0} \&              \& |(011110)|  \phantom{0} \&  |(011111)|  \phantom{0} \&  |(011101)|  \phantom{0} \& |(011100)|  \phantom{0} \&              \\
10 \& |(001000)|  \phantom{0} \& |(001001)|  \phantom{0} \& |(001011)|  \phantom{0} \& |(001010)|  \phantom{0} \&              \& |(011010)|  \phantom{0} \&  |(011011)|  \phantom{0} \&  |(011001)|  \phantom{0} \& |(011000)|  \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
% 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
% labels
% for all dimensions
\node[above] at (\@karnaughmap@var@mapsizex@*0.5,\@karnaughmap@var@mapsizey@+0.9) {\small{#5}};
\node[left] at (-0.9,\@karnaughmap@var@mapsizey@*0.5) {\small{#6}};
% when there are 2 sub maps
\ifnum\@karnaughmap@var@mapsizez@=2
\node[above] at (7,4.9) {\small{#5}};
% extra sub maps labels
\node[below] at (2,-0.1) {\small{#7$=0$}};
\node[below] at (7,-0.1) {\small{#7$=1$}};
\fi
% when there are 4 sub maps
\ifnum\@karnaughmap@var@mapsizez@=4
\node[above] at (7,4.9) {\small{#5}};
\node[left] at (-0.9,-3) {\small{#6}};
% extra sub maps labels
\node[below] at (2,-0.1) {\small{#7$=00$}};
\node[below] at (7,-0.1) {\small{#7$=01$}};
\node[below] at (2,-5.1) {\small{#7$=10$}};
\node[below] at (7,-5.1) {\small{#7$=11$}};
\fi
% 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@%
};
}{
\end{tikzpicture}
\endgroup
}
\makeatother
\begin{document}

\begin{karnaugh-map}[4][4][2][d,e][a,b][c ]
\minterms{1,2,4,7,12,18,22,24,26,27,31}
\maxterms{0,3,5,6,8,9,10,11,13,14,15,16,17,18,19,20,21,23,25,28,29,30}
\autoterms[X]
\implicant{4}{6}
\implicant{1}{5}
\implicant{3}{6}
\implicant{29}{29}
\end{karnaugh-map}

\end{document}

• As you can see, there's a difference between the positioning of 1's and 0's in both our diagrams. For example, what used to be square 24 is now square 12 (I think). Any idea why and how to fix it? it's supposed to look like this prntscr.com/k5t6nr – Theorem Jul 12 '18 at 22:06
• @Theorem Forgive my confusion, but this is a point where I have no idea what you mean, possibly because I know nothing about such diagrams. You said in your question that you needed the second table in the order 10 11 01 00. That is, as I understand it, what you get with my code. In the image you link to, the second table seems to be in the opposite order. – Torbjørn T. Jul 12 '18 at 22:21
• @Theorem That said, if you look at the "template" matrices, each one starts with a row like \& 00 \& 01 \& .... In the one under 442, the last four of these are the ones you seem to want to change (back to what the package originally does?), so you could just rearrange them as you like. Though if the package makes sense, then I would have thought that messes up the logic of the table, that the column heads no longer corresponds to the rest of the cells in the column. Or I'm just ignorant. – Torbjørn T. Jul 12 '18 at 22:30
• Yeah, I looked at the code and I was able to alter the order. Thanks!! – Theorem Jul 13 '18 at 6:36