Debian Codegraph @

texmf-dist/doc/latex/tikz-karnaugh/tikz-karnaugh-doc.tex

raw
1 %% This is file `tikz-karnaugh-doc.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 `tikzlibrarykarnaugh.code.tex' and its
12 %% accompanying documentation and `The Copyright Holder' referring to the
13 %% person Luis Paulo Laus.
14 %%
15 %% This software is based on Andreas W. Wieland's kvmacros.tex to whom
16 %% the author wished to express his gratitude.
17 %%
18 %% IMPORTANT NOTICE:
19 %%
20 %% For error reports, comments or suggestions in case of UNCHANGED
21 %% versions send mail to:
22 %% [email protected]
23 %%
24 %%
25 \pdfminorversion=5
26 \pdfobjcompresslevel=2
27 \documentclass[a4paper]{ltxdoc}
28 \usepackage[hyphens]{url}
29 \usepackage[version=latest]{pgf}
30 \usepackage{calc,listings,tikz}
31 \usepackage{bbding}
32 \usepackage{multirow}
33 \renewcommand{\arraystretch}{1.5}
34
35 % if you need an index.
36 \usepackage{makeidx}
37
38 % for cross-references:
39 \usepackage[pdfborder=0 0 0]{hyperref}
40 \hypersetup{%
41 colorlinks=true, % use true to enable colors below:
42 linkcolor=blue,%red,
43 filecolor=blue,%magenta,
44 pagecolor=blue,%red,
45 urlcolor=blue,%cyan,
46 citecolor=blue,
47 pdfborder=0 0 0, % PDF link-darstellung, falls colorlinks=false. 0 0 0: nix. 0 0 1: default.
48 pdfsubject={Karnaugh Maps with LaTeX and TikZ},
49 pdfauthor={Luis Paulo Laus},
50 pdftitle={Typesetting Karnaugh Maps with LaTeX and TikZ},
51 pdfkeywords={Karnaugh Maps,LaTeX,TikZ},
52 }
53
54 % We need lots of libraries...
55 \usetikzlibrary{backgrounds}
56
57 \newif\ifgdccodebasic
58 \newif\ifgdccodeogdf
59
60 \usepackage[a4paper,left=2.25cm,right=2.25cm,top=2.5cm,bottom=2.5cm,nohead]{geometry}
61 \usepackage{amsmath,amssymb}
62 \usepackage{xxcolor}
63 %\usepackage{pifont}
64 %%\usepackage{tgpagella} % no ligatures (test and orthography check)
65 \usepackage{enumitem}
66 \usepackage[T1]{fontenc}
67
68 % this is due to some stupidity in pgfmanual-en-macros.tex:
69 % if this macro is not defined, the automatic cross-referencing is
70 % disabled:
71 \def\pgfautoxrefs{1}
72
73 \input{pgfmanual-en-macros} % Borrowed from TikZ/PGF
74 %% ctan:/graphics/pgf/contrib/tikz-dependency/pgfmanual-en-macros.tex
75 %% https://mirrors.ctan.org/graphics/pgf/contrib/sa-tikz/doc/macros/pgfmanual-en-macros.tex
76
77
78 % this here allows automatic cross referencing:
79 \RequirePackage{pgfmanual}
80
81 % this here configures automatic cross referencing.
82 % It works for ANY package that uses pgfkeys and is independent on tikz/pgf.
83 \pgfkeys{
84 % whenever an unqualified key is found, the following key prefix
85 % list is tried to find a match.
86 /pdflinks/search key prefixes in={/tikz/,/pgf/},
87 %
88 % the link prefix written to the pdf file:
89 /pdflinks/internal link prefix=pgfp,
90 %
91 /pdflinks/codeexample links=true,
92 /pdflinks/warnings=false, % for debugging
93 /pdflinks/show labels=false,% for debugging
94 }
95
96 % belongs to \usepackage{makeindex}
97 \makeindex
98
99 \makeatletter
100 \renewcommand*\l@section[2]{%
101 \ifnum \c@tocdepth >\z@
102 \addpenalty\@secpenalty
103 \addvspace{1.0em \@plus\p@}%
104 \setlength\@tempdima{2.5em}%
105 \begingroup
106 \parindent \z@ \rightskip \@pnumwidth
107 \parfillskip -\@pnumwidth
108 \leavevmode \bfseries
109 \advance\leftskip\@tempdima
110 \hskip -\leftskip
111 #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
112 \endgroup
113 \fi}
114 \renewcommand*\l@subsection{\@dottedtocline{2}{2.5em}{3.3em}}
115 \renewcommand*\l@subsubsection{\@dottedtocline{3}{5.8em}{4.2em}}
116 \def\@pnumwidth{2.2em}
117 \makeatother
118
119
120 % Global styles:
121 \tikzset{
122 every plot/.style={prefix=plots/pgf-},
123 shape example/.style={
124 color=black!30,
125 draw,
126 fill=yellow!30,
127 line width=.5cm,
128 inner xsep=2.5cm,
129 inner ysep=0.5cm}
130 }
131
132 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133 %
134 % This file really starts here, all the above is for pgfmanual
135 % compatibility
136 \input ../tex/tikzlibrarykarnaugh.code % or \usetikzlibrary{karnaugh}
137
138 \sloppy
139 \begin{document}
140 \title{Typesetting Karnaugh Maps with \LaTeX\ and \tikzname}
141 \author{Luis Paulo Laus\\e-mail: \texttt{[email protected]}}
142 \date{Version: 1.5, Version date: 2022-02-15}
143
144 \maketitle
145
146
147 \section*{Abstract}
148
149 Karnaugh maps are used to simplify logic equations leading to the most compact expression of two levels for a given truth table. The drawing of them used to be a boring, annoying and error-prone task. This set of macros intend to simplify the task. They can typeset Karnaugh maps with up to twelve variables\footnote{The actual limit may be different for you.}, which is more than you might likely need\footnote{A twelve variables map contains of 4096 cells in a $64\times64$ grid. They are simply too big to handle manually and you should consider to use a software.}. You only have to provide a list of variable identifiers plus the truth table of your logic function. The macros also allow to highlight the simplifications of your logic function directly within a Karnaugh map. This package is based on \verb|kvmacros.tex| from \verb|karnaugh| package, but two drastically different ways of input the truth table are supported. The modifications carried out intended to use \tikzname\ instead of native \LaTeX\ commands allowing easier customisation, easier extension when you need to draw other elements along with the map and resulting in higher graph quality. The labels that indicate values of the variables across de rows and columns may be represented in both numerical and simplified form.
150
151
152 \section{Introduction}
153
154 Karnaugh maps \cite{Karnaugh} and Veitch charts are used to simplify logic equations. They are map representations of logic functions, and in that they are equivalent. Veitch charts are not supported by this package, but it should not be a big problem to port Andreas W. Wieland's \verb|veitch| macro, available in \verb|karnaugh| package, if you need it.
155 Please note that this package, including its documentation, is based on Andreas W. Wieland's previous work and the author wishes to register his acknowledgment. Modifications and extensions were made in order to support customization and a diversity of input forms.
156
157 Typesetting Karnaugh maps tend to be a boring, annoying and error-prone task. Please consider using a companion free java software (JQM, see Section~\ref{sec:JQM}).
158
159
160 \subsection{Introductory example\label{sec:IntroductoryE}}
161
162 With \verb|tikz-karnaugh| you can typeset big (up to twelve variables or 4096 cells) good looking maps like:
163
164 \hfill\begin{tikzpicture}[karnaugh, American style,thick,
165 grp/.style n args={3}{
166 draw=#1,fill=#1!30,
167 minimum width=#2\kmunitlength,
168 minimum height=#3\kmunitlength,
169 rounded corners=0.2\kmunitlength,
170 fill opacity=0.6,
171 rectangle}]
172 \karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}%
173 {1110011001100110}%
174 {
175 \node[grp={blue}{0.9}{1.9}] (n000) at (0.5,2.0) {};
176 \node[grp={blue}{0.9}{1.9}] (n001) at (3.5,2.0) {};
177 \draw[blue] (n000.north) to [bend left=25] (n001.north) (n000.south) to [bend right=25] (n001.south);
178 \node[grp={red}{1.9}{0.9}] (n100) at (2.0,3.5) {};
179 \node[grp={red}{1.9}{0.9}] (n110) at (2.0,0.5) {};
180 \draw[red] (n100.west) to [bend right=25] (n110.west) (n100.east) to [bend left=25] (n110.east);
181 \node[grp={orange}{0.8}{1.8}] at (0.5,3.0) {};
182 \node[red] at (2,-0.6) {American style};
183 }
184 \end{tikzpicture}\hfill and\hfill
185 \begin{tikzpicture}[karnaugh, kmbar sep=0.3\kmunitlength,thick, kmbar top sep=0.8\kmunitlength, kmbar left sep=0.7\kmunitlength,
186 grp/.style n args={3}{
187 draw=#1,fill=#1!30,
188 minimum width=#2\kmunitlength,
189 minimum height=#3\kmunitlength,
190 rounded corners=0.2\kmunitlength,
191 fill opacity=0.6,
192 rectangle}]
193 \karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}%
194 {1110011001100110}%
195 {
196 \node[grp={blue}{0.9}{1.9}] (n000) at (0.5,2.0) {};
197 \node[grp={blue}{0.9}{1.9}] (n001) at (3.5,2.0) {};
198 \draw[blue] (n000.north) to [bend left=25] (n001.north) (n000.south) to [bend right=25] (n001.south);
199 \node[grp={red}{1.9}{0.9}] (n100) at (2.0,3.5) {};
200 \node[grp={red}{1.9}{0.9}] (n110) at (2.0,0.5) {};
201 \draw[red] (n100.west) to [bend right=25] (n110.west) (n100.east) to [bend left=25] (n110.east);
202 \node[grp={orange}{0.8}{1.8}] at (0.5,3.0) {};
203 \node[red] at (2,-0.6) {simplified labels};
204 }
205 \end{tikzpicture}\hfill~\medskip
206
207 On the left we have a more traditional Gray coded labels map (American style) just like the maps found in many books on Digital Electronics and Digital System Design. On the right, the same map is presented using variable bars (simplified labels). It is very easy to configure the appearance of all maps (globally) or a particular map (individually). Just change the values of some \tikzname\ keys.
208
209
210 \subsection{Comparison with other packages}
211
212 If you ask yourself ``why another Karnaugh map typesetting package?'' the answer is easy: because I was not completely happy with the available packages I know and those are:
213 \begin{enumerate}
214 \item \verb|karnaugh|: it is a great package that uses native \LaTeX\ commands to draw the map. It supports Karnaugh maps and Veitch charts. It employs a recursive algorithm with no size limit\footnote{It works until you blow the memory out which will happen about ten to twelve variables.} which leads to an interesting kind of symmetry. Remember, Karnaugh maps are all about symmetry. It is not customisable, for instance, one cannot change the distance between bars\footnote{Those bars have been underappreciated along the history. Karnaugh \cite{Karnaugh} himself called them ``simplified labels'' and used them only to replace the Gray coded numbers showing around the map. Their true strength is the ease way they point out which variable belong to a prime implicant and which does not. An approach much easier than interpreting the Gray coded numbers.} (the marks showing around the map with variable identifiers on them) and if the variable identifier is long, it will overlap another bar. Also, I want to use \tikzname\ to draw colourful semi-transparent figures on to top of the map to highlight groups (prime implicants) and, although it is possible, it is rather difficult and the result is not very good because they always look a bit off. I have a long-time experience with this package and I have also written a java program to draw the maps because, though typesetting simple maps is easy, highlighting the prime implicants is not.
215 \item \verb|karnaughmap|: it uses \tikzname\ so you got a lot of options for customisation. It is limited to eight variable which, to be honest, should be enough for anyone. The problem is that it only draws bars (those marking mentioned above) up to four variables. Also, the order in which the variables list is inputted is different from the order employed by \verb|karnaugh|.
216 \item \verb|askmaps|: this package generates configurable American style Karnaugh maps for 2, 3, 4 and 5 variables. This style is supported by \verb|tikz-karnaugh| (see Section~\ref{sec:American}), though, in my twenty years of experience teaching the subject, I have found out that \emph{simplified labels} are much more intuitive. Package \verb|askmaps| contains four macros, one for each number of variables, and it can be used to highlight the prime implicants in the very same way that \verb|karnaugh| does.
217 \item \verb|karnaugh-map|: uses \tikzname\ to draw up to four maps of four variables leading to a 3D six variables map. The values are input as a list of indices. It contains commands for drawing implicants on top of the map. Like \verb|askmaps|, this package uses American style, but the label position can be centred or moved to corner (new in version 2.0).
218 \item \verb|kvmap|: this is a relatively new package (released on 16 September, 2020) that allows you to typeset a Karnaugh map similarly as you use a tabular environment. The current version of \verb|tikz-karnaugh| has a similar feature, though the syntax is somehow simpler. In Section~\ref{sec:JQM} the same result is obtained using a java software, but in this case the implicants (bundles or groups) highlighting is done automatically.
219 \item \verb|cartonaugh|: This is also a relatively new package (released on 15 July, 2021). It is a fork of \verb|karnaugh-map| package that draws Karnaugh maps with up to 6 variables. The documentation of both packages looks very similar; one notable difference is the position of the function and variable labels are equivalent as used \verb|tikz-karnaugh| if |American style| is enabled.
220 \end{enumerate}
221
222 Roughly speaking, there are two input orders, or formats, for the function values:
223 \begin{itemize}
224 \item the same order as they appear on the map.
225 \item the order they appear in a linear truth table following a canonical binary code. In some packages, namely \verb|karnaugh-map| and \verb|cartonaugh|, a list of indices is provided instead of an ordered list of values.
226 \end{itemize}
227
228 These formats must be minded very carefully in order to produce the desired map, therefore users are urged to read Section~\ref{sec:Inputting} and, for more details, Section~\ref{sec:Comparison}. In Section~\ref{sec:JQM} a companion software that helps with inputting the data necessary to produced the map is discussed.
229
230 Also, there are two main styles:
231 \begin{itemize}
232 \item American style is very popular in books, the association of variable and cell position is shown by Gray coded number outside the map grid;
233 \item simplified labels represent graphically the regions (columns or rows) for which a given variable is true or false. Because Karnaugh map is a graphical method, this style is very appealing although much less popular than American style.
234 \end{itemize}
235
236 The following table shows how each package deals with those two features.
237
238 \begin{center}
239 \catcode`\|=12
240 \begin{tabular}{|c|c|c|c|c|}
241 \hline
242 \multirow{2}{*}{Package} & \multicolumn{2}{c|}{Input Order} & \multicolumn{2}{c|}{Style}\\
243 \cline{2-5} \cline{3-5} \cline{4-5} \cline{5-5}
244 & Truth Table & Karnaugh Map & American & Simplified Labels\\
245 \hline
246 karnaugh & \CheckmarkBold{} & & & \CheckmarkBold{}\\
247 \hline
248 karnaughmap & \CheckmarkBold{} & & \CheckmarkBold{} & \CheckmarkBold{}\\
249 \hline
250 askmaps & \CheckmarkBold{} & & \CheckmarkBold{} & \\
251 \hline
252 karnaugh-map & \CheckmarkBold \textsuperscript{*} & & \CheckmarkBold{} & \\
253 \hline
254 kvmap & & \CheckmarkBold{} & \CheckmarkBold{} & \\
255 \hline
256 cartonaugh & \CheckmarkBold \textsuperscript{*} & & \CheckmarkBold{} & \\
257 \hline
258 tikz-karnaugh & \CheckmarkBold{} & \CheckmarkBold{} & \CheckmarkBold{} & \CheckmarkBold{}\\
259 \hline
260 \multicolumn{5}{l}{\textsuperscript{*} List of indices (cell position) instead list of
261 values.}\\
262 \end{tabular}
263 \catcode`\|=13
264 \par\end{center}
265
266 The only package that supports both input orders or formats is \verb|tikz-karnaugh|. With \verb|tikz-karnaugh| you can typeset big (up to twelve variables or 4096 cells) good looking maps with Gray code labels (American style) or bars (simplified labels). Using a companion free java software (JQM, see Section~\ref{sec:JQM}), you can do it automatically, including highlighting the solution.
267
268
269 \subsection{Inputting the truth table\label{sec:Inputting}}
270
271 To create a Karnaugh map, the first thing you have to do is to load \tikzname. For this, type |\usepackage{tikz}| in the preamble of your document. Then, if the package is somewhere \TeX\ can find it, load the library with the command |\usetikzlibrary{karnaugh}|. If it is not, you can use something like |\input tikzlibrarykarnaugh.code|. You may need to provide the full or relative path to file |tikzlibrarykarnaugh.code.tex|. User of online services like overleaf (\url{https://www.overleaf.com/}) will need to check to version and, if it is not updated, will also need to upload the file tikzlibrarykarnaugh.code.tex to same folder.
272
273 Now, consider the logic function~$f(a,b,c,d)$ of Section~\ref{sec:IntroductoryE}. If you need to typeset a Karnaugh map for $f(a,b,c,d)$, it is likely that this function would be given in one of two ways: 1) a truth table; 2) a Karnaugh map (from a book, for example). Returning to Section~\ref{sec:IntroductoryE}, the most important data are the values that function assumes. This can be represented in a chart like:
274
275 \begin{center}
276 \tikz[karnaugh cell size=5mm,karnaugh,thick,disable bars]{
277 \karnaughmap{4}{}{}{1110 0110 0110 0110}{}}
278 \end{center}
279
280 Other relevant information is the set of input variables and their relation with rows and columns of the map: $a$, $c$, $b$ and $d$, observe the order. So, the example of Section~\ref{sec:IntroductoryE} can easily be put into a Karnaugh map by using the |\karnaughmaptab| macro in a \tikzname\ environment (|\begin{tikzpicture}|) or inline command (|\tikz|) as:
281
282 \begin{codeexample}[width=5.2cm]
283 \tikz[karnaugh]{
284 \karnaughmaptab{4}{f(a,b,c,d)}{acbd}{%
285 1110
286 1001
287 1001
288 0110
289 }{}
290 }
291 \end{codeexample}
292
293 Inline command |\tikz| receives a list of options and one mandatory parameter, the macro |\karnaughmaptab| itself. The |\karnaughmaptab| macro has five mandatory parameters:
294 \begin{enumerate}
295 \item the number of variables in the map, 4 in this example;
296 \item an identifier for the function, f(a,b,c,d) in this example;
297 \item a list of variable identifiers for the variables, acbd in this example (order is important);
298 \item the list of values of $f(a,b,c,d)$ as they appear in the map\footnote{Spaces and newlines are not important and can be added for the sake of readability. If, however, you need to skip one cell, insert a pair of curly brackets, \{\}. It is usually a good idea to replace all zeros (or ones if you desire a zero map) by pairs of curly brackets. Karnaugh map is a graphical method and overloading the map with useless information can undermine the user ability to find the solution.}; and
299 \item a possibly empty set of \tikzname\ commands that will be drawn before the function values so the values will appear on top of them.
300 \end{enumerate}
301
302 The variable identifiers in the third parameter are ordered such that the variables associated with rows appear before the variables associated with columns and the most significant variable for the Gray code outside the map appear first. In other words, list the variables on left side of the map from most to least significant and then list the variables on top of the map also from most to least significant.
303
304 The fifth parameter remains empty in this example, it will be discussed further on.
305
306 Suppose now, instead of a Karnaugh map, you have a logic function~$f(a,b,c,d)$ with the following truth table:
307
308 \begin{center}
309 \catcode`\|=12
310 \begin{tabular}{|r|cccc|c|}
311 \hline
312 Index & $a$ & $b$ & $c$ & $d$ & $f$\\
313 \hline
314 \hline
315 0 & 0 & 0 & 0 & 0 & 1\\
316 1 & 0 & 0 & 0 & 1 & 1\\
317 2 & 0 & 0 & 1 & 0 & 1\\
318 3 & 0 & 0 & 1 & 1 & 0\\
319 \hline
320 4 & 0 & 1 & 0 & 0 & 0\\
321 5 & 0 & 1 & 0 & 1 & 1\\
322 6 & 0 & 1 & 1 & 0 & 1\\
323 7 & 0 & 1 & 1 & 1 & 0\\
324 \hline
325 8 & 1 & 0 & 0 & 0 & 0\\
326 9 & 1 & 0 & 0 & 1 & 1\\
327 10 & 1 & 0 & 1 & 0 & 1\\
328 11 & 1 & 0 & 1 & 1 & 0\\
329 \hline
330 12 & 1 & 1 & 0 & 0 & 0\\
331 13 & 1 & 1 & 0 & 1 & 1\\
332 14 & 1 & 1 & 1 & 0 & 1\\
333 15 & 1 & 1 & 1 & 1 & 0\\
334 \hline
335 \end{tabular}
336 \catcode`\|=13
337 \end{center}
338
339 Rewriting the function values in the correct order as they should appear in the map is time consuming and error-prone. So, why not let \LaTeX\ take care of the details for you? This logic function can be put into a Karnaugh map by using the |\karnaughmap| macro in a \tikzname\ environment (|\begin{tikzpicture}|) or inline command (|\tikz|) as:
340
341 \begin{codeexample}[width=5.2cm]
342 \tikz[karnaugh, enable indices]{
343 \karnaughmap{4}{f(a,b,c,d)}{abcd}{%
344 1110 0110 0110 0110
345 }{}
346 }
347 \end{codeexample}
348
349 Note that, this time, the order in which parameters \#3 and \#4 are typed in is different from the previous example. Parameter \#3 is the list of input variables in the most to least significant bit as in truth table. Parameter \#4 is just the last columns of truth table. The indices (little red number inside a cell) are enabled so one can relate each cell on the map to its position in the truth table. In other word, the index in the cell corresponds to the index in truth table.
350
351 The |\karnaughmap| macro, just like |\karnaughmaptab|, has five mandatory parameters:
352 \begin{enumerate}
353 \item the number of variables in the map;
354 \item an identifier for the function;
355 \item a list of variable identifiers for the variables, abcd (compare with the last example);
356 \item the list of values of $f$ for each line in the truth table; and
357 \item a possibly empty set of \tikzname\ commands that will be drawn before the function values so the values will appear on top of them.
358 \end{enumerate}
359
360 The variable identifiers in the third parameter are ordered from highest to lowest significance (the same way as in the truth table, with $a$ having a significance of $2^3=8$ and $d$ having a significance of $2^0=1$). The list of values of $f$ was read from lowest to highest index. The fifth parameter remains empty in this example, it will be discussed further on.
361
362 The indices in the upper left corner of each cell corresponds to the indices in the truth table. The indices can easily be calculated from the variable value in the truth table, e.g., the index for row~11 is given by: \[2^{3}\,a+2^{2}\,b+2^{1}\,c+2^{0}\,d=8\,a+4\,b+2\,c+1\,d=8+2+1=11 .\]
363
364 The macros that read the variables list and the list of logic values (i.e., parameters \#3 and \#4) work recursively. This is why variables $a$ and $c$ are assigned to Karnaugh map rows and $b$ and $d$ to columns. If you desire a different variable arrangement, you will not only need to change parameter \#3, but also \#4 accordingly. This can be troublesome; see Section~\ref{sec:JQM} for a java software that can help in this matter.
365
366 Each entry has to be one character long and spaces are allowed\footnote{White spaces are really usable to make the string more readable leading to fast verification.}, otherwise -- like a variable identifier enclosed in \$s -- you have to put it into curly brackets:
367
368 \begin{codeexample}[width=5.3cm]
369 \begin{tikzpicture}[karnaugh]
370 \karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}%
371 {0110 0110 0110 0110}{}
372 \end{tikzpicture}
373 \end{codeexample}
374
375 Observe that the labels are all in math mode in this example. Also, a \tikzname\ environment was used so |\karnaughmap| macro is not into curly brackets which is mandatory when using |\tikz| because |\karnaughmap| creates several path commands. Moreover, the indices were omitted by removing |enable indices| from the options list.
376
377 If you prefer Gray coded labels, referred herein as \emph{American style}, just type |American style| in the option list:
378
379 \begin{codeexample}[width=6.1cm]
380 \begin{tikzpicture}[karnaugh, American style]
381 \karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}%
382 {0110 0110 0110 0110}{}
383 \end{tikzpicture}
384 \end{codeexample}
385
386 See Section~\ref{sec:American} for more details on American styled maps.
387
388
389 \subsection{Comparison between \char`\\karnaughmaptab and \char`\\karnaughmap\label{sec:Comparison}}
390
391 Before we go any further in the details, it is important to understand the difference between the two input modes: truth table and table formatted Karnaugh map. If the input data is given in a linear format where the binary code associated with the input variables imposes the natural order in which the output function is written, |\karnaughmap| must be used to create a Karnaugh map in the appropriated fashion. Moreover, the input variables list also has to be written from the more to least significant bit that imposes that order. If, however, you already have a Karnaugh map and want to typeset it, it is usually much easier to type the output values in the order they appear in the Karnaugh map just like it was a simple \emph{table} using |\karnaughmaptab|. The weaky analogy between tabular environment gives the |tab| at the end of the macro name. Moreover, the input variables list has to be written in the order they appear on the given map.
392
393 Let's see how it works calling both macros with the same parameters. Instead of zeros, ones, white spaces and don't-cares, let's use a string of hexadecimal numbers from 0 to F, so we can track where the symbols go in the map. The indices are enabled, so you can see the values go in the correct position, just the notion of "correct" changes from map to map according to the macro used. Bellow, on the left we have |\karnaughmaptab| and the right |\karnaughmap|.
394
395 \hfill\begin{tikzpicture}[karnaugh, enable indices, kmbar sep=0.3\kmunitlength,thick, kmbar top sep=0.8\kmunitlength, kmbar left sep=0.7\kmunitlength]
396 \karnaughmaptab{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}{0123 4567 89AB CEDF}{
397 \node[blue] at (2,-0.6) {\char`\\karnaughmaptab};}
398 \end{tikzpicture}\hfill
399 \begin{tikzpicture}[karnaugh, enable indices, kmbar sep=0.3\kmunitlength,thick, kmbar top sep=0.8\kmunitlength, kmbar left sep=0.7\kmunitlength]
400 \karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}{0123 4567 89AB CEDF}{
401 \node[blue] at (2,-0.6) {\char`\\karnaughmap};}
402 \end{tikzpicture}\hfill~
403 \bigskip
404
405 Both macros were called with |{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}{0123 4567 89AB CEDF}{}|, but the result is different because each macro treats its arguments in a different way. |\karnaughmaptab| uses the same order the values are given, just put the value in the correct cell automatically changing the row when needed. The input variables also follow in the given order: first the variable at left side then at the top of the map. |\karnaughmap| on the other hand does something much more sophisticated. It sorts the values such that they fall in the correct cell fallowing the given linear order. It also places the input variables in the correct position according to the same order. Now, for four variables, there are 24 ways to assign the variables to a map. |\karnaughmap| takes the last given input variable and assigns it to the least significant bit associated to the columns. The second last is assigned to the least significant bit associated to the rows. The next from the right to left is again assigned to column, and that goes on until the first given input variable is assigned. The corresponding cell placement is done by a recursive macro. If you are not satisfied with the default assignment you can use a java software to customize the map any way you like (see Section~\ref{sec:JQM}).
406
407
408 \section{Karnaugh Map Library}
409
410 \begin{pgflibrary}{karnaugh}
411 This library provides \TeX\ macros to typeset Karnaugh maps.
412 This library defines the following key:
413
414 \begin{key}{/tikz/karnaugh}
415 This key should be passed as an option to a picture or a scope that contains a map, i.e., that calls |\karnaughmap| or |\karnaughmapvert| macros. It will do some internal setups.
416 \end{key}
417
418 \begin{command}{{\karnaughmap}\marg{num var}\marg{function}\marg{var list}\marg{contents}\marg{decoration}}
419 This macro creates a Karnaugh map of \meta{num var} variables for variable \meta{function} as a function of the variables listed in \meta{var list} for the values given in \meta{content} and applying the specified \meta{decoration}. Any but the first parameter can be empty.
420 \end{command}
421
422 \begin{command}{{\karnaughmaptab}\marg{num var}\marg{function}\marg{var list}\marg{contents}\marg{decoration}}
423 Similar to |\karnaughmap|, but the order in which the \meta{var list} and \meta{content} are given correspond to the map it creates.
424 \end{command}
425
426 \begin{command}{{\karnaughmapvert}\marg{num var}\marg{function}\marg{var list}\marg{contents}\marg{decoration}}
427 Similar to |\karnaughmap|, but map will be transposed (like in matrix transposition). See Section~\ref{sec:verticalmode} for more details.
428 \end{command}
429
430 \begin{command}{{\karnaughmaptabvert}\marg{num var}\marg{function}\marg{var list}\marg{contents}\marg{decoration}}
431 Similar to |\karnaughmaptab|, but map will be transposed (like in matrix transposition).
432 \end{command}
433
434 \begin{command}{{\pgfmathdectoGray}\marg{macro}\marg{number}}
435 Defines \meta{macro} as the result of converting \meta{number} from base 10 to Gray coded binary. The idea is to provide a new macro compatible with other base conversion macros described in Section~95.4~Base Conversion of reference \cite{Tantau}.
436 \begin{codeexample}[width=2.7cm]
437 \pgfmathdectoGray\mynumber{14}\mynumber
438 \end{codeexample}
439 \end{command}
440
441 \begin{command}{{\kmdectobin}\marg{number}}
442 Converts \meta{number} from base 10 to binary with the number of digits equal to |\kmvarno|, the number of variables in the map.
443 \begin{codeexample}[width=2.7cm]
444 \kmvarno=6\relax\kmdectobin{9}
445 \end{codeexample}
446 \end{command}
447
448 \begin{command}{{\kmdectoKG}\marg{number}}
449 Converts \meta{number} (usually the cell index) to a binary code that resembles Gray code deinterleaving the variables with the number of digits equal to |\kmvarno|, the number of variables in the map. See |kmcell/.style| for an example of application.
450 \begin{codeexample}[width=2.7cm]
451 \kmvarno=6\relax\kmdectoKG{9}
452 \end{codeexample}
453 \end{command}
454
455 \begin{command}{{\kmdectoKGdec}\marg{number}}
456 Converts \meta{number} (usually the cell index) to a decimal number equivalent to the binary code that resembles Gray code deinterleaving the variables. It uses |\kmxsize| to compute the correct amount. This macro is meant for tests purposes, but it can help advanced users to understand how a map is assembled. See Section~\ref{sec:JQM} for an example of use.
457 \begin{codeexample}[width=2.7cm]
458 \kmxsize=4\relax\kmdectoKGdec{13}
459 \end{codeexample}
460 \end{command}
461
462 \begin{stylekey}{/tikz/every karnaugh (initially \normalfont empty)}
463 The style automatically applied to every Karnaugh map. It can be globally set using |\tikzset| and this can be important to standardizing your maps along the document.
464 \end{stylekey}
465
466 \begin{command}{{\kmindexcounter}}
467 A \TeX\ counter for cell index. See |kmcell/.style| for an example of application.
468 \end{command}
469
470 \begin{command}{{\kmunitlength}=\marg{length}}
471 This length sets the size of an individual cell in the map. It can be set by |karnaugh cell size|. It is used to keep dimensions proportional.
472 \end{command}
473
474 \begin{key}{/tikz/karnaugh cell size=\meta{dimension} (initially 8mm)}
475 Sets the cell size and, consequently, all other dimensions that depends on the cell size. It sets the length |\kmunitlength| which is used internally but can also be used by the user when needed. Because |karnaugh| uses |\kmunitlength| to set a lot of things, it is a good idea to set the cell size first.
476 \begin{codeexample}[width=5cm]
477 \begin{tikzpicture}[karnaugh cell size=1.3em, karnaugh]
478 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
479 \end{tikzpicture}
480 \end{codeexample}
481 \noindent{}Care should be taken because not all dimensions depend on the cell size, for example, the arrow tip width of variable bars does not, so the arrow tip may touch the map if |kmbar sep| is not set to a suitable value.
482 \end{key}
483
484 \begin{key}{/tikz/American style=\meta{boolean} (default true, initially false)}
485 Boolean switch that changes the map layout to American style. The initial value is |false| meaning that the simplified labels (bars with a variable identifier on it) will be drawn unless they are explicitly replaced by American style (Gray coded labels). See Section~\ref{sec:American} for details.
486 \begin{codeexample}[width=7.2cm]
487 \begin{tikzpicture}[karnaugh, American style]
488 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
489 \end{tikzpicture}
490 \end{codeexample}
491 \end{key}
492
493 \begin{key}{/tikz/disable bars=\meta{boolean} (default true, initially false)}
494 Boolean switch that disables the typesetting of all bars and the function identifier. The original intention was to allow for manually crafted American style maps, but since version~1.3 this map style is fully supported so this switch lost its purpose. The initial value is |false| meaning that the bars will be typeset unless they are explicitly disabled.
495 \begin{codeexample}[width=4.8cm]
496 \begin{tikzpicture}[karnaugh,disable bars]
497 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
498 \end{tikzpicture}
499 \end{codeexample}
500 \noindent{}Note that |$f(a,b,c)$| and |{$a$}{$b$}{$c$}| are not used and could be empty in the example above.
501 \end{key}
502
503 \begin{stylekey}{/tikz/kmbar (initially {$\mid$-$\mid$})}
504 The style used for the top and side bars related to the variables and denoting the rows and columns for which the respective variable is~1. The initial value is \verb!|-|! meaning they all will be represented as a line with T shaped tips.
505 \begin{codeexample}[width=6.4cm]
506 \begin{tikzpicture}[karnaugh,
507 kmbar/.style={blue,<->}]
508 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
509 \end{tikzpicture}
510 \end{codeexample}
511 \noindent See Chapter~16~Arrows of reference \cite{Tantau} for more arrow options and proper control over arrow appearance. For instance, if you want to draw a bar that ends exactly aligned with the lines inside the map, you can change the style to \verb! |-|,shorten >=-0.2pt,shorten <=-0.2pt! , assuming you are using the default thickness \verb|thin| which implies \verb|line width=0.4pt| (refer to Section~15.3.1~Graphic Parameters: Line Width, Line Cap, and Line Join of reference \cite{Tantau} for \tikzname\ line width). Otherwise, the very end of the line tip will be aligned with the middle of the internal lines, but someone should have to be very careful to notice a difference of |0.2pt|. Compare the two examples below where |ultra thick| is used for emphasis.
512 \begin{codeexample}[width=6.4cm]
513 % sloppy alignment
514 \begin{tikzpicture}[karnaugh, ultra thick,
515 kmbar/.style={blue,|-|}]
516 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
517 \end{tikzpicture}
518 \end{codeexample}
519
520 \begin{codeexample}[width=6.4cm]
521 % exact alignment
522 \begin{tikzpicture}[karnaugh, ultra thick,
523 kmbar/.style={blue,
524 |-|,
525 shorten >=-0.8pt,
526 shorten <=-0.8pt}]
527 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
528 \end{tikzpicture}
529 \end{codeexample}
530 \end{stylekey}
531
532 \begin{stylekey}{/tikz/kmbar label (initially \normalfont empty)}
533 The style used for the variable identifiers on the bars. For American styled maps, it means the style for typesetting the lists of variables associated with columns and rows.
534 \begin{codeexample}[width=6.4cm]
535 \begin{tikzpicture}[karnaugh,
536 kmbar label/.style={blue,font=\Large}]
537 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
538 \end{tikzpicture}
539 \end{codeexample}
540 \end{stylekey}
541
542 \begin{key}{/tikz/kmbar sep=\meta{width} (initially 0.2\char`\\|kmunitlength|)}
543 The distance between the bar closer to the map and the map itself. It depends mainly on the line tip used in |kmbar/.style| and the thickness of the map set by |kmbox/.style|.
544 \begin{codeexample}[width=6.4cm]
545 \begin{tikzpicture}[karnaugh,
546 kmbar sep=1\kmunitlength]
547 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
548 \end{tikzpicture}
549 \end{codeexample}
550 \end{key}
551
552 \begin{key}{/tikz/kmbar top sep=\meta{width} (initially 1\char`\\|kmunitlength|)}
553 The distance between two bars on top of map. It depends mainly on the font height used in |kmbar label/.style|.
554 \begin{codeexample}[width=6.4cm]
555 \begin{tikzpicture}[karnaugh,
556 kmbar top sep=2\kmunitlength]
557 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
558 \end{tikzpicture}
559 \end{codeexample}
560 \end{key}
561
562 \begin{key}{/tikz/kmbar left sep=\meta{width} (initially 1\char`\\|kmunitlength|)}
563 The distance between two bars at the left side of map. It depends mainly on the variable identifier width and the font size used in |kmbar label/.style|.
564 \begin{codeexample}[width=7.1cm]
565 \begin{tikzpicture}[karnaugh,
566 kmbar left sep=2\kmunitlength]
567 \karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}%
568 {0110 0110 0110 0110}{}
569 \end{tikzpicture}
570 \end{codeexample}
571 \end{key}
572
573 \begin{key}{/tikz/enable indices=\meta{boolean} (default true, initially false)}
574 Boolean switch that enables the typesetting of all indices. The index corresponds to the row number in the truth table respective to the cell. The style |kmindex/.style| can modify how the indices are presented and the lengths |kmindex posx| and |kmindex posy| where they are placed. Base-10 is the default base for indices, but there are two Boolean switches, |binary index| and |Gray index|, to change the encoding. The initial value is |false| meaning that the indices will not be typeset unless they are explicitly enabled.
575 \begin{codeexample}[width=6.4cm]
576 \begin{tikzpicture}[karnaugh,
577 enable indices]
578 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
579 \end{tikzpicture}
580 \end{codeexample}
581 \end{key}
582
583 \begin{stylekey}{/tikz/kmindex (initially {red,font=\string\tiny})}
584 The style used for cell index if enable (see also |enable indices|).
585 \begin{codeexample}[width=6.4cm]
586 \begin{tikzpicture}[karnaugh,
587 enable indices,
588 kmindex/.style={blue,
589 font=\scriptsize\itshape}]
590 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
591 \end{tikzpicture}
592 \end{codeexample}
593 \end{stylekey}
594
595 \begin{key}{/tikz/kmindex posx=\meta{dimension} (initially 0.2\char`\\|kmunitlength|)}
596 The horizontal distance from the cell left side to the index centre.
597 \begin{codeexample}[width=6.4cm]
598 \begin{tikzpicture}[karnaugh,
599 enable indices,
600 kmindex posx=.8\kmunitlength]
601 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
602 \end{tikzpicture}
603 \end{codeexample}
604 \end{key}
605
606 \begin{key}{/tikz/kmindex posy=\meta{dimension} (initially 0.8\char`\\|kmunitlength|)}
607 The vertical distance from the cell bottom to the index centre.
608 \begin{codeexample}[width=6.4cm]
609 \begin{tikzpicture}[karnaugh,
610 enable indices,
611 kmindex posy=.2\kmunitlength]
612 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
613 \end{tikzpicture}
614 \end{codeexample}
615 \end{key}
616
617 \begin{command}{{/tikz/kmindex pos}=\marg{x coordinate}\marg{y coordinate}}
618 Sets |kmindex posx| and |kmindex posy| to |x| and |y| coordinates measured in |\kmunitlength| from the cell bottom left corner.
619 \begin{codeexample}[width=6.4cm]
620 \begin{tikzpicture}[karnaugh,
621 enable indices,
622 kmindex pos={0.8}{0.2}]
623 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
624 \end{tikzpicture}
625 \end{codeexample}
626 \end{command}
627
628 \begin{key}{/tikz/binary index=\meta{boolean} (default true, initially false)}
629 Boolean switch that sets the index presentation to binary code. It is convenient to also set the index coordinates. In the following example, the significance order is $a$, $b$ and $c$, meaning, $a$ is the most significant bit and $c$ is the least significant bit. Therefore, the left most bit of the indices is one only in the two left columns below $a$ bar, the middle bit is one in the bottom row as $b$ bar extends and the right most bit is one in the central columns below $c$ bar.
630 \begin{codeexample}[width=6.4cm]
631 \begin{tikzpicture}[karnaugh,
632 enable indices,
633 binary index,
634 kmindex pos={0.5}{0.8}]
635 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
636 \end{tikzpicture}
637 \end{codeexample}
638 \end{key}
639
640 \begin{key}{/tikz/Gray index=\meta{boolean} (default true, initially false)}
641 Boolean switch that sets the index presentation to binary code corresponding to the aggregate value of the variables for one cell and not the index of the truth table row respective to the cell. The difference between |Gray index| and |binary index| is that the latter is a binary coded representation of the cell position in the truth table and, because the recursive algorithm that builds the map scrambles (interleaves) the variables, it looks random at first sight. The former, descrambles (deinterleaves) the variables so it would look like a string of zeros and ones associated with row variables followed by one associated with columns in the order they are shown along the map and not in macro parameter \#3. It looks like Gray code, that's where its name came from, but in fact it is not. Therefore, |Gray index| is pretty much useless to locate the corresponding row in the truth table. However, it can be used to visualize the state of variables more easily than binary code. A secondary index can be set, see |kmcell/.style| for an example of how it can be done. Normally, enabling binary or Gray coded indices requires that the index position be adjusted. The initial value of this switch is |false| meaning that the indices will be typeset accordingly to another code, decimal or binary, depending on |binary index| switch.
642 \begin{codeexample}[width=7.2cm]
643 \begin{tikzpicture}[karnaugh,
644 American style,
645 enable indices,
646 Gray index,
647 kmindex pos={0.5}{0.8}]
648 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
649 \end{tikzpicture}
650 \end{codeexample}
651 \end{key}
652
653 \begin{stylekey}{/tikz/kmsep line (initially \normalfont empty)}
654 The style applied to the line that separates the list of variables assigned to columns from the list of variables assigned to rows in an American styled map.
655 \begin{codeexample}[width=7.2cm]
656 \begin{tikzpicture}[karnaugh,
657 American style,
658 kmsep line/.style={thick,draw=blue}]
659 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
660 \end{tikzpicture}
661 \end{codeexample}
662 \end{stylekey}
663
664 \begin{key}{/tikz/kmsep line length=\meta{dimension} (initially 1\char`\\|kmunitlength|)}
665 The length divided by $\sqrt{2}$ of the line that separates the list of variables assigned to columns from the list of variables assigned to rows in an American styled map. Actually, this key sets the vertical and horizontal distances from the top left of the map to the end of the line. The initial value of 1~|\kmunitlength| means that the line has a total length of $\sqrt{2}$~|\kmunitlength|.
666 \begin{codeexample}[width=6.8cm]
667 \begin{tikzpicture}[karnaugh,
668 American style,
669 kmsep line/.style={thick,draw=red},
670 kmsep line length=0.5\kmunitlength]
671 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
672 \end{tikzpicture}
673 \end{codeexample}
674 \end{key}
675
676 \begin{stylekey}{/tikz/kmcell (initially \normalfont empty)}
677 The style used for cell contents.
678 \begin{codeexample}[width=6.4cm]
679 \begin{tikzpicture}[karnaugh,
680 kmcell/.style={blue,font=\Large}]
681 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
682 \end{tikzpicture}
683 \end{codeexample}
684
685 Some interesting applications of |kmcell/.style| involves the cell index given by |\the\kmindexcounter|. You can name every cell for future use or place a label within the cell index just like |enable indices| does. In the following example, |kmcell/.style| is used to place a label within each cell with the decimal value of the cell index and |enable indices| is for the binary value. Moreover, the cell content is also a number that corresponds to the cell index (manually placed) just to show the correlation, namely, the cell index corresponds to the cell position it the parameter \#4.
686 \begin{codeexample}[width=6.4cm]
687 \begin{tikzpicture}[karnaugh,
688 enable indices,
689 binary index,
690 kmindex pos={0.5}{0.8},
691 kmcell/.style={green!60!black,
692 label={[font=\scriptsize,blue,
693 label distance=-0.3\kmunitlength]
694 below left:\the\kmindexcounter}}]
695 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0123 4567}{}
696 \end{tikzpicture}
697 \end{codeexample}
698
699 So, let's compare the binary index, internally generated by |\kmdectobin|, with pseudo index generated by |\kmdectoKG| when |Gray index| is set:
700 \begin{codeexample}[width=6.4cm]
701 \begin{tikzpicture}[karnaugh,
702 enable indices,
703 binary index,
704 kmindex pos={0.5}{0.8},
705 kmcell/.style={green!60!black,
706 label={[font=\tiny,blue,
707 label distance=-0.2\kmunitlength]
708 below:\kmdectoKG{\kmindexcounter}}}]
709 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0123 4567}{}
710 \end{tikzpicture}
711 \end{codeexample}
712 \noindent The binary index at the top in red is the representation of the cell position in binary so it contains the values of $abc$. Pseudo Gray code at the bottom in blue is the deinterleaved values of the cell position placing $b$ in the leftmost position, in other worlds, it is just $bac$. This code is convenient to represent the code obtained placing the variables side by side as they appear in the left and top of the map. Curious readers are invited to relate the values indicated by the variable bars with the red and blue codes.
713 \end{stylekey}
714
715 \begin{stylekey}{/tikz/kmlabel top (initially {above,blue,font=\string\footnotesize\string\ttfamily})}
716 The style used for Gray coded labels at the top of American styled maps. Variable |\x| can be used to express the column position from 0 to |\kmxsize|$-1$. The distance from the map can be controlled through |/tikz/yshift=|\meta{dimension}, |/pgf/inner sep=|\meta{dimension}, |/pgf/inner ysep=|\meta{dimension}, |/pgf/minimum width=|\meta{dimension} or |above=|\meta{dimension}, for instance. Naturally, |yshift|, |inner ysep| and |above| depend on the rotation and might need to be replaced by something else like |xshift|, |inner xsep| or |right|, as in the example below.
717 \begin{codeexample}[width=7.2cm]
718 \begin{tikzpicture}[karnaugh,
719 American style,
720 kmlabel top/.style={red,font=\small,
721 rotate=90,right=4pt,
722 draw=cyan,very thin,
723 label={[green!60!black,
724 label distance=1em]right:\x}}]
725 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}%
726 {0110 0110}{}
727 \end{tikzpicture}
728 \end{codeexample}
729 \end{stylekey}
730
731 \begin{stylekey}{/tikz/kmlabel left (initially {left,blue,font=\string\footnotesize\string\ttfamily})}
732 The style used for Gray coded labels at the left side of American styled maps. Variable |\y| can be used to express the row position from 0 to |\kmysize|$-1$. The distance from the map can be controlled through |/tikz/xshift=|\meta{dimension}, |/pgf/inner sep=|\meta{dimension}, |/pgf/inner xsep=|\meta{dimension}, |/pgf/minimum width=|\meta{dimension} or |left=|\meta{dimension}.
733 \begin{codeexample}[width=7.2cm]
734 \begin{tikzpicture}[karnaugh,
735 American style,
736 kmlabel left/.style={red,font=\small,
737 left=4pt,draw=cyan,very thin,
738 label={[green!60!black]left:\y}}]
739 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}%
740 {0110 0110}{}
741 \end{tikzpicture}
742 \end{codeexample}
743 \end{stylekey}
744
745 \begin{stylekey}{/tikz/kmvar (initially \normalfont empty)}
746 The style used for the variable name (function) of the map.
747 \begin{codeexample}[width=6.9cm]
748 \begin{tikzpicture}[karnaugh,
749 kmvar/.style={blue,font=\Large}]
750 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
751 \end{tikzpicture}
752 \end{codeexample}
753 \end{stylekey}
754
755 \begin{stylekey}{/tikz/kmbox (initially \normalfont empty)}
756 The style used for the box surrounding the map.
757 \begin{codeexample}[width=6.4cm]
758 \begin{tikzpicture}[karnaugh,
759 kmbox/.style={blue,very thick}]
760 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
761 \end{tikzpicture}
762 \end{codeexample}
763 \end{stylekey}
764
765 \begin{stylekey}{/tikz/kmlines (initially \normalfont empty)}
766 The style used for the lines separating adjacent rows and columns inside the map.
767 \begin{codeexample}[width=6.4cm]
768 \begin{tikzpicture}[karnaugh,
769 kmlines/.style={blue,very thick}]
770 \karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0110 0110}{}
771 \end{tikzpicture}
772 \end{codeexample}
773 \end{stylekey}
774
775 You can add options to the graphics by setting the |every karnaugh/.style| which is automatically applied.
776
777 \end{pgflibrary}
778
779
780 \section{Adjusting the map size}
781
782 Possibly the most important feature that you can change is the size of the diagrams and it is done by changing the size of the cells within the map, simply by typing:
783
784 \begin{codeexample}[width=8.1cm]
785 \begin{tikzpicture}[karnaugh cell size=14mm,
786 karnaugh]
787 \karnaughmap{4}{$f$}{abcd}{0110 0110 0110 0110}{}
788 \end{tikzpicture}
789 \end{codeexample}
790
791 You can set the cell size using |karnaugh cell size| locally or globally using |\tikzset{karnaugh cell size=14mm}| or modifying the length |\kmunitlength|. The setting of the |\kmunitlength| remains active until you change it again\footnote{Or, of course, until you leave the group in which you redefined the value.}. Just remember that, if you need to define the coordinate multiple (|x| or |y|) in terms of |\kmunitlength|, you must set |karnaugh cell size| \emph{before} setting |x| or |y|.
792
793
794 \section{Marking simplifications}
795
796 The already mentioned fifth parameter can be used if you want to draw something inside the Karnaugh map. For example, this is useful if you want to show how you simplified a logic function highlighting the prime implicants:
797
798 \begin{codeexample}[width=5.3cm]
799 \begin{tikzpicture}[karnaugh,
800 thick,
801 grp/.style n args={3}{#1,fill=#1!30,
802 minimum width=#2\kmunitlength,
803 minimum height=#3\kmunitlength,
804 rounded corners=0.2\kmunitlength,
805 fill opacity=0.6,
806 rectangle,draw}]
807 \karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}%
808 {0110 0110 0110 0110}%
809 {
810 \node[grp={blue}{0.9}{1.9}](n000) at (0.5,2.0) {};
811 \node[grp={blue}{0.9}{1.9}](n001) at (3.5,2.0) {};
812 \draw[blue] (n000.north) to [bend left=25] (n001.north)
813 (n000.south) to [bend right=25] (n001.south);
814 \node[grp={red}{1.9}{0.9}](n100) at (2.0,3.5) {};
815 \node[grp={red}{1.9}{0.9}](n110) at (2.0,0.5) {};
816 \draw[red] (n100.west) to [bend right=25] (n110.west)
817 (n100.east) to [bend left=25] (n110.east);
818 }
819 \end{tikzpicture}
820 \end{codeexample}
821
822 \noindent{}and the corresponding expression is:\[f(a,b,c,d) = {\color{blue}c\,\bar{d}} + {\color{red}\bar{c}\,d}\]where colours were used to relate the subexpression with the prime implicant highlighted on the map.
823
824 Instead of \LaTeX's graphics macros, as in the original package, you can use \tikzname\ for this purpose. In this example, a new style |grp| was defined in order to draw semi-transparent rectangles with a specified colour, width and height (both given in |\kmunitlength|). The Karnaugh map has its datum at the lower left point \emph{exactly}. The centre point coordinates of those rectangles are specified using the |at| command. The length of a single cell within the Karnaugh map is equal to |\kmunitlength|. Thus, the |x| and |y| units are set to |1\kmunitlength| by the |tikzpicture| environment option |karnaugh| so the coordinates can be written without the unit and the rectangles will fall in the precise position even if one changes the map size by changing the |\kmunitlength|. Just remember that if |\kmunitlength| is set, using perhaps |karnaugh cell size|, \emph{after} the option |karnaugh| you will need to set |x| and |y| manually. Thus, it is preferable to set |\kmunitlength| \emph{before} option |karnaugh|. As a good design practice, always place |karnaugh cell size| first and then write |karnaugh| in the option list when you need a custom size.
825
826 Highlighting the solution can be an arduous and error-prone task. See Section~\ref{sec:JQM} for JQM, a java free software that can generate Karnaugh maps with the implicants automatically highlighted and the simplified equation colour coded accordingly to the highlights. You will still need \verb|tikz-karnaugh| package to render the map. By the way, the highlights seem in this section and the next two sections were generated using JQM.
827
828
829 \section{Complete example}
830
831 In this and in the next sections, examples of how individual variables and cell contents can be format are presented. The syntax relies on square brackets (|[]|) to enclose \tikzname\ features that change the appearance and add more graphs to an individual variable or cell content. Let us see a more interesting and colourful example:
832
833 \begin{codeexample}[code only]
834 \begin{tikzpicture}[karnaugh cell size=2.5em, karnaugh,
835 thick,
836 grp/.style n args={3}{#1,fill=#1!30,
837 minimum width=#2\kmunitlength,
838 minimum height=#3\kmunitlength,
839 rounded corners=0.2\kmunitlength,
840 fill opacity=0.6,
841 rectangle,draw},
842 kmbar/.style={blue,<->,double=white,semithick},
843 kmbar left sep=1.2\kmunitlength,
844 kmbar sep=0.4\kmunitlength,
845 kmbar label/.style={red!70!black,font=\large},
846 kmindex/.style={orange,font=\tiny},
847 enable indices,
848 kmcell/.style={cyan!80!black},
849 kmbox/.style={brown,thick},
850 kmlines/.style={brown,thin},
851 kmvar/.style={green!70!black,font=\huge},
852 lbl/.style={left,align=right,text width=1.5\kmunitlength}]
853 \karnaughmap{6}{$z_{0}$}{%
854 {$x_{5}$}{$x_{2}$}{$x_{4}$}{$x_{1}$}%
855 {[yellow!70!black,name=Nv,|-|,double=red,very thick,
856 label={[font=\tiny,green!50!black]above:var.},
857 text=blue!60!red]$x_{3}$}%
858 {$x_{0}$}}%
859 {--1{}1{}1{}-11-1{}1{}1-{}1{}{}1-1-{}1-{}-1-1%
860 {[red,name=Nc,label={[name=Nl,orange!90!black,
861 label distance=1\kmunitlength]left:Special},
862 circle,inner sep=2pt,draw=green!70!blue]1}%
863 --{}1{}-11{}-{}1--1{}1{}{}111-{}1{}--1}%
864 {
865 \node[grp={blue}{1.9}{0.9}](n000) at (1.0,7.5) {};
866 \node[grp={blue}{1.9}{0.9}](n002) at (7.0,7.5) {};
867 \node[grp={blue}{1.9}{1.9}](n010) at (1.0,4.0) {};
868 \node[grp={blue}{1.9}{1.9}](n012) at (7.0,4.0) {};
869 \node[grp={blue}{1.9}{0.9}](n030) at (1.0,0.5) {};
870 \node[grp={blue}{1.9}{0.9}](n032) at (7.0,0.5) {};
871 \draw[blue] (n000.east) to [bend left=25] (n002.west)
872 (n010.east) to [bend left=25] (n012.west)
873 (n030.east) to [bend right=25] (n032.west)
874 (n000.south) to [bend right=25] (n010.north)
875 (n002.south) to [bend left=25] (n012.north)
876 (n010.south) to [bend right=25] (n030.north)
877 (n012.south) to [bend left=25] (n032.north);
878 \node[grp={red}{0.8}{7.8}](n100) at (0.5,4.0) {};
879 \node[grp={red}{0.8}{7.8}](n101) at (3.5,4.0) {};
880 \draw[red] (n100.north) to [bend left=25] (n101.north)
881 (n100.south) to [bend right=25] (n101.south);
882 \node[grp={orange}{1.9}{1.9}](n200) at (5.0,6.0) {};
883 \node[grp={orange}{1.9}{1.9}](n220) at (5.0,2.0) {};
884 \draw[orange] (n200.west) to [bend right=25] (n220.west)
885 (n200.east) to [bend left=25] (n220.east);
886 \node[grp={teal}{1.8}{1.8}](n300) at (6.0,6.0) {};
887 \node[grp={teal}{1.8}{1.8}](n320) at (6.0,2.0) {};
888 \draw[teal] (n300.west) to [bend right=25] (n320.west)
889 (n300.east) to [bend left=25] (n320.east);
890 }
891 \draw[<-] (Nv) -- +(-1,1) node[lbl]{variable on its bar};
892 \draw[<-] (Nc) -- +(-1,-1) node[lbl]{special cell};
893 \draw[<-] (Nl.120) -- +(-1.1,3.5) node[lbl]{label for special cell};
894 \end{tikzpicture}
895 \end{codeexample}
896
897 The corresponding Karnaugh map looks like this:
898
899 \begin{center}
900 \begin{tikzpicture}[karnaugh cell size=2.5em, karnaugh,
901 thick,
902 grp/.style n args={3}{#1,fill=#1!30,
903 minimum width=#2\kmunitlength,
904 minimum height=#3\kmunitlength,
905 rounded corners=0.2\kmunitlength,
906 fill opacity=0.6,
907 rectangle,draw},
908 kmbar/.style={blue,<->,double=white,semithick},
909 kmbar left sep=1.2\kmunitlength,
910 kmbar sep=0.4\kmunitlength,
911 kmbar label/.style={red!70!black,font=\large},
912 kmindex/.style={orange,font=\tiny},
913 enable indices,
914 kmcell/.style={cyan!80!black},
915 kmbox/.style={brown,thick},
916 kmlines/.style={brown,thin},
917 kmvar/.style={green!70!black,font=\huge},
918 lbl/.style={left,align=right,text width=1.5\kmunitlength}]
919 \karnaughmap{6}{$z_{0}$}{%
920 {$x_{5}$}{$x_{2}$}{$x_{4}$}{$x_{1}$}%
921 {[yellow!70!black,name=Nv,|-|,double=red,very thick,
922 label={[font=\tiny,green!50!black]above:var.},
923 text=blue!60!red]$x_{3}$}%
924 {$x_{0}$}}%
925 {--1{}1{}1{}-11-1{}1{}1-{}1{}{}1-1-{}1-{}-1-1%
926 {[red,name=Nc,label={[name=Nl,orange!90!black,
927 label distance=1\kmunitlength]left:Special},
928 circle,inner sep=2pt,draw=green!70!blue]1}%
929 --{}1{}-11{}-{}1--1{}1{}{}111-{}1{}--1}%
930 {
931 \node[grp={blue}{1.9}{0.9}](n000) at (1.0,7.5) {};
932 \node[grp={blue}{1.9}{0.9}](n002) at (7.0,7.5) {};
933 \node[grp={blue}{1.9}{1.9}](n010) at (1.0,4.0) {};
934 \node[grp={blue}{1.9}{1.9}](n012) at (7.0,4.0) {};
935 \node[grp={blue}{1.9}{0.9}](n030) at (1.0,0.5) {};
936 \node[grp={blue}{1.9}{0.9}](n032) at (7.0,0.5) {};
937 \draw[blue] (n000.east) to [bend left=25] (n002.west)
938 (n010.east) to [bend left=25] (n012.west)
939 (n030.east) to [bend right=25] (n032.west)
940 (n000.south) to [bend right=25] (n010.north)
941 (n002.south) to [bend left=25] (n012.north)
942 (n010.south) to [bend right=25] (n030.north)
943 (n012.south) to [bend left=25] (n032.north);
944 \node[grp={red}{0.8}{7.8}](n100) at (0.5,4.0) {};
945 \node[grp={red}{0.8}{7.8}](n101) at (3.5,4.0) {};
946 \draw[red] (n100.north) to [bend left=25] (n101.north)
947 (n100.south) to [bend right=25] (n101.south);
948 \node[grp={orange}{1.9}{1.9}](n200) at (5.0,6.0) {};
949 \node[grp={orange}{1.9}{1.9}](n220) at (5.0,2.0) {};
950 \draw[orange] (n200.west) to [bend right=25] (n220.west)
951 (n200.east) to [bend left=25] (n220.east);
952 \node[grp={teal}{1.8}{1.8}](n300) at (6.0,6.0) {};
953 \node[grp={teal}{1.8}{1.8}](n320) at (6.0,2.0) {};
954 \draw[teal] (n300.west) to [bend right=25] (n320.west)
955 (n300.east) to [bend left=25] (n320.east);
956 }
957 \draw[<-] (Nv) -- +(-1,1) node[lbl]{variable on its bar};
958 \draw[<-] (Nc) -- +(-1,-1) node[lbl]{special cell};
959 \draw[<-] (Nl.120) -- +(-1.1,3.5) node[lbl]{label for special cell};
960 \end{tikzpicture}
961 \end{center}
962 end the logic expression\footnote{This is not of any importance here, but I couldn't hold myself back. By the way, if you are curious, there are another two minimal solutions.} is \[z = {\color{blue}\bar{x}_{3}\,\bar{x}_{1}} + {\color{red}\bar{x}_{2}\,\bar{x}_{0}} + {\color{orange}x_{3}\,x_{2}\,x_{1}} + {\color{teal}x_{3}\,x_{2}\,x_{0}}.\]
963
964 You may notice that the zeros were omitted (replaced by |{}| in the list). Also, the cell~34 is special because \verb|{[red,name=Nc,| \verb|label={[name=Nl,orange!90!black,| \verb|label distance=|\verb|1\kmunitlength| \verb|]left:| \verb|Special},| \verb|circle,| \verb|inner sep=2pt,| \verb|draw=green!70!blue]1}|. You can put almost anything inside a cell using curly brackets and you can customize the cell style using square brackets. The format is: \verb|{[opt]string}| where \verb|opt| is an optional set of styles (among other \tikzname\ parameters) which will be passed as the last option of \tikzname\ command |\node| and |string| will be written inside the cell by that command. To use this syntax, it is imperative that the very first character after the opening curly brackets (\verb|{|) be the opening square brackets (\verb|[|). Matching pairs of square brackets are allowed inside the optional sequence provided that they are protected inside a pair of curly brackets. In this case, the proper content of cell~34 is just the number~1 near the end, all the rest is the style applied to this single~1, therefore coded between square brackets. The style uses \tikzname\ syntax in order to change colour, font size, add a label, add figure, add decoration and name it for future reference. In this case, two nodes are named |Nc| and |Nl| for future reference. Near the \tikzname\ environment end, those names are used to place arrows pointing to the nodes with a description. The \verb|\draw| command that draws those arrows cannot be placed inside the fifth argument of macro |\karnaughmap| because the fifth argument is typeset before the cell's contents (the fourth argument), therefore no name would be created at the time the fifth argument is typeset.
965
966 The variables identifiers (the third argument) can also be formatted individually using style, but note that the custom style will be applied to both the bar line and the node for the variable identifier. If a bar gets segmented, just like $x_3$ bar, the named node will be the top most if the bar is vertical or the right most if the bar is horizontal. The $x_3$ bar is different from the other bars because \verb?[yellow!70!black,name=Nv,|-|,double=red,? \verb|very thick,| \verb|label={[font=\tiny,green!50!black]above:var.},| \verb|text=blue!60!red]| changes its appearance. The node name |Nv| is also not available at the time the fifth argument is typeset. So, any command that makes use of it will need to be placed after the end of macro |\karnaughmap|.
967
968 The distance between bars on the left side was set to |1.2\kmunitlength| to prevent overlapping between $x_3$ (the label) and $x_4$ bar and $x_4$ and $x_5$ bar, but the distance between the bars on top was left unchanged. The distance between the map and the bars closest to it was set to |0.4\kmunitlength| to prevent overlapping between the bar tip (\tikz[baseline] \draw[blue,->,double=white,semithick] (0,0.6ex) -- (0.5cm,0.6ex);) and the map itself.
969
970 The indices can be computed by \[32\,x_{5}+8\,x_{4}+2\,x_{3}+16\,x_{2}+4\,x_{1}+1\,x_{0}\] which is a bit bizarre. The truth table values ought to be arranged according to this index order. This bizarreness is the price we pay to have the variables placed in positions which are more intuitive. See Section~\ref{sec:JQM} for a java software that can help on this matter.
971
972
973 \section{American style\label{sec:American}}
974
975 Support for American styled map was included in version~1.3 and improved in version~1.4. Although the default style is what Karnaugh calls ``simplified labels'' \cite{Karnaugh}, it is sufficient to enable the |American style| to obtain a map that resembles the maps found in many books on Digital Electronics and Digital System Design. You can use |\tikzset| to set this and other styles globally, for instance:
976
977 \begin{codeexample}[code only]
978 \tikzset{every karnaugh/.style={
979 American style,
980 kmindex/.style={orange,font=\tiny},
981 enable indices,
982 Gray index,
983 kmindex pos={0.5}{0.8},
984 kmsep line/.style={blue,double=white,semithick}}
985 }
986 \end{codeexample}
987 \noindent enables the |American style| and, consequently, disables bars; sets the style for indices; enables index in each cell; sets the index to in Gray code; sets the index position and sets the style for the line that separates row from column variables lists.
988
989 The example of the last section, just deleting some useless code, renders as:
990
991 \bgroup
992 \tikzset{every karnaugh/.style={
993 American style,
994 kmindex/.style={orange,font=\tiny},
995 enable indices,
996 Gray index,
997 kmindex pos={0.5}{0.8},
998 kmsep line/.style={blue,double=white,semithick}}
999 }
1000
1001 \begin{center}
1002 \begin{tikzpicture}[karnaugh cell size=2.5em, karnaugh,
1003 thick,
1004 grp/.style n args={3}{#1,fill=#1!30,
1005 minimum width=#2\kmunitlength,
1006 minimum height=#3\kmunitlength,
1007 rounded corners=0.2\kmunitlength,
1008 fill opacity=0.6,
1009 rectangle,draw},
1010 kmbar label/.style={red!70!black,font=\large},
1011 kmcell/.style={cyan!80!black},
1012 kmbox/.style={brown,thick},
1013 kmlines/.style={brown,thin},
1014 kmvar/.style={green!70!black,font=\huge},
1015 lbl/.style={left,align=right,text width=1.5\kmunitlength}]
1016 \karnaughmap{6}{$z_{0}$}{%
1017 {$x_{5}$}{$x_{2}$}{$x_{4}$}{$x_{1}$}{[red]$x_{3}$}{$x_{0}$}}%
1018 {--1{}1{}1{}-11-1{}1{}1-{}1{}{}1-1-{}1-{}-1-1%
1019 {[red,name=Nc,label={[name=Nl,orange!90!black,
1020 label distance=1\kmunitlength]left:Special},
1021 circle,inner sep=2pt,draw=green!70!blue]1}%
1022 --{}1{}-11{}-{}1--1{}1{}{}111-{}1{}--1}%
1023 {
1024 \node[grp={blue}{1.9}{0.9}](n000) at (1.0,7.5) {};
1025 \node[grp={blue}{1.9}{0.9}](n002) at (7.0,7.5) {};
1026 \node[grp={blue}{1.9}{1.9}](n010) at (1.0,4.0) {};
1027 \node[grp={blue}{1.9}{1.9}](n012) at (7.0,4.0) {};
1028 \node[grp={blue}{1.9}{0.9}](n030) at (1.0,0.5) {};
1029 \node[grp={blue}{1.9}{0.9}](n032) at (7.0,0.5) {};
1030 \draw[blue] (n000.east) to [bend left=25] (n002.west)
1031 (n010.east) to [bend left=25] (n012.west)
1032 (n030.east) to [bend right=25] (n032.west)
1033 (n000.south) to [bend right=25] (n010.north)
1034 (n002.south) to [bend left=25] (n012.north)
1035 (n010.south) to [bend right=25] (n030.north)
1036 (n012.south) to [bend left=25] (n032.north);
1037 \node[grp={red}{0.8}{7.8}](n100) at (0.5,4.0) {};
1038 \node[grp={red}{0.8}{7.8}](n101) at (3.5,4.0) {};
1039 \draw[red] (n100.north) to [bend left=25] (n101.north)
1040 (n100.south) to [bend right=25] (n101.south);
1041 \node[grp={orange}{1.9}{1.9}](n200) at (5.0,6.0) {};
1042 \node[grp={orange}{1.9}{1.9}](n220) at (5.0,2.0) {};
1043 \draw[orange] (n200.west) to [bend right=25] (n220.west)
1044 (n200.east) to [bend left=25] (n220.east);
1045 \node[grp={teal}{1.8}{1.8}](n300) at (6.0,6.0) {};
1046 \node[grp={teal}{1.8}{1.8}](n320) at (6.0,2.0) {};
1047 \draw[teal] (n300.west) to [bend right=25] (n320.west)
1048 (n300.east) to [bend left=25] (n320.east);
1049 }
1050 \draw[<-] (Nc) -- +(-1,-1) node[lbl]{special cell};
1051 \draw[<-] (Nl.120) -- +(-1.1,3.5) node[lbl]{label for special cell};
1052 \end{tikzpicture}
1053 \end{center}
1054
1055 \egroup
1056
1057 One possible problem is that, depending on the number of variables, one would need to reduce the labels on top of the map to an unreasonable size to prevent them from overlapping, or to enlarger the cell size to make them fit. The same is true for indices inside the cells.
1058
1059 If, instead of |Gray index|, you use |binary index| you would notice they are quite different. This is because binary indices reflect the position of the cell as it was taken from a row of a linear truth table. Since the algorithm that builds the map distributes the variables among the rows and columns of the map in a non-trivial way, the indices depend on the cell position and its relation to the variable input order. Gray coded indices are fix. The decimal indices can still be computed by \[32\,x_{5}+8\,x_{4}+2\,x_{3}+16\,x_{2}+4\,x_{1}+1\,x_{0}.\]
1060
1061 If you are struggling with the variable position and the sequence in which variables and values should appear in order to result in the map you want, see Section~\ref{sec:JQM} for a java software that can help.
1062
1063 \section{Vertical mode\label{sec:verticalmode}}
1064
1065 For an odd number of variables, the Karnaugh map is rectangular and macros |karnaughmap| and |karnaughmaptab| will typeset it twice as wide as it is high (not taking into account the bars or other features outside the grid). Like this single variable map:
1066
1067 \begin{codeexample}[width=3cm]
1068 \tikz[karnaugh,enable indices]{
1069 \karnaughmap{1}{$f(a)$}{{$a$}}{10}{}
1070 }
1071 \end{codeexample}
1072
1073 This layout is good for presentations because the projection area is usually wider than higher. Paper sheets, on the other hand, are usually higher than wider, so for a big map you may need something like\footnote{Or you can use landscape.}:
1074
1075 \begin{codeexample}[width=3cm]
1076 \tikz[karnaugh,enable indices]{
1077 \karnaughmapvert{1}{$f(a)$}{{$a$}}{10}{}
1078 }
1079 \end{codeexample}
1080
1081 This is called, for lack of a better name, vertical mode\footnote{Not to be confused with \TeX\ vertical mode.} and it is done by the |\karnaughmapvert| and |\karnaughmaptabvert| macros. Note that |\karnaughmapvert| and |\karnaughmaptabvert| macros arrange the variables in a different order. Compare the two square (four variables) maps below in the normal (on the left) and vertical mode (on the right) paying attention to the indices and variable identifiers.
1082
1083 \hfill\tikz[karnaugh,enable indices]{\karnaughmap{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}{0101 1011 1011 1101}{\node[red] at (2,-0.6){Normal (horizontal) mode};}}
1084 \hfill
1085 \tikz[karnaugh,enable indices]{\karnaughmapvert{4}{$f(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}{0101 1011 1011 1101}{\node[red] at (2,-0.6){Vertical mode};}}\hfill~\medskip
1086
1087 The indices are calculated in the same way, but their position inside the map is different because the variables positions are different. It is like one map is mirrored and then rotated 90$^o$ (mirrored horizontally and rotated clockwise or mirrored vertically and rotated anticlockwise.) Exactly like matrix transposition.
1088
1089 One interesting application of vertical mode is when you want to keep consistency in variable identifier position among maps with odd and even number of variables. For example, if you want the most significant variable $a$ appearing on top of the maps you can use normal (horizontal) mode for maps of odd number of variables and vertical mode for even amounts, like this:
1090
1091 \hfill\tikz[karnaugh,enable indices]{\karnaughmap{3}{$f(a,b,c)$}{{$a$}{$b$}{$c$}}{0101 1011}{\node[red] at (2,-0.6){Normal (horizontal) mode};}}
1092 \hfill
1093 \tikz[karnaugh,enable indices]{\karnaughmapvert{4}{$g(a,b,c,d)$}{{$a$}{$b$}{$c$}{$d$}}{0101 1011 1011 1101}{\node[red] at (2,-0.6){Vertical mode};}}\hfill~\medskip
1094
1095 A more general approach is to use the java software described in Section~\ref{sec:JQM} to create maps with arbitrary variables positioning. Suppose that you desire the most significant variable $a$ to appear at the left side of a three variables map. You can do the opposite of what was done in the last example, but you will end up with a vertical map of three variables and maybe it is not what you want. Using the software described in Section~\ref{sec:JQM} allows $a$ to be placed at the left in a normal (horizontal) mode map. This will change the indices because it reorders the truth tablet such that $a$ will no longer be the most significant variable, but without changing the logic function.
1096
1097
1098 \section{Java Quine McCluskey -- JQM\label{sec:JQM}}
1099
1100 Java Quine McCluskey (JQM) implements the Quine McCluskey algorithm with Petrick's Method (or the method of prime implicants) for minimization of Boolean functions. It is available on \url{https://sourceforge.net/projects/jqm-java-quine-mccluskey/}. Up to sixteen functions of sixteen variables can be minimized. A graphical interface is provided for entering and editing the truth table that can be saved and loaded.
1101
1102 One very useful feature of JQM is that you can reorder the variables on the map to suite your particular application instead of relying exclusively on the macro to scatter your variables around.
1103
1104 You can also input a map in text mode and obtain the \tikzname\ code for creating the corresponding map, and the correspondence would be self-evident. For instance, a function $f\left(a,\,b,\,c,\,d,\,e\right)$ can be written in a text file like:
1105
1106 \begin{codeexample}[code only]
1107 .k
1108 a,b,c,d,e,,f
1109 0000 0010
1110 0111 1000
1111 0000 00-0
1112 0-00 0-00
1113 \end{codeexample}
1114
1115 \noindent where - means don't care and $f$, the output variable, is separated from the input variables by two commas. It would be rendered as:
1116
1117 \begin{codeexample}[width=8.1cm]
1118 \tikzset{
1119 grp/.style n args={3}{
1120 draw=#1,fill=#1!30,
1121 minimum width=#2\kmunitlength,
1122 minimum height=#3\kmunitlength,
1123 rounded corners=0.2\kmunitlength,
1124 fill opacity=0.6,
1125 rectangle}
1126 }
1127
1128 \begin{tikzpicture}[karnaugh,thick]
1129 \karnaughmap{5}{$f$}{{$c$}{$a$}{$d$}{$b$}{$e$}}%
1130 {000100110-00000001000010000-0-00}%
1131 {
1132 \node[grp={blue}{1.9}{0.9}] at (2.0,2.5) {};
1133 \node[grp={red}{1.9}{0.9}] at (4.0,2.5) {};
1134 \node[grp={orange}{0.9}{0.9}] at (6.5,3.5) {};
1135 }
1136 \end{tikzpicture}
1137 \end{codeexample}
1138
1139 Note that the original text file represents the obtained Karnaugh map very faithfully, but the command that creates the map above, in particular parameters \#3 and \#4, has no evident relation with the original text or the generated map. This, again, is due to the recursive algorithm that scrambles the variables.
1140
1141 Observe the sequence order of variables $a$, $b$, $c$, $d$ and $e$ in the text file and in the map: there is a clear correspondence. However, the sequence order has to be modified in parameter \#3 to compensate for scrambling produced by the algorithm and this make the manual encoding of parameter \#4 complicated. For a small map, meaning a small number of variables, it would not be too difficult to manually sort the parameters in order to obtain the desire result, but it would be simply too problematic for a bigger map. That's where JQM comes in. You can manipulate the map assigning the position of the variables in any order and the software automatically sorts the truth table accordingly. The order in which the variables will appear in the final expression is also independent on the order they appear in the map.
1142
1143 If, however, the export option 'Table mode (new algorithm)' is activate in JQM (it is new in version~1.3.4) the result would be very similar to the input file. This is because the new macro |\karnaughmaptab| is designed to keep the parity between the Karnaugh map and macro parameters, see below:
1144
1145 \begin{codeexample}[width=8.1cm]
1146 \tikzset{
1147 grp/.style n args={3}{
1148 draw=#1,fill=#1!30,
1149 minimum width=#2\kmunitlength,
1150 minimum height=#3\kmunitlength,
1151 rounded corners=0.2\kmunitlength,
1152 fill opacity=0.6,
1153 rectangle}
1154 }
1155
1156 \begin{tikzpicture}[karnaugh,thick]
1157 \karnaughmaptab{5}{$f$}{{$a$}{$b$}{$c$}{$d$}{$e$}}%
1158 {0000001001111000000000-00-000-00}%
1159 {
1160 \node[grp={blue}{1.9}{0.9}] at (2.0,2.5) {};
1161 \node[grp={red}{1.9}{0.9}] at (4.0,2.5) {};
1162 \node[grp={orange}{0.9}{0.9}] at (6.5,3.5) {};
1163 }
1164 \end{tikzpicture}
1165 \end{codeexample}
1166
1167 Another popular way of inputting the truth table is to provide a list of indices for which the output is one or \emph{don't care}. Alternatively, it can be zero and don't care. Consider the example shown in Table~XIV of reference \cite{McCluskey}. Given the binary variables $x_4$, $x_3$, $x_2$ and $x_1$ where $x_4$ is the most significant, it is desired to input the decimal equivalents of the binary numbers formed by the entries (called \emph{indices} herein) that lead the output to one (or zero) and some combinations of input conditions for which the output is not specified (don't care or $d$-entries). In this example, the decimal inputs that lead to ones are 5, 6 and 13 and the $d$-entries are 9, 14. In the notation used by McCluskey~\cite{McCluskey}:\[T=\sum\left(5,\,6,\,13\right)+d\left(9,\,14\right)\text{ where 9 and 14 are the }d\text{-terms.}\]
1168
1169 The input file should contain:
1170 \begin{codeexample}[code only]
1171 .l
1172 x_4,x_3,x_2,x_1,,T
1173 s, 5, 6, 13, d, 9, 14
1174 \end{codeexample}
1175
1176 \noindent where the first line has the command |.l| (lower case L) to indicate \emph{list mode}. In the second line there is a list of input variables (sequence order is ultimately important here) and the output variable $T$, again, separated from the input variables by two commas. The third line has a list of indices that lead the output to one starting with the letter |s| (should be |z| for a list of zeros), then the letter |d| which indicates the beginning of the $d$-entries, and finally the $d$-entries. From this text file we obtain:
1177
1178 \begin{codeexample}[width=5.1cm]
1179 \tikzset{
1180 grp/.style n args={3}{
1181 draw=#1,fill=#1!30,
1182 minimum width=#2\kmunitlength,
1183 minimum height=#3\kmunitlength,
1184 rounded corners=0.2\kmunitlength,
1185 fill opacity=0.6,
1186 rectangle}
1187 }
1188
1189 \begin{tikzpicture}[karnaugh,x=1\kmunitlength,
1190 y=1\kmunitlength,thick,
1191 enable indices,
1192 kmcell/.style={
1193 minimum height=1.5em,
1194 label={[font=\tiny,blue,
1195 label distance=-0.3\kmunitlength]
1196 below right:\kmdectoKGdec{\kmindexcounter}}}]
1197 \karnaughmap{4}{$T$}{{$x_{4}$}{$x_{2}$}{$x_{3}$}{$x_{1}$}}%
1198 {{}{}{}1{}{}1{}{}-{}1{}{}-{}}%
1199 {
1200 \node[grp={blue}{0.9}{1.9}] at (3.5,2.0) {};
1201 \node[grp={red}{0.9}{1.9}] at (1.5,2.0) {};
1202 }
1203 \end{tikzpicture}
1204 \end{codeexample}
1205
1206 \noindent where the indices were enabled and a secondary index was inserted in every cell. This secondary index, shown in blue, uses the macro |\kmdectoKGdec| to convert the decimal index given by |\kmindexcounter| to a pseudo index that is equal to the decimal equivalents of the binary numbers formed by the entries $\left(x_4,\,x_3,\,x_2,\,x_1\right)$. This is not the index used to assemble the map which is shown in red, is equal to |\kmindexcounter| and is the decimal equivalate to $\left(x_4,\,x_2,\,x_3,\,x_1\right)$. JQM rearranged parameters \#3 and \#4 such that, as in the previous example, the map is typeset accordingly to the specification given in the input text file. To force the macro |\karnaughmap| to behave as expected, JQM swapped variable $x_2$ and $x_3$ and resequenced parameter \#4. This is done automatically and the user does not have to worried with the details. As expected, the result shows $x_4$ and $x_3$ assigned to the rows (at the left-hand side of the map) and $x_2$ and $x_1$ assigned to the columns (on the top of the map).
1207
1208
1209 \subsection{JQM options to export to \LaTeX\ file}
1210
1211 When exporting a \LaTeX\ file from JQM, bear in mind that if a minimized truth table is exported, the corresponding map, or maps for multiple outputs or multiple solutions, will has the solution highlighted. Conversely, non-minimized truth tables will not present the solution although they can be exported.
1212
1213 There are several options that impacts on the ''.tex'' file produced. Frist of all, you can either produced a complete document or just an insert to be included in a master document. Next you see options regarding Boolean expressions, truth table and ladder diagram (PLC program). Interested readers can find the description in JQM ReadMe file.
1214
1215 Karnaugh maps options starts given the possibility of creating maps with both zeros and ones. The default behaviour is to replace zeros (in a ones map, sum of products, disjunctive normal form, or OR of ANDs) or ones (in a zeros map, product of sums, conjunctive normal form or AND of ORs) by empty cells. The intention is to keep the map as clean as possible. By checking the box, both zeros and ones will appear on the map.
1216
1217 Next option gives the possibility of generating the Boolean expression. Moreover, this expression is colour coded: each implicant is generated in the corresponding colour of the highlight on the map.
1218
1219 Then you have the choice between truth table mode (which uses |\karnaughmap|) and table formatted Karnaugh map (which uses |\karnaughmaptab|). The preselect option favours |\karnaughmaptab|. Although the macro used is different, the generated map is the same because JQM arranges the data send to the macro accordingly. Thus, if you like to see in your ''.tex'' file how map will look like, use the table mode. If you prefer to see the truth table uncheck the box.
1220
1221 Vertical mode, see Section~\ref{sec:verticalmode}, is supported. In this mode, the map will look like it has been transposed. The variables associated with rows will be associated with columns vice-versa. A rectangular map (with an old number of variables) will look twice as high as it is wide in this mode and that is why it is called \emph{vertical mode}. For a square map (with an even number of variables), the same effect can be obtained reordering the input variables\footnote{There is a tiny difference that appears when implicants are segmented in several parts particularly in big maps. In this case, some lines that connect those parts are vertical instead of horizontal.}.
1222
1223 You can generate tests (exams) using package |examdesign|. This package can be used to create different tests versions of the exam preventing cheating. The support included in JQM for |examdesign| allow the generation of answer sheet with solution and exam (obviously, without solution).
1224
1225 Finally, we have a button that opens a new window for selecting the order in which the variables will appear on the map. There are tree preestablished orders, but user is free to move the variables around assorting them in any order. Note that the input variable order, which can be change in the main window, affects the variables position in both Boolean expression and Karnaugh map. Thus, it is always a good idea to verify the desired order before exporting.
1226
1227 Users are encouraged to experiment with those options and see which ones fit some particular need.
1228
1229
1230 \subsection{JQM main features}
1231 \begin{itemize}
1232 \item Up to 16 input variables.
1233 \item Up to 16 functions (output variables).
1234 \item Petrick's Method used to find solutions covered by non-essential prime implicant.
1235 \item Comfortable graphical interface allows variable renaming and column reorder.
1236 \item Truth table can be saved in CSV file for external editing or reuse. Then it can be loaded again. Also, one can generate the truth table using other software and ``import'' (open) for edition and minimization.
1237 \item Besides using the graphical interface, truth table can be written in a text file then load in the software. Several formats are available including: list decimal representing implicants and don't care, Karnaugh map and CSV with wildcards.
1238 \item Results can be expressed in several formats like: conventional Boolean expression, \LaTeX, Structured Text (ST) and Ladder Diagram (LD).
1239 \item Results can be exported to HTML file and open in an internet browser.
1240 \item Because this software aims PLC programming, solutions are independent (non-simultaneous). It would not be too difficult to modify the algorithm to support simultaneous solution.
1241 \item Generates Karnaugh maps in HTML and \LaTeX.
1242 \item Not only solves the problem, but also shows how the solution was obtained.
1243 \item To use the software just download the \verb|zip| file, \verb|unzip| it and double click on ``JQM-QuineMcCluskey.jar''. Please see ReadMe.txt, or LeiaMe.txt if you prefer Portuguese, for more details and examples.
1244 \end{itemize}
1245
1246
1247 \section{Final remarks}
1248
1249 To effectively typeset good looking Karnaugh maps you may need to know more about \tikzname\ and this material is beyond the scope of this text, but you can refer to \cite{Tantau}.
1250
1251 In case you find a bug, or if you have comments or suggestions, please send me an e-mail.
1252
1253 The maximum size map I could produce was a Karnaugh map with twelve variables; with bigger maps I only exceeded \TeX's main memory. This is due to the huge amount of \tikzname\ nodes necessary to create the map. Quite likely you will exceed \TeX's capacity with even smaller maps if they occur in large documents.
1254
1255
1256 \begin{thebibliography}{1}
1257 \bibitem{Karnaugh}\textbf{Karnaugh}, Maurice. The map method for synthesis of combinational logic circuits, \textit{Transactions of the American Institute of Electrical Engineers, Part I: Communication and Electronics} 72(5), 593--599, 1953.
1258 \bibitem{Tantau}\textbf{Tantau}, Till. \textit{The TikZ and PGF Packages} : Manual for version 3.1.9a-34-ga0d9cada, \url{https://github.com/pgf-tikz/pgf}, 2021.
1259 \bibitem{McCluskey}\textbf{McCluskey}, Jr., Edward Joseph. Minimization of Boolean functions, \textit{The Bell System Technical Journal} 35(6), 1417--1444, 1956.
1260 \end{thebibliography}
1261
1262 \end{document}