Debian Codegraph @

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

raw
1 \documentclass{article}
2 \input kvmacros
3 \sloppy
4 \begin{document}
5 \title{Typesetting Karnaugh Maps and Veitch Charts with \LaTeX}
6 \author{Andreas W. Wieland\\eMail: \texttt{[email protected]}}
7 \date{January 7th, 2002}
8 \maketitle
9
10
11 \section{Abstract}
12
13 Karnaugh maps and Veitch charts\footnote{In previous releases of this document
14 I referred to Karnaugh maps as Karnaugh-Veitch-Maps. This was definitely not
15 correct. Thanks to F.~M.~Brown for pointing this error out to me.} are used
16 to simplify logic equations. The drawing of them used to be a boring,
17 annoying and error-prone task. With the included macros this is no longer a
18 problem. They can typeset Karnaugh maps and Veitch charts with up to ten
19 variables, which is more than you might likely need. You only have to provide
20 a list of variable identifiers plus the function table of your logic
21 function(s). The macros also allow to mark the simplifications of your logic
22 function directly within a Karnaugh map or Veitch chart.
23
24 \section{Introduction}
25
26 Karnaugh maps and Veitch charts are used to simplify logic equations. They are
27 map representations of logic functions, and in that they are equivalent. The
28 difference between the two kinds of diagrams is the way in which the variables
29 of the functions are located within the diagrams: ``The Karnaugh map \ldots{}
30 displays a function's discriminants according to the reflected binary (Gray)
31 code. The Veitch chart \ldots{} displays the discriminants in natural binary
32 order.'' (F.~M.~Brown)
33
34 However, the drawing of either kind of them used to be a boring, annoying and
35 error-prone task. Since I had to typeset a 50+~page collection of exercises
36 for a basic computer-science lecture with plenty of Karnaugh maps in it, I
37 decided to write some macros that would do this task for me. Let us start
38 first with an introduction on how to use these Macros:
39
40 The first thing you have to do is to put the macro file \verb|kvmacros.tex|
41 into a directory where \TeX{} will find it, i.e. you have to put it into a
42 directory where your \texttt{TEXINPUTS} environment variable points to. You
43 can then load them by typing
44 \verb|\input kvmacros| somewhere in the preamble of your document.
45
46 Suppose now you have a logic function~$f$ with the following function table:
47
48 \begin{center}
49
50 \begin{tabular}{lr}
51 \begin{tabular}[t]{r|cccc|c}
52 Index&$a$&$b$&$c$&$d$&$f$\\
53 \hline
54 0&0&0&0&0&1\\
55 1&0&0&0&1&1\\
56 2&0&0&1&0&1\\
57 3&0&0&1&1&0\\
58 4&0&1&0&0&0\\
59 5&0&1&0&1&1\\
60 6&0&1&1&0&1\\
61 7&0&1&1&1&0\\
62 \end{tabular}
63 &
64 \begin{tabular}[t]{r|cccc|c}
65 Index&$a$&$b$&$c$&$d$&$f$\\
66 \hline
67 8&1&0&0&0&0\\
68 9&1&0&0&1&1\\
69 10&1&0&1&0&1\\
70 11&1&0&1&1&0\\
71 12&1&1&0&0&0\\
72 13&1&1&0&1&1\\
73 14&1&1&1&0&1\\
74 15&1&1&1&1&0\\
75 \end{tabular}
76 \end{tabular}
77 \end{center}
78
79 This logic function can easily be put into a Karnaugh map by using the
80 \verb|\karnaughmap|-macro\footnote{Users of older versions of the macro
81 package will notice that the name has changed. Below you will find a
82 discussion of the changes between versions.}. It has five parameters: The
83 number of variables in the map, an identifier for the function, a list of
84 variable identifiers for the variables, and the list of values of $f$ for
85 each line in the function table. The variable identifiers in the third
86 parameter are ordered from highest to lowest significance (the same way as in
87 the function table, with $a$ having a significance of $2^3=8$ and $d$ having a
88 significance of $2^0=1$). The list of values of $f$ was read from lowest to
89 highest index. The fifth parameter remains empty in this example, it will be
90 discussed below:
91
92 \begin{verbatim}
93 \karnaughmap{4}{f(a,b,c,d):}{abcd}{1110011001100110}{}
94 \end{verbatim}
95
96 This produces the following Karnaugh map, where the indices in the lower left
97 corner of each box correspond to the indices in the function
98 table:\footnote{The indices can easily be calculated from the variable values
99 in the function table, i.e. line 11: the index equals $a\cdot 2^3 + b\cdot
100 2^2 + c \cdot 2^1 + d \cdot 2^0=1\cdot 8 + 0\cdot 4 + 1 \cdot 2 + 1 \cdot
101 1=11$.}
102
103 \begin{center}
104 \karnaughmap{4}{f(a,b,c,d):}{abcd}{1110011001100110}{}
105 \end{center}
106
107 The macros work similar for Veitch charts:
108
109 \begin{verbatim}
110 \veitchchart{4}{f(a,b,c,d):}{abcd}{0110011001100110}{}
111 \end{verbatim}
112
113 \begin{center}
114 \veitchchart{4}{f(a,b,c,d):}{abcd}{0110011001100110}{}
115 \end{center}
116
117 Notice the difference in the variable location in the diagrams. In my opinion
118 Karnaugh maps are much easier to use if you need to simplify a logic function;
119 therefore Veitch charts will not be discussed any further in this
120 document. Except otherwise noticed, the macros' syntax and functionality are
121 exactly the same for Karnaugh maps and Veitch charts.
122
123 The macros that read the variable list and the list of logic values
124 (i.e. parameters \#3 and \#4) work
125 recursively. Therefore, you have to take special care to supply the
126 correct number of tokens to print: If you supply a list of variable
127 identifiers or a list of logic values that contains fewer elements than needed
128 the macros will cause error messages similar to the following:
129
130 \begin{verbatim}
131 ! Argument of \kvgetonechar has an extra }.
132 <inserted text>
133 \par
134 l.201 }{}
135
136 ?
137 \end{verbatim}
138
139 On the contrary, if any of the two lists is longer than
140 needed, \emph{you will not be notified!} Moreover, each entry has to be one
141 character long, otherwise --- like a variable identifier enclosed in \$s ---
142 you have to put it into braces:
143
144 \begin{verbatim}
145 \karnaughmap{4}{$f(a,b,c,d):$}{{$a$}{$b$}{$c$}{$d$}}{0110011001100110}{}
146 \end{verbatim}
147
148 This produces the following Karnaugh map:
149
150 \begin{center}
151 \karnaughmap{4}{$f(a,b,c,d):$}{{$a$}{$b$}{$c$}{$d$}}{0110011001100110}{}
152 \end{center}
153
154 \section{Marking simplifications}
155
156 The already mentioned fifth parameter can be used if you want to draw
157 something inside the Karnaugh map. For example this is useful if you want to
158 show how you simplified a logic function:
159
160 \begin{verbatim}
161 \karnaughmap{4}{$f(a,b,c,d):$}{{$a$}{$b$}{$c$}{$d$}}%
162 {0110011001100110}%
163 {%
164 \put(0,2){\oval(1.9,1.9)[r]}
165 \put(4,2){\oval(1.9,1.9)[l]}
166 \put(2,0){\oval(1.9,1.9)[t]}
167 \put(2,4){\oval(1.9,1.9)[b]}
168 }
169 \end{verbatim}
170
171 The corresponding Karnaugh map looks like this:
172
173 \begin{center}
174 \karnaughmap{4}{$f(a,b,c,d):$}{{$a$}{$b$}{$c$}{$d$}}%
175 {0110011001100110}%
176 {%
177 \put(0,2){\oval(1.9,1.9)[r]}
178 \put(4,2){\oval(1.9,1.9)[l]}
179 \put(2,0){\oval(1.9,1.9)[t]}
180 \put(2,4){\oval(1.9,1.9)[b]}
181 }
182 \end{center}
183
184 You can use any of \LaTeX's graphics macros for this purpose. The Karnaugh map
185 has its pivot point at the lower left point\footnote{This is not exactly true,
186 but sufficient if you do not want to modify the macros.} and a unitlength that
187 is equal to the length of a single box within the Karnaugh map. Sometimes you
188 may find that the \verb|\oval|s that you want to draw inside a Karnaugh map are
189 slightly shifted to the right. This occurs if you use the \verb|eepic|-Package
190 in your document. In this case, make a copy of the \verb|eepic.sty| file,
191 rename it to something like \verb|myeepic.sty| and comment out the definition
192 of the \verb|\oval|-macro in your local copy. If you put this new
193 \verb|myeepic.sty| file somewhere into your \TeX's search path so that \TeX{}
194 can find it and you load it properly with e.g. \verb|\usepackage{myeepic}|,
195 you shouldn't experience any more trouble.
196
197 \section{Other features}
198
199 There are some more features that you can use. Possibly the most important is
200 that you can change the size of the diagrams by changing the size of the
201 boxes within the map, simply by typing:
202
203 \begin{verbatim}
204 \kvunitlength=15mm
205 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
206 \end{verbatim}
207
208 This results in the following Karnaugh map. The setting of the
209 \verb|\kvunitlength| remains active until you change it again;\footnote{Or, of
210 course, until you leave the group in which you redefined the value. This
211 applies to all changeable parameters and macro definitions in this section!}
212 the default \verb|\kvunitlength| is 8\,mm:
213
214 \begin{center}
215 \kvunitlength=15mm
216 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
217 \end{center}
218
219 Another feature is that you can switch off the indices inside the map by
220 typing:
221
222 \begin{verbatim}
223 \kvnoindex
224 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
225 \end{verbatim}
226
227 \begin{center}
228 \kvunitlength=8mm
229 \kvnoindex
230 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
231 \end{center}
232
233 This setting also remains active until you switch indexing on again using the
234 \verb|\kvindex|-macro:
235
236 \begin{verbatim}
237 \kvindex
238 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
239 \end{verbatim}
240
241 \begin{center}
242 \kvindex
243 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
244 \end{center}
245
246 Usually, the font size of the map's contents and indices should be
247 suitable (the defaults are \verb|\tiny| for \verb|\kvindexsize| and
248 \verb|\normalsize| for \verb|\kvcontentsize|). If you want to change them, you
249 can do this by redefining the \verb|\kvcontentsize|- and
250 \verb|\kvindexsize|-macros (Preferably, you should do this in the preamble of
251 your document):
252
253 \begin{verbatim}
254 \renewcommand\kvindexsize{\footnotesize}
255 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
256 \end{verbatim}
257
258 \begin{center}
259 \renewcommand\kvindexsize{\footnotesize}
260 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
261 \end{center}
262
263 In the same way you can also change the font family (Although I doubt that
264 this is sensible):
265
266 \begin{verbatim}
267 \renewcommand\kvindexsize{\footnotesize\it}
268 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
269 \end{verbatim}
270
271 \begin{center}
272 \renewcommand\kvindexsize{\footnotesize\it}
273 \karnaughmap{4}{$f:$}{abcd}{0110011001100110}{}
274 \end{center}
275
276
277 These macros are named \verb|\kvfoo|, because they work both for Karnaugh maps
278 and Veitch charts.
279
280 \section{If you use an older version of the macros\ldots}
281
282 \ldots{} you will certainly have noticed a number of changes. The most
283 important one is that I have changed the name of the macro that draws Karnaugh
284 maps from \verb|\kvmap| to \verb|\karnaughmap|. I suggest that you use the new
285 name in documents that you newly create. However, I introduced an alias so
286 that the old macro name still works:
287
288 \begin{verbatim}
289 \def\kvmap{\karnaughmap}
290 \end{verbatim}
291
292 Also, in some older versions tokens in the variable list or function table
293 that were longer than one character had to be enclosed in \emph{double} braces
294 (If you have used any of these versions you will certainly remember this
295 annoying feature). This is no longer necessary --- however, it does no
296 harm.
297
298 If you need to know more about the exact changes, please consult the revision
299 history in \verb|kvmacros.tex|.
300
301 \section{Final remarks}
302
303 This is all you need to know about the usage of my macros. I have tried them
304 out and I haven't yet found any serious bugs, but I'm pretty sure there are
305 plenty. In this case, or if you have comments or suggestions, please send me
306 an eMail. Anyway, I would very much appreciate if you would notify me when you
307 use my macros; I would like to know if they are of any use to somebody.
308
309 The maximum size map I could produce was a Karnaugh map with 10~variables;
310 with bigger maps I only exceeded \TeX's main memory. This is due to the
311 macros' recursive algorithm. Quite likely you will exceed \TeX's capacity with
312 even smaller maps if they occur in large documents, but I think no~one in his
313 right mind will ever use Karnaugh maps of more than 6\,variables, so I didn't
314 care about this problem.
315
316
317 \section{More examples}
318
319 Below I have given some more examples of Karnaugh maps produced with my
320 macros:
321
322 \begin{verbatim}
323 \karnaughmap{1}{}{a}{01}{}
324
325 \karnaughmap{2}{$f:$}{{$a$}{$b$}}{0110}{}
326
327 \karnaughmap{3}{$f:$}{{$a$}{$b$}{$c$}}{01100110}{}
328
329 \karnaughmap{4}{$f:$}{{$a$}{$b$}{$c$}{$d$}}{0110011001100110}{}
330
331 \karnaughmap{4}{$f:$}{abcd}{abcdefghijklmnop}{}
332
333 \karnaughmap{5}{$f:$}{{$a$}{$b$}{$c$}{$d$}{$e$}}%
334 {0123456789abcdef0123456789abcdef}{}
335
336 \karnaughmap{6}{$f_i^t:$}{abcdef}%
337 {0123456789{10}{11}{12}{13}{14}{15}%
338 {16}{17}{18}{19}{20}{21}{22}{23}%
339 {24}{25}{26}{27}{28}{29}{30}{31}%
340 {32}{33}{34}{35}{36}{37}{38}{39}%
341 {40}{41}{42}{43}{44}{45}{46}{47}%
342 {48}{49}{50}{51}{52}{53}{54}{55}%
343 {56}{57}{58}{59}{60}{61}{62}{63}}{}%
344
345 \kvunitlength=6mm
346 \kvnoindex
347 \karnaughmap{8}{$q_2^4:$}{{$a$}{$b$}{$c$}{$d$}{$e$}%
348 {$f$}{$g$}{$h$}}{%
349 0011001111001100%
350 0011001111001100%
351 0011001111001100%
352 0011001111001100%
353 0011001111001100%
354 0011001111001100%
355 0011001111001100%
356 0011001111001100%
357 0011001111001100%
358 0011001111001100%
359 0011001111001100%
360 0011001111001100%
361 0011001111001100%
362 0011001111001100%
363 0011001111001100%
364 0011001111001100%
365 }{}
366 \kvunitlength=8mm
367 \kvindex
368 \end{verbatim}
369
370 \begin{flushleft}
371 \karnaughmap{1}{}{a}{01}{}
372
373 \karnaughmap{2}{$f:$}{{$a$}{$b$}}{0110}{}
374
375 \karnaughmap{3}{$f:$}{{$a$}{$b$}{$c$}}{01100110}{}
376
377 \karnaughmap{4}{$f:$}{{$a$}{$b$}{$c$}{$d$}}{0110011001100110}{}
378
379 \karnaughmap{4}{$f:$}{abcd}{abcdefghijklmnop}{}
380
381 \karnaughmap{5}{$f:$}{{$a$}{$b$}{$c$}{$d$}{$e$}}%
382 {0123456789abcdef0123456789abcdef}{}
383
384 \karnaughmap{6}{$f_i^t:$}{abcdef}%
385 {0123456789{10}{11}{12}{13}{14}{15}%
386 {16}{17}{18}{19}{20}{21}{22}{23}%
387 {24}{25}{26}{27}{28}{29}{30}{31}%
388 {32}{33}{34}{35}{36}{37}{38}{39}%
389 {40}{41}{42}{43}{44}{45}{46}{47}%
390 {48}{49}{50}{51}{52}{53}{54}{55}%
391 {56}{57}{58}{59}{60}{61}{62}{63}}{}%
392
393
394 \kvunitlength=6mm
395 \kvnoindex
396 \karnaughmap{8}{$q_2^4:$}{{$a$}{$b$}{$c$}{$d$}{$e$}%
397 {$f$}{$g$}{$h$}}{%
398 0011001111001100%
399 0011001111001100%
400 0011001111001100%
401 0011001111001100%
402 0011001111001100%
403 0011001111001100%
404 0011001111001100%
405 0011001111001100%
406 0011001111001100%
407 0011001111001100%
408 0011001111001100%
409 0011001111001100%
410 0011001111001100%
411 0011001111001100%
412 0011001111001100%
413 0011001111001100%
414 }{}
415 \kvunitlength=8mm
416 \kvindex
417 \end{flushleft}
418
419 \section{Sourcecode}
420
421 Here now you will find the current sourcecode of my macros. I should have
422 commented it more extensively, but failed due to lack of time. Sorry.
423
424 \small
425 \begin{verbatim}
426 %% This is file `kvmacros.tex',
427 %% Version of January 7th, 2002
428 %%
429 %% Copyright (C) 1998-2002 by Andreas W. Wieland, [email protected]
430 %%
431 %% This program can be redistributed and/or modified under the terms
432 %% of the LaTeX Project Public License distributed from CTAN
433 %% archives in directory macros/latex/base/lppl.txt; either
434 %% version 1 of the License, or (at your option) any later version,
435 %% with `The Program' referring to the software `kvmacros.tex' and its
436 %% accompanying documentation and `The Copyright Holder' referring to the
437 %% person Andreas W. Wieland.
438 %%
439 %%
440 %%
441 %% IMPORTANT NOTICE:
442 %%
443 %% For error reports, comments or suggestions in case of UNCHANGED
444 %% versions send mail to:
445 %% [email protected]
446 %%
447 %% Anyway, if you use my macros, send me an eMail, too. I would like to know
448 %% if they are useful to somebody out there.
449 %%
450 %% Please do not request updates from me directly. Distribution is
451 %% done through Mail-Servers and TeX organizations.
452 %%
453 %% You are allowed to distribute this file under the condition that
454 %% it is distributed together with `kvdoc.tex', `kvdoc.dvi' and `kvdoc.ps'.
455 %%
456 %% If you receive this file alone from someone, complain!
457 %%
458 \typeout{}
459 \typeout{Macros for typesetting Karnaugh maps and Veitch charts}
460 \typeout{Version of January 7th, 2002}
461 \typeout{by Andreas W. Wieland, [email protected]}
462 \typeout{}
463 %%
464 %% Change History:
465 %% August 04th, 1998: Original Version
466 %%
467 %% August 19th, 1998: Minor changes to make the lines within the maps look
468 %% better, also moved the variable identifiers from the right to the left and
469 %% added an identifier for the logic function; since then \kvmap has 5
470 %% instead of 4 parameters. I also included the documentation in an additional
471 %% Postscript-file.
472 %%
473 %% September 23rd, 1998: Fixed the problem with double braces by introducing
474 %% the \compdummy and \ende macros in the \ifx-comparisons (see below):
475 %% The parameters that are longer than one character do not have to be
476 %% enclosed in double braces any longer --- single braces are
477 %% sufficient. However, double braces will still be processed correctly.
478 %%
479 %% May 19th, 1999: Changed the documentation (which was months overdue) after
480 %% F. M. Brown pointed me to the incorrectness of the term
481 %% "Karnaugh-Veitch-map". Actually, the macros itself haven't
482 %% changed. Probably I should introduce macros for typesetting Veitch charts
483 %% as well (which would be a minor task). This version was actually never
484 %% released.
485 %%
486 %% May 25th, 1999: Introduced macros for Veitch charts
487 %% (\veitchfoo) and renamed the macros that are solely related to
488 %% Karnaugh maps from \kvfoo to \karnaughfoo. Introduced aliases for those
489 %% macros that are user-callable to maintain compatibility with older versions:
490 %% \kvmap -> \karnaughmap. However, \kvindex, \kvnoindex and \kvunitlength
491 %% haven't changed their names as they are related to both Karnaugh maps and
492 %% Veitch charts. Changed the (plain stupid) algorithm that processes the
493 %% variable-length arguments so that it takes much less time to produce large
494 %% maps. Several macros so became redundant and are no longer included. Also I
495 %% introduced \kvindexsize and \kvcontentsize to adjust the font size of the
496 %% indices and the contents of a diagram. They default to \tiny and
497 %% \normalsize.
498 %%
499 %% January 17th, 2001: Changed the eMail-address in the macro file and the
500 %% accompanying documentation and made minor changes to the documentation.
501 %%
502 %% January 7th, 2002: Exchanged the \xdef to \gdef in the definition of
503 %% \kvargumentstring and \kvgetonechar so that things like {\textbf 1} in the
504 %% argumentlist of \karnaughmap do not cause error messages any longer.
505 %% Thanks to Michal Medveck{\'y} for pointing me to this error.
506 %%
507 %%
508 %%
509 %% We need a fixed dimension for a single field in a Karnaugh map:
510 %%
511 \newdimen\kvunitlength
512 \kvunitlength=8mm
513 %%
514 %% We need a default font size for the indices:
515 %%
516 \newcommand{\kvindexsize}{\tiny}
517 %%
518 %% And we need a default size for the contents and variable identifiers:
519 %%
520 \newcommand{\kvcontentsize}{\normalsize}
521 %%
522 %% First, we have to introduce some counters:
523 %%
524 %% \kvrecursiondepth is used to control the recursion of the
525 %% \karnaughmakemap- and \veitchmakechart-macro.
526 %%
527 \newcount\kvrecursiondepth
528 %%
529 %% The \kvindexcounter is needed for the indices in the fields of the
530 %% diagrams.
531 %%
532 \newcount\kvindexcounter
533 %%
534 %% \kvxsize and \kvysize store the dimensions of an entire diagram.
535 %%
536 \newcount\kvxsize
537 \newcount\kvysize
538 %%
539 %% Some counters are necessary to compute the marks for the variable
540 %% identifiers:
541 %%
542 \newcount\kvvarno
543 \newcount\kvxvarno
544 \newcount\kvyvarno
545 \newcount\kvmarkstart
546 \newcount\kvmarklength
547 \newcount\kvmarknum
548 \newcount\kvmarkmove
549 %%
550 %% And we need a savebox to store the variable marks:
551 %%
552 \newsavebox\kvsavebox
553 %%
554 %% Single fields in a diagram should be indexed, which makes the map easier to
555 %% use. This is the default. If you don't want indices, simply call
556 %% \kvnoindex. If you want indices back, call \kvindex:
557 %%
558 \def\kvnoindex{%
559 \def\kvcurrentindex{}}
560 %%
561 \def\kvindex{%
562 \def\kvcurrentindex{%
563 \the\kvindexcounter\global\advance\kvindexcounter by 1}%
564 }
565 %%
566 \kvindex
567 %%
568 %% We need a macro that computes the powers of two:
569 %%
570 \def\kvpoweroftwo#1#2{% Computes #1=2^#2, both of which have to be counters
571 {\ifnum#2>0
572 \global\multiply#1 by 2
573 \advance#2 by -1
574 \kvpoweroftwo{#1}{#2}
575 \fi}}
576 %%
577 %% The macros \kvargumentstring, \kvgetchar and \kvgetonechar are needed to
578 %% process the variabe-length parameters in \karnaughmap and \veitchchart:
579 %%
580 \def\kvargumentstring#1{\gdef\kvdummystring{#1{}\noexpand\end}}
581 %%
582 \def\kvgetchar{\expandafter\kvgetonechar\kvdummystring}
583 %%
584 \def\kvgetonechar#1#2\end{{#1}\gdef\kvdummystring{#2\noexpand\end}}%
585 %%
586 %% The Macro \karnaughmakemap calls itself recursively until the parameter #1
587 %% equals 1, whereupon it returns a single token from the list of arguments,
588 %% enclosed in a \makebox plus the index (if enabled) in a smaller \makebox:
589 %%
590 \def\karnaughmakemap#1#2{{%
591 \kvrecursiondepth=\number#1
592 \ifnum\kvrecursiondepth>1
593 \divide\kvrecursiondepth by 2
594 \unitlength=\kvunitlength
595 \multiply\unitlength by \kvrecursiondepth
596 %%
597 \ifcase#2
598 %%
599 %% The parameter #2 of \karnaughmakemap is needed because the inner Karnaugh
600 %% maps need to be mirrored. This is achieved by the following case-statement,
601 %% which orders the inner Karnaugh maps properly:
602 %%
603 %% Case 0: top-left Karnaugh map
604 \begin{picture}(2,2)%
605 \put(0,1){\karnaughmakemap{\kvrecursiondepth}{0}}%
606 \put(1,1){\karnaughmakemap{\kvrecursiondepth}{1}}%
607 \put(0,0){\karnaughmakemap{\kvrecursiondepth}{2}}%
608 \put(1,0){\karnaughmakemap{\kvrecursiondepth}{3}}%
609 \end{picture}%
610 \or
611 %% Case 1: top-right Karnaugh map
612 \begin{picture}(2,2)%
613 \put(1,1){\karnaughmakemap{\kvrecursiondepth}{1}}%
614 \put(0,1){\karnaughmakemap{\kvrecursiondepth}{0}}%
615 \put(1,0){\karnaughmakemap{\kvrecursiondepth}{3}}%
616 \put(0,0){\karnaughmakemap{\kvrecursiondepth}{2}}%
617 \end{picture}%
618 \or
619 %% Case 2: bottom-left Karnaugh map
620 \begin{picture}(2,2)%
621 \put(0,0){\karnaughmakemap{\kvrecursiondepth}{2}}%
622 \put(1,0){\karnaughmakemap{\kvrecursiondepth}{3}}%
623 \put(0,1){\karnaughmakemap{\kvrecursiondepth}{0}}%
624 \put(1,1){\karnaughmakemap{\kvrecursiondepth}{1}}%
625 \end{picture}%
626 \or
627 %% Case 3: bottom-right Karnaugh map
628 \begin{picture}(2,2)%
629 \put(1,0){\karnaughmakemap{\kvrecursiondepth}{3}}%
630 \put(0,0){\karnaughmakemap{\kvrecursiondepth}{2}}%
631 \put(1,1){\karnaughmakemap{\kvrecursiondepth}{1}}%
632 \put(0,1){\karnaughmakemap{\kvrecursiondepth}{0}}%
633 \end{picture}%
634 \fi
635 \else
636 \unitlength=\kvunitlength
637 \begin{picture}(1,1)
638 \put(0,0){\makebox(1,1){\kvcontentsize\kvgetchar}}%
639 \put(0.05,0.05){\makebox(0.9,0.9)[bl]{\kvindexsize\kvcurrentindex}}%
640 \end{picture}
641 \fi}}%
642 %%
643 %% \karnaughmaketopmark typesets the variable marks of a Karnaugh map that are
644 %% located on top of the diagram:
645 %%
646 \def\karnaughmaketopmark{%
647 \unitlength\kvunitlength
648 \begin{picture}(\kvxsize,1)
649 \kvmarkstart=1
650 \kvpoweroftwo{\kvmarkstart}{\kvxvarno} % \kvymarkstart is the start
651 % position for the \multiput
652 \kvmarklength=\kvmarkstart
653 \multiply\kvmarklength by 2 % \kvmarklength is the length of a mark
654 \kvmarkmove=\kvmarkstart
655 \multiply\kvmarkmove by 4 % This is the move distance for the \multiput.
656 \kvmarknum=\kvxsize
657 \divide\kvmarknum by \kvmarkmove % This is the number of repetitions for
658 % the \multiput.
659 %The highest-order variable mark needs a special treatment:
660 \ifnum\kvmarknum=0\kvmarknum=1\divide\kvmarklength by 2\fi
661 \savebox\kvsavebox(\kvmarklength,1){%
662 \begin{picture}(\kvmarklength,1)
663 \put(0,0.3){\makebox(\kvmarklength,0.7){\kvcontentsize\kvgetchar}}
664 \put(0,0.1){\line(0,1){0.4}}
665 \put(\kvmarklength,0.1){\line(0,1){0.4}}
666 \put(0,0.3){\line(1,0){\kvmarklength}}
667 \end{picture}}
668 \multiput(\kvmarkstart,0)(\kvmarkmove,0){\kvmarknum}{\usebox\kvsavebox}
669 \end{picture}
670 }
671 %%
672 %% \karnaughmakeleftmark typesets the variable marks of a Karnaugh map that are
673 %% located on the left of the diagram:
674 %%
675 \def\karnaughmakeleftmark{%
676 \unitlength\kvunitlength
677 \begin{picture}(-1,\kvysize)(0,-\kvysize)
678 \kvmarkstart=1
679 \kvpoweroftwo{\kvmarkstart}{\kvyvarno} % \kvmarkstart is the start
680 % position for the \multiput
681 \kvmarklength=\kvmarkstart
682 \multiply\kvmarklength by 2 % \kvmarklength is the length of a mark
683 \kvmarkmove=\kvmarkstart
684 \multiply\kvmarkmove by 4 % This now is the move distance for the
685 % \multiput.
686 \kvmarknum=\kvysize
687 \divide\kvmarknum by \kvmarkmove % This now is the number of
688 % repetitions for the \multiput.
689 %The highest-order variable mark needs a special treatment:
690 \ifnum\kvmarknum=0\kvmarknum=1\divide\kvmarklength by 2\fi
691 \advance\kvmarkstart by \kvmarklength
692 \savebox\kvsavebox(1,\kvmarklength){%
693 \begin{picture}(1,\kvmarklength)
694 \put(-0.3,0){\makebox(-0.7,\kvmarklength){\kvcontentsize\kvgetchar}}
695 \put(-0.1,0){\line(-1,0){0.4}}
696 \put(-0.1,\kvmarklength){\line(-1,0){0.4}}
697 \put(-0.3,0){\line(0,1){\kvmarklength}}
698 \end{picture}}
699 \multiput(0,-\kvmarkstart)(0,-\kvmarkmove){\kvmarknum}{\usebox\kvsavebox}
700 \end{picture}
701 }
702 %% \karnaughmakemarks calls \karnaughmaketopmark or \karnaughmakeleftmark
703 %% depending on whether \kvvarno is odd or even.
704 %%
705 \def\karnaughmakemarks{%
706 \ifnum\kvvarno>0
707 \let\next=\karnaughmakemarks
708 \ifodd\kvvarno % We have to make a mark at the top
709 \advance\kvxvarno by -1
710 \put(0,\kvxvarno){\karnaughmaketopmark}
711 \else % We have to make a mark at the left
712 \advance\kvyvarno by -1
713 \put(-\kvyvarno,-\kvysize){\karnaughmakeleftmark}
714 \fi
715 \advance\kvvarno by -1
716 \else
717 \let\next=\relax
718 \fi
719 \next
720 }
721 %%
722 %% \karnaughmap is the macro that a user calls if he wants to draw a
723 %% Karnaugh map:
724 %%
725 \def\karnaughmap#1#2#3#4#5{%
726 %%
727 %% #1 is the number of variables in the Karnaugh map
728 %% #2 is the identifier of the function
729 %% #3 is the list of identifiers of those variables
730 %% #4 is the list of tokens that have to be written into the map
731 %% #5 is something that you want to draw inside the Karnaugh map
732 %%
733 \kvvarno=#1 % \kvvarno is the total number of variables
734 \kvyvarno=#1 % \kvyvarno is the number of variable marks at the left
735 \divide\kvyvarno by 2
736 \kvxvarno=#1 % \kvxvarno is the number of variable marks on top
737 \advance\kvxvarno by -\kvyvarno
738 \kvxsize=1
739 \kvpoweroftwo{\kvxsize}{\kvxvarno}
740 \kvysize=1
741 \kvpoweroftwo{\kvysize}{\kvyvarno}
742 \advance\kvxsize by \kvyvarno
743 \advance\kvxvarno by \kvysize
744 \unitlength\kvunitlength
745 \begin{picture}(\kvxsize,\kvxvarno)(-\kvyvarno,-\kvysize)
746 \advance\kvxsize by -\kvyvarno
747 \advance\kvxvarno by -\kvysize
748 \put(0,-\kvysize){%
749 \begin{picture}(\kvxsize,\kvysize)
750 \multiput(0,0)(0,1){\kvysize}{\line(1,0){\kvxsize}}
751 \multiput(0,0)(1,0){\kvxsize}{\line(0,1){\kvysize}}
752 \put(0,\kvysize){\line(1,0){\kvxsize}}
753 \put(\kvxsize,0){\line(0,1){\kvysize}}
754 #5
755 \end{picture}}
756 \put(-\kvyvarno,0){\makebox(\kvyvarno,\kvxvarno){#2}}
757 \kvindexcounter=0
758 \kvargumentstring{#4}
759 \put(0,-\kvysize){\karnaughmakemap{\kvysize}{0}}
760 \ifodd\kvvarno
761 {\divide\kvxsize by 2
762 \put(\kvxsize,-\kvysize){\karnaughmakemap{\kvysize}{1}}}
763 \fi
764 \kvargumentstring{#3}
765 \karnaughmakemarks
766 %%
767 \end{picture}
768 }%
769 %%
770 %% The definition of \kvmap is necessary to maintain compatibility with older
771 %% versions of this macro package:
772 %%
773 \def\kvmap{\karnaughmap}%
774 %%
775 %%
776 %% The Macro \veitchmakechart calls itself recursively until the parameter #1
777 %% equals 1, whereupon it returns a single token from the list of arguments,
778 %% enclosed in a \makebox plus the index (if enabled) in a smaller \makebox:
779 %%
780 \def\veitchmakechart#1{{%
781 \kvrecursiondepth=\number#1
782 \ifnum\kvrecursiondepth>1
783 \divide\kvrecursiondepth by 2
784 \unitlength=\kvunitlength
785 \multiply\unitlength by \kvrecursiondepth
786 %%
787 \begin{picture}(2,2)%
788 \put(0,1){\veitchmakechart{\kvrecursiondepth}}%
789 \put(1,1){\veitchmakechart{\kvrecursiondepth}}%
790 \put(0,0){\veitchmakechart{\kvrecursiondepth}}%
791 \put(1,0){\veitchmakechart{\kvrecursiondepth}}%
792 \end{picture}%
793 \else
794 \unitlength=\kvunitlength
795 \begin{picture}(1,1)
796 \put(0,0){\makebox(1,1){\kvcontentsize\kvgetchar}}%
797 \put(0.05,0.05){\makebox(0.9,0.9)[bl]{\kvindexsize\kvcurrentindex}}%
798 \end{picture}
799 \fi}}%
800 %%
801 \def\veitchmaketopmark{%
802 \unitlength\kvunitlength
803 \begin{picture}(\kvxsize,1)
804 \kvmarkstart=1
805 \kvpoweroftwo{\kvmarkstart}{\kvxvarno} % \kvymarkstart is the start
806 % position for the \multiput
807 \kvmarklength=\kvmarkstart % \kvmarklength is the length of a mark
808 \kvmarkmove=\kvmarkstart
809 \multiply\kvmarkmove by 2 % This is the move distance for the \multiput.
810 \kvmarknum=\kvxsize
811 \divide\kvmarknum by \kvmarkmove % This is the number of repetitions for
812 % the \multiput.
813 \savebox\kvsavebox(\kvmarklength,1){%
814 \begin{picture}(\kvmarklength,1)
815 \put(0,0.3){\makebox(\kvmarklength,0.7){\kvcontentsize\kvgetchar}}
816 \put(0,0.1){\line(0,1){0.4}}
817 \put(\kvmarklength,0.1){\line(0,1){0.4}}
818 \put(0,0.3){\line(1,0){\kvmarklength}}
819 \end{picture}}
820 \multiput(\kvmarkstart,0)(\kvmarkmove,0){\kvmarknum}{\usebox\kvsavebox}
821 \end{picture}
822 }
823 %%
824 \def\veitchmakeleftmark{%
825 \unitlength\kvunitlength
826 \begin{picture}(-1,\kvysize)(0,-\kvysize)
827 \kvmarkstart=1
828 \kvpoweroftwo{\kvmarkstart}{\kvyvarno} % \kvmarkstart is the start
829 % position for the \multiput
830 \kvmarklength=\kvmarkstart
831 \kvmarkmove=\kvmarkstart
832 \multiply\kvmarkmove by 2 % This now is the move distance for the \multiput.
833 \kvmarknum=\kvysize
834 \divide\kvmarknum by \kvmarkmove % This now is the number of
835 % repetitions for the \multiput.
836 \advance\kvmarkstart by \kvmarklength
837 \savebox\kvsavebox(1,\kvmarklength){%
838 \begin{picture}(1,\kvmarklength)
839 \put(-0.3,0){\makebox(-0.7,\kvmarklength){\kvcontentsize\kvgetchar}}
840 \put(-0.1,0){\line(-1,0){0.4}}
841 \put(-0.1,\kvmarklength){\line(-1,0){0.4}}
842 \put(-0.3,0){\line(0,1){\kvmarklength}}
843 \end{picture}}
844 \multiput(0,-\kvmarkstart)(0,-\kvmarkmove){\kvmarknum}{\usebox\kvsavebox}
845 \end{picture}
846 }
847 %%
848 \def\veitchmakemarks{%
849 \ifnum\kvvarno>0
850 \let\next=\veitchmakemarks
851 \ifodd\kvvarno % We have to make a mark at the top
852 \advance\kvxvarno by -1
853 \put(0,\kvxvarno){\veitchmaketopmark}
854 \else % We have to make a mark at the left
855 \advance\kvyvarno by -1
856 \put(-\kvyvarno,-\kvysize){\veitchmakeleftmark}
857 \fi
858 \advance\kvvarno by -1
859 \else
860 \let\next=\relax
861 \fi
862 \next
863 }
864 %%
865 \def\veitchchart#1#2#3#4#5{%
866 %%
867 %% #1 is the number of variables in the Veitch chart
868 %% #2 is the identifier of the function
869 %% #3 is the list of identifiers of those variables
870 %% #4 is the list of tokens that have to be written into the chart
871 %% #5 is something that you want to draw inside the Veitch chart
872 %%
873 \kvvarno=#1 % \kvvarno is the total number of variables
874 \kvyvarno=#1 % \kvyvarno is the number of variable marks at the left
875 \divide\kvyvarno by 2
876 \kvxvarno=#1 % \kvxvarno is the number of variable marks on top
877 \advance\kvxvarno by -\kvyvarno
878 \kvxsize=1
879 \kvpoweroftwo{\kvxsize}{\kvxvarno}
880 \kvysize=1
881 \kvpoweroftwo{\kvysize}{\kvyvarno}
882 \advance\kvxsize by \kvyvarno
883 \advance\kvxvarno by \kvysize
884 \unitlength\kvunitlength
885 \begin{picture}(\kvxsize,\kvxvarno)(-\kvyvarno,-\kvysize)
886 \advance\kvxsize by -\kvyvarno
887 \advance\kvxvarno by -\kvysize
888 \put(0,-\kvysize){%
889 \begin{picture}(\kvxsize,\kvysize)
890 \multiput(0,0)(0,1){\kvysize}{\line(1,0){\kvxsize}}
891 \multiput(0,0)(1,0){\kvxsize}{\line(0,1){\kvysize}}
892 \put(0,\kvysize){\line(1,0){\kvxsize}}
893 \put(\kvxsize,0){\line(0,1){\kvysize}}
894 #5
895 \end{picture}}
896 \put(-\kvyvarno,0){\makebox(\kvyvarno,\kvxvarno){#2}}
897 \kvindexcounter=0
898 \kvargumentstring{#4}
899 \put(0,-\kvysize){\veitchmakechart{\kvysize}}
900 \ifodd\kvvarno
901 {\divide\kvxsize by 2
902 \put(\kvxsize,-\kvysize){\veitchmakechart{\kvysize}}}
903 \fi
904 \kvargumentstring{#3}
905 \veitchmakemarks
906 %%
907 \end{picture}
908 }%
909 %%
910
911 %%% Local Variables:
912 %%% mode: latex
913 %%% TeX-master: t
914 %%% End:
915 \end{verbatim}
916
917 \end{document}
918
919 %%% Local Variables:
920 %%% mode: latex
921 %%% TeX-master: t
922 %%% End:
923
924