Debian Codegraph @

texmf-dist/source/latex/cartonaugh/cartonaugh.dtx

raw
1 % \iffalse meta-comment
2 % Copyright (C) 2021 Jamal Bouajjaj | 2015, 2016, 2017 Mattias Jacobsson and contributors
3 % This work, cartonaugh, is a fork of Mattias Jacobsson's [karnaugh-map](https://github.com/2pi/karnaugh-map) package with major changes. As the copyright on the base package is under [CC BY-SA](https://creativecommons.org/licenses/by-sa/3.0/), so is this fork.
4 % \fi
5 %
6 % \iffalse
7 %<*package>
8 %<package>%%
9 %<package>%% Copyright (C) 2021 Jamal Bouajjaj | 2015, 2016, 2017 Mattias Jacobsson and contributors
10 %<package>%% This work, cartonaugh, is a fork of Mattias Jacobsson's [karnaugh-map](https://github.com/2pi/karnaugh-map) package with major changes. As the copyright on the base package is under [CC BY-SA](https://creativecommons.org/licenses/by-sa/3.0/), so is this fork.
11 %<package>%%
12 %<package>
13 %<package>\NeedsTeXFormat{LaTeX2e}
14 %<package>\ProvidesPackage{cartonaugh}[2021/07/15 v1.0 Draw Karnaugh Maps]
15 %
16 %<*driver>
17 \documentclass{ltxdoc}
18 \usepackage{cartonaugh}
19 \usepackage{multicol}% for documentation
20 \usepackage{tabularx}% for documentation
21 \usepackage{float}% for documentation
22 \usepackage{hyperref}% for documentation
23 \setlength{\parindent}{0pt}
24 \setlength{\parskip}{0.6em}
25 \EnableCrossrefs
26 \CodelineIndex
27 \RecordChanges
28 \OnlyDescription
29 \begin{document}
30 \DeleteShortVerb{\|}
31 \DocInput{cartonaugh.dtx}
32 \PrintChanges
33 \end{document}
34 %</driver>
35 % \fi
36 %
37 % \CharacterTable
38 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
39 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
40 % Digits \0\1\2\3\4\5\6\7\8\9
41 % Exclamation \! Double quote \" Hash (number) \#
42 % Dollar \$ Percent \% Ampersand \&
43 % Acute accent \' Left paren \( Right paren \)
44 % Asterisk \* Plus \+ Comma \,
45 % Minus \- Point \. Solidus \/
46 % Colon \: Semicolon \; Less than \<
47 % Equals \= Greater than \> Question mark \?
48 % Commercial at \@ Left bracket \[ Backslash \\
49 % Right bracket \] Circumflex \^ Underscore \_
50 % Grave accent \` Left brace \{ Vertical bar \|
51 % Right brace \} Tilde \~}
52 %
53 %
54 % \changes{v1.0}{2021/07/15}{Initial Release}
55 %
56 % \GetFileInfo{cartonaugh.sty}
57 %
58 % \title{The \textsf{cartonaugh} package}
59 % \author{Jamal Bouajjaj}
60 % \date{\textsf{cartonaugh}~\fileversion, \filedate}
61 %
62 %\maketitle
63 %
64 % \begin{abstract}
65 % This package, a fork of Mattias Jacobsson/2pi's \href{https://github.com/2pi/karnaugh-map}{karnaugh-map} package, draws karnaugh maps with 2, 3, 4, 5, and 6 variables.
66 % It also contains commands for filling the karnaugh map with terms semi-automatically or manually.
67 % Last but not least it contains commands for drawing implicants on top of the map.
68 % Cartonaugh is a portmanteau of ``cartographer`` and ``karnaugh``.
69 % Below is an example of a two variable karnaugh map of $X_0 \oplus X_1$.
70 % \end{abstract}
71 % \begin{figure}[H]
72 % \centering
73 % \begin{cartonaugh}[2][2][1][$X_0$][$X_1$]
74 % \minterms{1,2}
75 % \autoterms[0]
76 % \implicant{1}{1}
77 % \implicant{2}{2}
78 % \end{cartonaugh}
79 % \end{figure}
80 % \tableofcontents
81 % \pagebreak
82 %
83 % \iffalse
84 % \begin{macrocode}
85 % ^^A ##########################################################################
86 % ^^A ##########################################################################
87 % ^^A #### CODE ####
88 % ^^A ##########################################################################
89 % ^^A ##########################################################################
90
91 %%
92 %% Dependencies
93 %%
94
95 %% Check if ran under LuaLaTeX. If not, exit
96 \RequirePackage{iftex}
97 \RequireLuaTeX
98
99 %% parsing arguments
100 \RequirePackage{xparse}
101
102 %% working with strings
103 \RequirePackage{xstring}
104
105 %% drawing
106 \RequirePackage{tikz}
107 \usetikzlibrary{calc,matrix}
108
109 %%
110 %% Helpers
111 %%
112 \directlua{require("cartonaugh")}
113
114 %% command raises an error if executed outside the cartonaugh environment
115 \newcommand{\@cartonaugh@func@bailoutsideenvironment@}[0]{%
116 \ifnum\@cartonaugh@var@isactive@=0
117 \PackageError{cartonaugh}{%
118 Command can not be used outside cartonaugh environment%
119 }{%
120 Do not use this command outside the cartonaugh environment.%
121 }
122 \fi
123 }
124
125 \newcommand{\cartonaughSetColorAtIndex}[2]{\directlua{CARTONAUGH_COLORS[\the\numexpr(#1)\relax] = "\luaescapestring{#2}"}}
126
127 %% store whether a kmap has been created
128 \newcommand{\@cartonaugh@var@isactive@}{0}
129 % ^^A ##########################################################################
130 % ^^A ##########################################################################
131 % ^^A #### /CODE ####
132 % ^^A ##########################################################################
133 % ^^A ##########################################################################
134 % \end{macrocode}
135 % \fi
136 %
137 % \section{Usage}
138 %
139 % \begin{environment}{cartonaugh}
140 % The |cartonaugh| environment is the base for this package, and everything related to this package happens inside an instances of this environment.
141 %
142 % \textbf{Usage:}
143 % \MakeShortVerb{\|}
144 % \begin{tabularx}{\textwidth}{l X}
145 % \small{|\begin{cartonaugh}|} & \\
146 % \small{\meta{*}} & \small{One asterisk for black and white implicants, non for colorized implicants} \\
147 % \small{\oarg{X size}} & \small{Number of X-axis cells. Default: ''4''} \\
148 % \small{\oarg{Y size}} & \small{Number of Y-axis cells. Default: ''4''} \\
149 % \small{\oarg{Z size}} & \small{Number of X$\times$Y submaps. Default: ''1''} \\
150 % \small{\oarg{X label}} & \small{Label for the X-axis. Default: ''$X_1X_0$''} \\
151 % \small{\oarg{Y label}} & \small{Label for the Y-axis. Default: ''$X_3X_2$''} \\
152 % \small{\oarg{Z label}} & \small{Label for the submaps. Default: ''$X_5X_4$''} \\
153 % \small{\oarg{Submap Seperation Type}} & \small{Whether to seperate the submap by space or by a thick line. Default: 0 (so seperation by space)} \\
154 % \end{tabularx}
155 % \DeleteShortVerb{\|}
156 % \textbf{Example:}
157 %
158 % Four variable karnaugh map, colorized, with X label $X_1X_0$, and Y label $X_3X_2$.
159 % \begin{verbatim}
160 %\begin{cartonaugh}
161 %\end{cartonaugh}
162 %
163 %or
164 %
165 %\begin{cartonaugh}[4][4][1][$X_1X_0$][$X_3X_2$]
166 %\end{cartonaugh}
167 % \end{verbatim}
168 % Six variable karnaugh map, black and white, with X label $ba$, Y label $dc$, and Z label $fe$.
169 % \begin{verbatim}
170 %\begin{cartonaugh}*[4][4][4][$ba$][$dc$][$fe$]
171 %\end{cartonaugh}
172 % \end{verbatim}
173 %
174 % Six variable karnaugh map, black and white, with X label $ba$, Y label $dc$, and Z label $fe$, and the submaps are seperated by a thick line in between.
175 % \begin{verbatim}
176 %\begin{cartonaugh}*[4][4][4][$ba$][$dc$][$fe$][1]
177 %\end{cartonaugh}
178 % \end{verbatim}
179 %
180 % \iffalse
181 % \begin{macrocode}
182 % ^^A ##########################################################################
183 % ^^A ##########################################################################
184 % ^^A #### CODE ####
185 % ^^A ##########################################################################
186 % ^^A ##########################################################################
187 \NewDocumentEnvironment{cartonaugh}{s O{4} O{4} O{1} O{$X_1X_0$} O{$X_3X_2$} O{$X_5X_4$} O{0}} {%
188 \begingroup
189 % store map size {[START]
190 \renewcommand{\@cartonaugh@var@isactive@}{1}%
191 % [END]}
192 % determinate if markings should be color or black and white
193 %
194 % [END]}
195 % test if a matrix template is found or not(aka "\@cartonaugh@local@matrixtemplate@" equals to '0')
196 \begin{tikzpicture}
197 \directlua{init_cartonaught_env(
198 \the\numexpr(#2)\relax,
199 \the\numexpr(#3)\relax,
200 \the\numexpr(#4)\relax,
201 \the\numexpr(#1)\relax,
202 "\luaescapestring{\detokenize{#5}}",
203 "\luaescapestring{\detokenize{#6}}",
204 "\luaescapestring{\detokenize{#7}}",
205 \the\numexpr(#8)\relax
206 )}
207 }{
208 \end{tikzpicture}
209 \endgroup
210 }
211 % ^^A ##########################################################################
212 % ^^A ##########################################################################
213 % ^^A #### /CODE ####
214 % ^^A ##########################################################################
215 % ^^A ##########################################################################
216 % \end{macrocode}
217 % \fi
218 % \end{environment}
219 %
220 % \newpage
221 % \MakeShortVerb{\|}
222 % \subsection{Terms}
223 % \begin{macro}{\autoterms}
224 % The |\autoterms| command fills the remaining unfilled cells of the karnaugh map with the contents of the optional argument.
225 %
226 % \textbf{Usage:}
227 %
228 % \begin{tabularx}{\textwidth}{l X}
229 % \small{|\autoterms|} & \\
230 % \small{\oarg{content}} & \small{Content for the remaining unfilled cells. Default: ''-''}
231 % \end{tabularx}
232 %
233 % \textbf{Example:}
234 %
235 % Fill all remaining unfilled cells with ''-''.
236 % \begin{verbatim}
237 % \DeleteShortVerb{\|}
238 %\begin{cartonaugh}
239 % \autoterms[-]
240 %\end{cartonaugh}
241 % \end{verbatim}
242 %
243 % \iffalse
244 % \begin{macrocode}
245 % ^^A ##########################################################################
246 % ^^A ##########################################################################
247 % ^^A #### CODE ####
248 % ^^A ##########################################################################
249 % ^^A ##########################################################################
250 \DeclareDocumentCommand{\autoterms}{O{-}} {%
251 \@cartonaugh@func@bailoutsideenvironment@{}
252 %
253 \directlua{
254 autoterms("\luaescapestring{\detokenize{#1}}", '-')
255 }
256 }
257 % ^^A ##########################################################################
258 % ^^A ##########################################################################
259 % ^^A #### /CODE ####
260 % ^^A ##########################################################################
261 % ^^A ##########################################################################
262 % \end{macrocode}
263 % \fi
264 %
265 % \end{macro}
266 %
267 % \begin{macro}{\indeterminants}
268 % The |\indeterminants| command fills the specified cells with ''-'' if they aren't already filled. Order of the cell numbers does not matter.
269 %
270 % \textbf{Usage:}
271 %
272 % \begin{tabularx}{\textwidth}{l X}
273 % \small{|\indeterminants|} & \\
274 % \small{\marg{cells}} & \small{Comma separated list of cells to fill with ''-''}
275 % \end{tabularx}
276 %
277 % \textbf{Example:}
278 %
279 % Fill the top left and right cell with ''-''.
280 % \begin{verbatim}
281 %\begin{cartonaugh}
282 % \indeterminants{0,2}
283 %\end{cartonaugh}
284 % \end{verbatim}
285 %
286 % \iffalse
287 % \begin{macrocode}
288 % ^^A ##########################################################################
289 % ^^A ##########################################################################
290 % ^^A #### CODE ####
291 % ^^A ##########################################################################
292 % ^^A ##########################################################################
293 \DeclareDocumentCommand{\indeterminants}{m} {%
294 % bail if outside environment cartonaugh
295 \@cartonaugh@func@bailoutsideenvironment@{}
296 %
297 \directlua{
298 write_to_cell("\luaescapestring{\detokenize{#1}}", '-')
299 }
300 }
301 % ^^A ##########################################################################
302 % ^^A ##########################################################################
303 % ^^A #### /CODE ####
304 % ^^A ##########################################################################
305 % ^^A ##########################################################################
306 % \end{macrocode}
307 % \fi
308 %
309 % \end{macro}
310 %
311 % \begin{macro}{\manualterms}
312 % The |\manualterms| command fills the 0th cell with the first element in the argument, the 1st cell with the second element in the argument, and so on. If any of the cells already is filled, it is left as it was.
313 %
314 % \textbf{Usage:}
315 %
316 % \begin{tabularx}{\textwidth}{l X}
317 % \small{|\manualterms|} & \\
318 % \small{\marg{content}} & \small{Comma separated list of cell contents}
319 % \end{tabularx}
320 %
321 % \textbf{Example:}
322 %
323 % Fill the first four cells with 0, 1, 0, and 1 respectively.
324 % \begin{verbatim}
325 %\begin{cartonaugh}
326 % \manualterms{0,1,0,1}
327 %\end{cartonaugh}
328 % \end{verbatim}
329 %
330 % \iffalse
331 % \begin{macrocode}
332 % ^^A ##########################################################################
333 % ^^A ##########################################################################
334 % ^^A #### CODE ####
335 % ^^A ##########################################################################
336 % ^^A ##########################################################################
337 \DeclareDocumentCommand{\manualterms}{m} {%
338 % bail if outside environment cartonaugh
339 \@cartonaugh@func@bailoutsideenvironment@{}
340 %
341 \directlua{
342 manualterms("\luaescapestring{\detokenize{#1}}")
343 }
344 }
345 % ^^A ##########################################################################
346 % ^^A ##########################################################################
347 % ^^A #### /CODE ####
348 % ^^A ##########################################################################
349 % ^^A ##########################################################################
350 % \end{macrocode}
351 % \fi
352 %
353 % \end{macro}
354 %
355 % \begin{macro}{\maxterms}
356 % The |\maxterms| command fills the specified cells with ''0'' if they aren't already filled. Order of the cell numbers does not matter.
357 %
358 % \textbf{Usage:}
359 %
360 % \begin{tabularx}{\textwidth}{l X}
361 % \small{|\maxterms|} & \\
362 % \small{\marg{cells}} & \small{Comma separated list of cells to fill with ''0''} \\
363 % \end{tabularx}
364 %
365 % \textbf{Example:}
366 %
367 % Fill the top left and right cell with ''0''.
368 % \begin{verbatim}
369 %\begin{cartonaugh}
370 % \maxterms{0,2}
371 %\end{cartonaugh}
372 % \end{verbatim}
373 %
374 % \iffalse
375 % \begin{macrocode}
376 % ^^A ##########################################################################
377 % ^^A ##########################################################################
378 % ^^A #### CODE ####
379 % ^^A ##########################################################################
380 % ^^A ##########################################################################
381 \DeclareDocumentCommand{\maxterms}{m} {%
382 % bail if outside environment cartonaugh
383 \@cartonaugh@func@bailoutsideenvironment@{}
384 %
385 \directlua{
386 write_to_cell("\luaescapestring{\detokenize{#1}}", '0')
387 }
388 }
389 % ^^A ##########################################################################
390 % ^^A ##########################################################################
391 % ^^A #### /CODE ####
392 % ^^A ##########################################################################
393 % ^^A ##########################################################################
394 % \end{macrocode}
395 %\fi
396 %
397 % \end{macro}
398 %
399 % \begin{macro}{\minterms}
400 % The |\minterms| command fills the specified cells with ''1'' if they aren't already filled. Order of the cell numbers does not matter.
401 %
402 % \textbf{Usage:}
403 %
404 % \begin{tabularx}{\textwidth}{l X}
405 % \small{|\minterms|} & \\
406 % \small{\marg{cells}} & \small{Comma separated list of cells to fill with ''1''} \\
407 % \end{tabularx}
408 %
409 % \textbf{Example:}
410 %
411 % Fill the top left and right cell with ''1''.
412 % \begin{verbatim}
413 %\begin{cartonaugh}
414 % \minterms{0,2}
415 %\end{cartonaugh}
416 % \end{verbatim}
417 %
418 % \iffalse
419 % \begin{macrocode}
420 % ^^A ##########################################################################
421 % ^^A ##########################################################################
422 % ^^A #### CODE ####
423 % ^^A ##########################################################################
424 % ^^A ##########################################################################
425 \DeclareDocumentCommand{\minterms}{m} {%
426 % bail if outside environment cartonaugh
427 \@cartonaugh@func@bailoutsideenvironment@{}
428 %
429 \directlua{
430 write_to_cell("\luaescapestring{\detokenize{#1}}", '1')
431 }
432 }
433 % ^^A ##########################################################################
434 % ^^A ##########################################################################
435 % ^^A #### /CODE ####
436 % ^^A ##########################################################################
437 % ^^A ##########################################################################
438 % \end{macrocode}
439 %\fi
440 %
441 % \end{macro}
442 %
443 % \begin{macro}{\terms}
444 % The |\terms| command fills the specified cells with the specified content if they aren't already filled. Order of the cell numbers does not matter.
445 %
446 % \textbf{Usage:}
447 %
448 % \begin{tabularx}{\textwidth}{l X}
449 % \small{|\terms|} & \\
450 % \small{\marg{cells}} & \small{Comma separated list of cells to fill with content} \\
451 % \small{\marg{content}} & \small{Content to fill the cells with} \\
452 % \end{tabularx}
453 %
454 % \textbf{Example:}
455 %
456 % Fill the top left and right cell with ''X''.
457 % \begin{verbatim}
458 %\begin{cartonaugh}
459 % \terms{0,2}{X}
460 %\end{cartonaugh}
461 % \end{verbatim}
462 %
463 % \iffalse
464 % \begin{macrocode}
465 % ^^A ##########################################################################
466 % ^^A ##########################################################################
467 % ^^A #### CODE ####
468 % ^^A ##########################################################################
469 % ^^A ##########################################################################
470 \DeclareDocumentCommand{\terms}{m m} {%
471 % bail if outside environment cartonaugh
472 \@cartonaugh@func@bailoutsideenvironment@{}
473 %
474 \directlua{
475 write_to_cell("\luaescapestring{\detokenize{#1}}",
476 "\luaescapestring{\detokenize{#2}}")
477 }
478 }
479 % ^^A ##########################################################################
480 % ^^A ##########################################################################
481 % ^^A #### /CODE ####
482 % ^^A ##########################################################################
483 % ^^A ##########################################################################
484 % \end{macrocode}
485 %\fi
486 %
487 % \end{macro}
488 %
489 % \newpage
490 % \MakeShortVerb{\|}
491 % \subsection{Implicants}
492 % \begin{macro}{\implicant}
493 % The |\implicant| command draws quadratic implicants on one or multiple submaps. If the implicant shall be drawn on multiple submaps, \marg{northwest cell} and \marg{southeast cell} must be specified as if the implicant was to be drawn on the 0:th submap. When turned on, colorization is done automatically, following a global sequence of available colors.
494 %
495 % \textbf{Usage:}
496 %
497 % \begin{tabularx}{\textwidth}{l X}
498 % \small{|\implicant|} & \\
499 % \small{\marg{northwest cell}} & \small{The most northwest cell in the implicant} \\
500 % \small{\marg{southeast cell}} & \small{The most southeast cell in the implicant} \\
501 % \small{\oarg{submaps}} & \small{Comma separated list of submaps the implicant should be drawn on. Default: ''0''} \\
502 % \end{tabularx}
503 %
504 % \textbf{Example:}
505 % \DeleteShortVerb{\|}
506 % \begin{multicols}{2}
507 % [Implicant around the four most inner cells.]
508 % \begin{verbatim}
509 %\begin{cartonaugh}
510 % \implicant{5}{15}
511 %\end{cartonaugh}
512 % \end{verbatim}
513 % \columnbreak
514 % \resizebox{\columnwidth}{!}{
515 % \begin{cartonaugh}
516 % \implicant{5}{15}
517 % \end{cartonaugh}
518 % }
519 % \end{multicols}
520 % \begin{multicols}{2}
521 % [Single cell implicant, 0:th cell, on all four submaps.]
522 % \begin{verbatim}
523 %\begin{cartonaugh}[4][4][4]
524 % [$BA$][$DC$][$FE$][1]
525 % \implicant{0}{0}[0,1,2,3]
526 %\end{cartonaugh}
527 % \end{verbatim}
528 % \columnbreak
529 % \resizebox{\columnwidth}{!}{
530 % \begin{cartonaugh}[4][4][4][$BA$][$DC$][$FE$][1]
531 % \implicant{0}{0}[0,1,2,3]
532 % \end{cartonaugh}
533 % }
534 % \end{multicols}
535 %
536 % \iffalse
537 % \begin{macrocode}
538 % ^^A ##########################################################################
539 % ^^A ##########################################################################
540 % ^^A #### CODE ####
541 % ^^A ##########################################################################
542 % ^^A ##########################################################################
543 \DeclareDocumentCommand{\implicant}{m m O{0}} {%
544 % bail if outside environment cartonaugh
545 \@cartonaugh@func@bailoutsideenvironment@{}
546 %
547 \directlua{
548 manual_draw_implicant(
549 "\luaescapestring{\detokenize{#1}}",
550 "\luaescapestring{\detokenize{#2}}",
551 "\luaescapestring{\detokenize{#3}}"
552 )
553 }
554 }
555 % ^^A ##########################################################################
556 % ^^A ##########################################################################
557 % ^^A #### /CODE ####
558 % ^^A ##########################################################################
559 % ^^A ##########################################################################
560 % \end{macrocode}
561 %\fi
562 %
563 % \end{macro}
564 %
565 % \MakeShortVerb{\|}
566 % \begin{macro}{\implicantedge}
567 % The |\implicantedge| command draws quadratic implicants with the middle of the implicant facing the edge of a submap either horizontally or vertically. The function is able to draw the same implicant on one or multiple submaps. However if the implicant shall be drawn on multiple submaps, \marg{northwest part - northwest cell}, \marg{northwest part - southeast cell}, \marg{southeast part - northwest cell}, \marg{southeast part - southeast cell} must be specified as if the implicant was to be drawn on the 0:th submap. When turned on, colorization is done automatically, following a global sequence of available colors.
568 %
569 % \textbf{Usage:}
570 %
571 % \begin{tabularx}{\textwidth}{l X}
572 % \small{|\implicantedge|} & \\
573 % \small{\marg{northwest part - northwest cell}} & \small{The most northwest cell in the northwest part of the implicant} \\
574 % \small{\marg{northwest part - southeast cell}} & \small{The most southeast cell in the northwest part of the implicant} \\
575 % \small{\marg{southeast part - northwest cell}} & \small{The most northwest cell in the southeast part of the implicant} \\
576 % \small{\marg{southeast part - southeast cell}} & \small{The most southeast cell in the southeast part of the implicant} \\
577 % \small{\oarg{submaps}} & \small{Comma separated list of submaps the implicant should be drawn on. Default: ''0''} \\
578 % \end{tabularx}
579 %
580 % \textbf{Example:}
581 % \DeleteShortVerb{\|}
582 %
583 % \begin{multicols}{2}
584 % [Horizontal implicant over the submap edge containing the cells 4, 6, 12, and 14.]
585 % \begin{verbatim}
586 %\begin{cartonaugh}
587 % \implicantedge{4}{12}{6}{14}
588 %\end{cartonaugh}
589 % \end{verbatim}
590 % \columnbreak
591 % \resizebox{\columnwidth}{!}{
592 % \begin{cartonaugh}
593 % \implicantedge{4}{12}{6}{14}
594 % \end{cartonaugh}
595 % }
596 % \end{multicols}
597 %
598 % \iffalse
599 % \begin{macrocode}
600 % ^^A ##########################################################################
601 % ^^A ##########################################################################
602 % ^^A #### CODE ####
603 % ^^A ##########################################################################
604 % ^^A ##########################################################################
605 \DeclareDocumentCommand{\implicantedge}{m m m m O{0}} {%
606 % bail if outside environment cartonaugh
607 \@cartonaugh@func@bailoutsideenvironment@{}
608 %
609 \directlua{
610 manual_draw_edge_implicant(
611 "\luaescapestring{\detokenize{#1}}",
612 "\luaescapestring{\detokenize{#2}}",
613 "\luaescapestring{\detokenize{#3}}",
614 "\luaescapestring{\detokenize{#4}}",
615 "\luaescapestring{\detokenize{#5}}"
616 )
617 }
618 }
619 % ^^A ##########################################################################
620 % ^^A ##########################################################################
621 % ^^A #### /CODE ####
622 % ^^A ##########################################################################
623 % ^^A ##########################################################################
624 % \end{macrocode}
625 %\fi
626 %
627 % \end{macro}
628 % \MakeShortVerb{\|}
629 % \begin{macro}{\implicantcorner}
630 % The |\implicantcorner| command draws an implicant around only the four corner pieces on one or multiple four variable karnaugh submaps. When turned on, colorization is done automatically, following a global sequence of available colors.
631 %
632 % \textbf{Usage:}
633 %
634 % \begin{tabularx}{\textwidth}{l X}
635 % \small{|\implicantcorner|} & \\
636 % \small{\oarg{submaps}} & \small{Comma separated list of submaps the implicant should be drawn on. Default: ''0''} \\
637 % \end{tabularx}
638 %
639 % \textbf{Example:}
640 % \DeleteShortVerb{\|}
641 %
642 % \begin{multicols}{2}
643 % [Draw an implicant around all corners on 0th and 2nd submap of a six variable karnaugh map.]
644 % \begin{verbatim}
645 %\begin{cartonaugh}[4][4][4]
646 % \implicantcorner[0,2]
647 %\end{cartonaugh}
648 % \end{verbatim}
649 % \columnbreak
650 % \resizebox{\columnwidth}{!}{
651 % \begin{cartonaugh}[4][4][4]
652 % \implicantcorner[0,2]
653 % \end{cartonaugh}
654 % }
655 % \end{multicols}
656 %
657 % \iffalse
658 % \begin{macrocode}
659 % ^^A ##########################################################################
660 % ^^A ##########################################################################
661 % ^^A #### CODE ####
662 % ^^A ##########################################################################
663 % ^^A ##########################################################################
664 \DeclareDocumentCommand{\implicantcorner}{O{0}} {
665 % bail if outside environment cartonaugh
666 \@cartonaugh@func@bailoutsideenvironment@{}
667 %
668 \directlua{
669 manual_draw_corner_implicant(
670 "\luaescapestring{\detokenize{#1}}"
671 )
672 }
673 }
674 % ^^A ##########################################################################
675 % ^^A ##########################################################################
676 % ^^A #### /CODE ####
677 % ^^A ##########################################################################
678 % ^^A ##########################################################################
679 % \end{macrocode}
680 %\fi
681 %
682 % \end{macro}
683 %
684 % \newpage
685 % \MakeShortVerb{\|}
686 % \subsection{Options}
687 % \begin{macro}{\implicantspread}
688 % The |\implicantspread| changed the implicant spread
689 %
690 % \textbf{Usage:}
691 %
692 % \begin{tabularx}{\textwidth}{l X}
693 % \small{|\implicantspread|} & \\
694 % \small{\marg{innerspread}} & \small{The inner spread's spread, from 0 to 0.5 (as >0.5 will go out of the implicant square)} \\
695 % \small{\marg{outerspread}} & \small{The outer spread's spread, from 0.5 and up} \\
696 % \end{tabularx}
697 %
698 % \textbf{Example:}
699 % \DeleteShortVerb{\|}
700 %
701 % \begin{multicols}{2}
702 % \begin{verbatim}
703 % \begin{cartonaugh}[4][4][2]
704 % \implicantedge{4}{12}{6}{14}[0]
705 % \implicantspread{0.25}{0.7}
706 % \implicantedge{4}{12}{6}{14}[1]
707 % \end{cartonaugh}
708 % \end{verbatim}
709 % \columnbreak
710 % \resizebox{\columnwidth}{!}{
711 % \begin{cartonaugh}[4][4][2]
712 % \implicantedge{4}{12}{6}{14}[0]
713 % \implicantspread{0.25}{0.7}
714 % \implicantedge{4}{12}{6}{14}[1]
715 % \end{cartonaugh}
716 % }
717 % \end{multicols}
718 %
719 % \iffalse
720 % \begin{macrocode}
721 % ^^A ##########################################################################
722 % ^^A ##########################################################################
723 % ^^A #### CODE ####
724 % ^^A ##########################################################################
725 % ^^A ##########################################################################
726 \DeclareDocumentCommand{\implicantspread}{m m} {
727 % bail if outside environment cartonaugh
728 \@cartonaugh@func@bailoutsideenvironment@{}
729 %
730 \directlua{
731 change_implicant_inner_spread(\luaescapestring{\detokenize{#1}})
732 change_implicant_outer_spread(\luaescapestring{\detokenize{#2}})
733 }
734 }
735 % ^^A ##########################################################################
736 % ^^A ##########################################################################
737 % ^^A #### /CODE ####
738 % ^^A ##########################################################################
739 % ^^A ##########################################################################
740 % \end{macrocode}
741 %\fi
742 %
743 % \end{macro}
744 %
745 % \MakeShortVerb{\|}
746 % \begin{macro}{\resetimplicantspread}
747 % The |\resetimplicantspread| resets the implicant spread if set by |\implicantspread| to default values
748 % \DeleteShortVerb{\|}
749 % \iffalse
750 % \begin{macrocode}
751 % ^^A ##########################################################################
752 % ^^A ##########################################################################
753 % ^^A #### CODE ####
754 % ^^A ##########################################################################
755 % ^^A ##########################################################################
756 \DeclareDocumentCommand{\resetimplicantspread}{} {
757 % bail if outside environment cartonaugh
758 \@cartonaugh@func@bailoutsideenvironment@{}
759 %
760 \directlua{
761 change_implicant_inner_spread(0.35)
762 change_implicant_outer_spread(0.55)
763 }
764 }
765 % ^^A ##########################################################################
766 % ^^A ##########################################################################
767 % ^^A #### /CODE ####
768 % ^^A ##########################################################################
769 % ^^A ##########################################################################
770 % \end{macrocode}
771 %\fi
772 %
773 % \end{macro}
774 % \MakeShortVerb{\|}
775 % \begin{macro}{\changecolor}
776 % The |\changecolor| changes the color for implicants
777 %
778 % \textbf{Usage:}
779 %
780 % \begin{tabularx}{\textwidth}{l X}
781 % \small{|\changecolor|} & \\
782 % \small{\marg{color}} & \small{The new color for implicants, in Tikz format} \\
783 % \end{tabularx}
784 % \DeleteShortVerb{\|}
785 % \textbf{Example:}
786 %
787 % \begin{multicols}{2}
788 % [Change the implicant color to pink, then green]
789 % \begin{verbatim}
790 % \begin{cartonaugh}
791 % \changecolor{pink}
792 % \implicant{0}{1}
793 % \changecolor{green}
794 % \implicant{8}{10}
795 % \end{cartonaugh}
796 % \end{verbatim}
797 % \columnbreak
798 % \resizebox{\columnwidth}{!}{
799 % \begin{cartonaugh}
800 % \changecolor{pink}
801 % \implicant{0}{1}
802 % \changecolor{green}
803 % \implicant{8}{10}
804 % \end{cartonaugh}
805 % }
806 % \end{multicols}
807 %
808 %
809 % \iffalse
810 % \begin{macrocode}
811 % ^^A ##########################################################################
812 % ^^A ##########################################################################
813 % ^^A #### CODE ####
814 % ^^A ##########################################################################
815 % ^^A ##########################################################################
816 \DeclareDocumentCommand{\changecolor}{m} {
817 % bail if outside environment cartonaugh
818 \@cartonaugh@func@bailoutsideenvironment@{}
819 %
820 \directlua{
821 customColor("\luaescapestring{\detokenize{#1}}")
822 }
823 }
824 % ^^A ##########################################################################
825 % ^^A ##########################################################################
826 % ^^A #### /CODE ####
827 % ^^A ##########################################################################
828 % ^^A ##########################################################################
829 % \end{macrocode}
830 %\fi
831 %
832 % \end{macro}
833 %
834 % \newpage
835 % \section{Examples}
836 % \begin{multicols}{2}
837 % [Draw a karnaugh map for \small{$f(a,b,c,d,e,f) =$\\$\Sigma(0,1,2,3,8,13,17,20,22,28,33,32,30,19,40,35,49,42,34,10,60,54,62,51,52)$\\$+d(15,45,47)$}.]
838 % \begin{verbatim}
839 %\begin{cartonaugh}[4][4][4][$ba$][$dc$][$fe$][1]
840 % \minterms{0,1,2,3,8,13,17,20,22,28,
841 % 33,32,30,19,40,35,49,42,34,10,60,
842 % 54,62,51,52}
843 % \indeterminants{15,45,47}
844 % \autoterms[0]
845 % \implicantcorner[0,2]
846 % \implicant{1}{3}[0,1,2,3]
847 % \implicantedge{4}{12}{6}{14}[1,3]
848 % \implicant{13}{15}[0,2]
849 %\end{cartonaugh}
850 % \end{verbatim}
851 % \columnbreak
852 % \resizebox{\columnwidth}{!}{
853 % \begin{cartonaugh}[4][4][4][$ba$][$dc$][$fe$][1]
854 % \minterms{0,1,2,3,8,13,17,20,22,28,33,32,30,19,40,35,49,42,34,10,60,54,62,51,52}
855 % \indeterminants{15,45,47}
856 % \autoterms[0]
857 % \implicantcorner[0,2]
858 % \implicant{1}{3}[0,1,2,3]
859 % \implicantedge{4}{12}{6}{14}[1,3]
860 % \implicant{13}{15}[0,2]
861 % \end{cartonaugh}
862 % }
863 % \end{multicols}
864 %
865 % \begin{multicols}{2}
866 % [Draw a karnaugh map for \small{$f(X_0,X_1) = \Pi(0,2,3)$} in black and white.]
867 % \begin{verbatim}
868 %\begin{cartonaugh}*[2][2][1][$X_0$][$X_1$]
869 % \maxterms{0,2,3}
870 % \autoterms[1]
871 % \implicant{1}{1}
872 %\end{cartonaugh}
873 % \end{verbatim}
874 % \columnbreak
875 % \resizebox{\columnwidth}{!}{
876 % \begin{cartonaugh}*[2][2][1][$X_0$][$X_1$]
877 % \maxterms{0,2,3}
878 % \autoterms[1]
879 % \implicant{1}{1}
880 % \end{cartonaugh}
881 % }
882 % \end{multicols}
883 %
884 % \newpage
885 % \begin{multicols}{2}
886 % [Draw a variable entered map.]
887 % \begin{verbatim}
888 %\begin{cartonaugh}[4][2][1][$ab$][$c$]
889 % \maxterms{0,2,4,5,6}
890 % \minterms{3}
891 % \terms{1}{$d$}
892 % \terms{7}{$d'$}
893 % \implicant{1}{3}
894 % \implicant{3}{7}
895 %\end{cartonaugh}
896 % \end{verbatim}
897 % \columnbreak
898 % \resizebox{\columnwidth}{!}{
899 % \begin{cartonaugh}[4][2][1][$ab$][$c$]
900 % \maxterms{0,2,4,5,6}
901 % \minterms{3}
902 % \terms{1}{$d$}
903 % \terms{7}{$d'$}
904 % \implicant{1}{3}
905 % \implicant{3}{7}
906 % \end{cartonaugh}
907 % }
908 % \end{multicols}
909 %
910 % \section{Dependencies}
911 % \begin{itemize}
912 % \item tikz
913 % \item xparse
914 % \item xstring
915 % \item lualatex
916 % \end{itemize}
917 %
918 % \newpage
919 % \section{Miscellaneous}
920 % \MakeShortVerb{\|}
921 % \subsection*{Resizing}
922 % The karnaugh maps produced with this package have a prespecified size which can not be changed. However you can resize the karnaugh map to your desired size. Resizing can be done using the |\resizebox| command from the graphicx package. Scaling the karnaugh map to fill the column width while preserving the aspect ratio can be done as follows.
923 % \begin{verbatim}
924 %\resizebox{\columnwidth}{!}{
925 % \DeleteShortVerb{\|}
926 % \begin{cartonaugh}
927 % \end{cartonaugh}
928 %}
929 % \end{verbatim}
930 %
931 % \subsection*{Comma separated lists}
932 % Anywhere in this package where a comma separated list is used data should only be comma separated. Therefore a comma and space separeated list will for example \textit{not} work properly.
933 %
934 % An example of errorious usage related to the \small{\marg{cells}} parameter in the terms related commands can result in multiple zeros, ones and other terms overlapping in the same cell in the outputted karnaugh map.
935 %
936 %\iffalse
937 %</package>
938 %<*luacode>
939 %
940 %
941 % This is the Luacode that is generated and put into cartonaugh.lua
942 %
943 %
944 %\begin{macrocode}
945 DEBUG_FLAG = 0
946
947 CARTONAUGH_COLORS = {'red', 'green', 'yellow', 'cyan', 'blue', 'magenta'}
948 CARTONAUGH_DEFAULT_COLOR = 'cyan'
949
950 cartonaugh_env_settings = {}
951 used_cells = {}
952
953 -- Function that is used to either print to LaTeX or to the console
954 -- Used for debugging mode
955 function localPrint(str)
956 if DEBUG_FLAG==1 then
957 print(str)
958 end
959 tex.sprint(str)
960 end
961
962 -- Function that takes a string and splits it be delimiter, then returns it back
963 -- From https://stackoverflow.com/questions/19262761/lua-need-to-split-at-comma
964 function split(source, delimiters)
965 local elements = {}
966 local pattern = '([^'..delimiters..']+)'
967 string.gsub(source, pattern, function(value) elements[#elements + 1] = value; end);
968 return elements
969 end
970
971 -- Function that converts a decimal number to binary
972 function decimalToBin(num, numb_bits, return_concat)
973 if return_concat == nil then return_concat=true end
974 num = tonumber(num)
975 numb_bits = tonumber(numb_bits)
976 local t={}
977 for b=numb_bits,1,-1 do
978 rest=math.floor(math.fmod(num,2))
979 t[b]=rest
980 num=(num-rest)/2
981 end
982 if return_concat == true then
983 return table.concat(t)
984 else
985 return t
986 end
987 end
988
989 -- Function that converts a decimal number to grey code
990 function decimalToGreyBin(num, numb_bits)
991 -- Get the binary array
992 num = num ~ (num >> 1)
993 local t = decimalToBin(num, numb_bits, false)
994 return table.concat(t)
995 end
996
997 function greyBinToDecimal(num)
998 -- num = tonumber(num,2)
999 local mask = num
1000 while mask > 0 do
1001 mask = mask >> 1
1002 num = num ~ mask
1003 end
1004 return tonumber(num)
1005 end
1006
1007 -- Function to pad a string by a amount with b string
1008 function padString(str, pad_to, pad_with)
1009 local ret = str
1010 for l=1, (pad_to-string.len(str)) do
1011 ret = pad_with .. ret
1012 end
1013 return ret
1014 end
1015
1016 function getColor(index)
1017 if index <= 0 then return CARTONAUGH_DEFAULT_COLOR end
1018 if CARTONAUGH_COLORS[index] ~= nil then
1019 return CARTONAUGH_COLORS[index]
1020 else
1021 return CARTONAUGH_DEFAULT_COLOR
1022 end
1023 end
1024
1025 function customColor(color)
1026 -- Some high number so it's not in the CARTONAUGH_COLORS array
1027 cartonaugh_env_settings.color_index = 100
1028 CARTONAUGH_DEFAULT_COLOR = color
1029 end
1030
1031 -- Function to generate a kmap template
1032 function generateKMap(column, row, grid_numb)
1033 local outside_column_bits = 1
1034 local outside_row_bits = 1
1035 local outside_grid_numb_bits = 2
1036 local return_str = ''
1037 if row >= 4 then
1038 outside_row_bits = 2
1039 end
1040 if column >= 4 then
1041 outside_column_bits = 2
1042 end
1043 for c=1,column,1 do
1044 for r=1,row,1 do
1045 if r == 0 then
1046 if c == 0 then
1047 -- Do Nothing...
1048 elseif c == (column+1) then
1049 return_str = return_str .. ("\\phantom{" .. decimalToBin(0, outside_column_bits) .. "}")
1050 else
1051 return_str = return_str .. (decimalToGreyBin(c-1, outside_column_bits))
1052 end
1053 elseif r==(row+1) then
1054 if c==0 then
1055 return_str = return_str .. ("\\phantom{" .. decimalToBin(0, outside_column_bits) .. "}")
1056 end
1057 else
1058 if c == 0 then
1059 return_str = return_str .. (decimalToGreyBin(r-1, outside_row_bits))
1060 elseif c == (column+1) then
1061
1062 else
1063 return_str = return_str ..("|(" .. padString((decimalToGreyBin(grid_numb, outside_grid_numb_bits) .. decimalToGreyBin(c-1, outside_column_bits) .. decimalToGreyBin(r-1, outside_row_bits)), 6, 0) .. ")|" .. "\\phantom{0}")
1064 --TODO: Look into why reversing c and r from where they should be makes it work
1065 end
1066 end
1067
1068 if r == (row) then
1069 return_str = return_str .. ("\\\\")
1070 else
1071 return_str = return_str .. ("\\&")
1072 end
1073 end
1074 end
1075 return return_str
1076 end
1077
1078 function init_cartonaught_env(numb_cols, numb_row, numb_submaps, is_bw, var12_str, var34_str, var56_str, is_submap_seperated)
1079 -- Change the default texts depending on the number of submaps if not custom name has been given
1080 -- TODO: Get this to actually work
1081 if var12_str == 'X_1X_0' and var34_str == 'X_3X_2' and var56_str == 'X_5X_4' then
1082 if numb_submaps == 1 then
1083 var56_str = 'X_3'
1084 end
1085 if numb_cols == 1 then
1086 var12_str = 'X_0'
1087 end
1088 end
1089 cartonaugh_env_settings = {
1090 cols = numb_cols,
1091 rows = numb_row,
1092 submaps = numb_submaps,
1093 bw = tonumber(is_bw),
1094 var_string = {
1095 v12 = var12_str,
1096 v34 = var34_str,
1097 v56 = var56_str,
1098 },
1099 color_index = 1,
1100 implicant_settings = {
1101 inner_sep = 0.35,
1102 outer_sep = 0.55,
1103 }
1104 }
1105 used_cells = {}
1106
1107 if is_submap_seperated == 0 then
1108 is_submap_seperated = false
1109 else
1110 is_submap_seperated = true
1111 end
1112
1113 draw_pgf_kmap(numb_cols, numb_row, numb_submaps, var12_str, var34_str, var56_str, is_submap_seperated)
1114 end
1115
1116 function change_implicant_inner_spread(new_amount)
1117 cartonaugh_env_settings.implicant_settings.inner_sep = new_amount
1118 end
1119
1120 function change_implicant_outer_spread(new_amount)
1121 cartonaugh_env_settings.implicant_settings.outer_sep = new_amount
1122 end
1123
1124 -- Function to generate the k-maps
1125 -- NOTE: Each variable/cell in the k-map is 1cm. This is so that everything alings with each other just be adding
1126 -- the number of row and column. It's a bit of hack, but for now it will stay this way. Resizing of the matrix
1127 -- will be done with the scale option in the future
1128 function draw_pgf_kmap(column, row, submaps_n, var1, var2, var3, is_submap_seperated)
1129 submaps_n = submaps_n-1
1130 -- TODO: Transform the following settings variables into arguments
1131 local is_multitable_seperated = is_submap_seperated -- Setting to determine if the graphs are drawn with a sperator line or distanced out
1132 local graph_seperator = 1.5 -- Seperation lenght between kmaps if is_multitable_seperated=false
1133 local kmaplevel_seperator_lenght = 0.1 -- Setting to determine the seperator line's thickness if is_multitable_seperated=true
1134 local line_width = 0.015 -- Set the line thickness of things here
1135 local zero_var_line_lenght = 0.75 -- The lenght of the line at the top-left corner of the kmap where the implacants reside
1136 local column_header_numb_bits = ((column-1) // 2)+1
1137 local row_header_numb_bits = ((row-1) // 2)+1
1138 if is_multitable_seperated then graph_seperator = 0 end
1139 for d=0,submaps_n,1 do
1140 -- Find the top-left corner of each grid (seperated by 1 unit)
1141 local grid_x_loc = (d % 2)*(column+graph_seperator)
1142 local grid_y_loc = -(d // 2)*(row+graph_seperator)
1143 -- localPrint(string.format("\\node[above] at (%f,%f) {\\small{%s}};", 0, 0, abimplecant))
1144 if is_multitable_seperated then
1145 if (d % 2) == 1 then
1146 local add_heigh = 0
1147 if d >= 2 then add_heigh = kmaplevel_seperator_lenght end
1148 localPrint(string.format("\\fill[black] (%f,%f) rectangle (%f,%f);", grid_x_loc, grid_y_loc, grid_x_loc+kmaplevel_seperator_lenght, grid_y_loc-row-line_width-add_heigh))
1149 grid_x_loc = grid_x_loc + kmaplevel_seperator_lenght
1150 end
1151 if d >= 2 then
1152 localPrint(string.format("\\fill[black] (%f,%f) rectangle (%f,%f);", grid_x_loc, grid_y_loc, grid_x_loc+column+line_width, grid_y_loc-kmaplevel_seperator_lenght))
1153 grid_y_loc = grid_y_loc - kmaplevel_seperator_lenght
1154 end
1155 end
1156 -- Print out the top-left line corner with the variables
1157 if (is_multitable_seperated == false) or (d==0) then
1158 localPrint(string.format("\\draw[inner sep=0pt, outer sep=0pt] (%f, %f) -- (%f, %f);", grid_x_loc+line_width, grid_y_loc-line_width, grid_x_loc-zero_var_line_lenght, grid_y_loc+zero_var_line_lenght))
1159 localPrint(string.format("\\node[left] at (%f,%f) {\\small{%s}};", grid_x_loc-0.3, grid_y_loc+0.3, var2))
1160 localPrint(string.format("\\node[right] at (%f,%f) {\\small{%s}};", grid_x_loc-0.6, grid_y_loc+0.6, var1))
1161 end
1162 -- Print out the top boolean column header
1163 if (is_multitable_seperated == false) or (d < 2) then
1164 localPrint(string.format("\\matrix[matrix of nodes, ampersand replacement=\\&, column sep={1cm,between origins}, nodes={align=center,text width=1cm,inner sep=0pt}, anchor=south west, inner sep=0pt, outer sep=0pt] at (%f, %f) {",grid_x_loc,grid_y_loc+0.05))
1165 for c=0, column-1, 1 do
1166 localPrint(string.format("%s", decimalToGreyBin(c, column_header_numb_bits)))
1167 if c ~= (column-1) then localPrint("\\&") end
1168 end
1169 localPrint("\\\\};")
1170 end
1171 -- Print out the side boolean row header
1172 if (is_multitable_seperated == false) or (d%2 == 0) then
1173 localPrint(string.format("\\matrix[matrix of nodes, ampersand replacement=\\&, row sep={1cm,between origins}, nodes={minimum height=1cm,inner sep=0pt, text height=2ex, text depth=0.5ex}, anchor=north east, inner sep=0pt, outer sep=0pt] at (%f, %f) {",grid_x_loc-0.05,grid_y_loc))
1174 for r=0, row-1, 1 do
1175 localPrint(string.format("%s \\\\", decimalToGreyBin(r, row_header_numb_bits)))
1176 end
1177 localPrint("};")
1178 end
1179 -- Print out the matrix
1180 localPrint(string.format("\\matrix[matrix of nodes, ampersand replacement=\\&, column sep={1cm,between origins}, row sep={1cm,between origins}, nodes={rectangle,draw,minimum height=1cm,align=center,text width=1cm,inner sep=0pt, text height=2ex, text depth=0.5ex, line width=0.015cm}, anchor=north west, inner sep=0pt, outer sep=0pt] at (%f, %f) {%s};",
1181 grid_x_loc, grid_y_loc, generateKMap(row, column, d)
1182 ))
1183 -- Print out the buttom text saying which matrix is which
1184 if (submaps_n > 0) then
1185 if (is_multitable_seperated == false) then
1186 localPrint(string.format("\\node[below] at (%f, %f) {%s = %s};", grid_x_loc+(column//2),grid_y_loc-row,var3, decimalToBin(d, 2)))
1187 elseif (is_multitable_seperated == true) then
1188 if (d < 2) then
1189 localPrint(string.format("\\node[] at (%f, %f) {%s = %s};", grid_x_loc+(column//2), grid_y_loc+1, var3, decimalToBin(d, 2)))
1190 end
1191 if (d % 2 == 0) and (submaps_n > 2) then
1192 localPrint(string.format("\\node[rotate=90] at (%f, %f) {%s = %s};", grid_x_loc-1, grid_y_loc-(row//2), var3, decimalToBin(d, 2)))
1193 end
1194 end
1195 end
1196 end
1197 end
1198
1199 -- Function for drawing an implicant manually (meaning to give the start and endpoint, as well as optional
1200 -- submaps for different ones)
1201 function manual_draw_implicant(st, en, submaps_str)
1202 local color_index = cartonaugh_env_settings.color_index
1203 local max_submaps = cartonaugh_env_settings.submaps
1204 local inner_spread = cartonaugh_env_settings.implicant_settings.inner_sep
1205 st = tonumber(st)
1206 en = tonumber(en)
1207 local submap_arr = split(submaps_str, ',')
1208 -- Check if the implacent selection
1209 for s=1,table.getn(submap_arr),1 do
1210 current_submap = tonumber(submap_arr[s])
1211 if current_submap < max_submaps then
1212 local draw_str = string.format("($(%s.center)+(-%s,%s)$) rectangle ($(%s.center)+(%s,-%s)$)", decimalToGreyBin(current_submap, 2) .. decimalToBin(st,4), inner_spread, inner_spread, decimalToGreyBin(current_submap, 2) .. decimalToBin(en,4), inner_spread, inner_spread)
1213 if cartonaugh_env_settings.bw == 0 then
1214 localPrint(string.format("\\fill[rounded corners=3pt,fill=%s,fill opacity=0.25,] {%s};", getColor(color_index) , draw_str))
1215 end
1216 localPrint(string.format("\\draw[rounded corners=3pt,draw opacity=1.0,] {%s};", draw_str))
1217 else
1218 localPrint(string.format("\\PackageWarning{cartonaugh}{You can only draw on existing sub maps. Ignoring instruction to draw on non existing sub map number %d}", s))
1219 end
1220 end
1221 cartonaugh_env_settings.color_index = cartonaugh_env_settings.color_index+1
1222 end
1223
1224 -- Handler function for drawing edge implacants, figuring out orientation as well
1225 function manual_draw_edge_implicant(corner1, corner2, corner3, corner4, submaps_str)
1226 corner1 = tonumber(corner1)
1227 corner2 = tonumber(corner2)
1228 corner3 = tonumber(corner3)
1229 corner4 = tonumber(corner4)
1230
1231 if corner1-corner2 > corner1-corner3 then
1232 manual_draw_edge_implicant_orientation(corner1, corner2, submaps_str, 'n')
1233 manual_draw_edge_implicant_orientation(corner3, corner4, submaps_str, 's')
1234 else
1235 manual_draw_edge_implicant_orientation(corner1, corner2, submaps_str, 'w')
1236 manual_draw_edge_implicant_orientation(corner3, corner4, submaps_str, 'e')
1237 end
1238
1239 cartonaugh_env_settings.color_index = cartonaugh_env_settings.color_index+1
1240
1241 end
1242
1243 -- Function to draw out a 1 edge implacant given 2 corners, the submaps, and the orientation (n, s, e, or w)
1244 -- TODO: Perhaps find a way to repeat the code between n/s and e/w
1245 -- TODO: For the mirror variable, have that apply to the lua var directly instead of having LaTeX handle it
1246 -- TODO: Maybe add option for squigly lines for the end instead of nothing
1247 -- TODO: Open up the internal inner_spread and outer_spead settings to the user
1248 function manual_draw_edge_implicant_orientation(corner1, corner2, submaps_str, orientation)
1249 local color_index = cartonaugh_env_settings.color_index
1250 local max_submaps = cartonaugh_env_settings.submaps
1251 corner1 = tonumber(corner1)
1252 corner2 = tonumber(corner2)
1253 local submap_arr = split(submaps_str, ',')
1254 local inner_spread = cartonaugh_env_settings.implicant_settings.inner_sep
1255 local outer_spead = cartonaugh_env_settings.implicant_settings.outer_sep
1256 -- Check if the implacent selection
1257 for s=1,table.getn(submap_arr),1 do
1258 current_submap = tonumber(submap_arr[s])
1259 if current_submap < max_submaps then
1260 local draw_string = ""
1261 local mirror = 1
1262 local corner1_bin = decimalToGreyBin(current_submap, 2) .. decimalToBin(corner1,4)
1263 local corner2_bin = decimalToGreyBin(current_submap, 2) .. decimalToBin(corner2,4)
1264 if orientation == 'n' or orientation == 's' then
1265 -- If the orientation is south, just mirror it
1266 if orientation == 's' then mirror = -1 end
1267 draw_string = string.format("($(%s.center)+(-%f,%f*%s)$)", corner1_bin, inner_spread, outer_spead, mirror)
1268 draw_string = draw_string .. string.format("{[rounded corners=3pt] -- ($(%s.center)+(-%f,-%f*%s)$)}", corner1_bin, inner_spread, inner_spread, mirror)
1269 draw_string = draw_string .. string.format("{[rounded corners=3pt] -- ($(%s.center)+(%f,-%f*%s)$)}", corner2_bin, inner_spread, inner_spread, mirror)
1270 draw_string = draw_string .. string.format("-- ($(%s.center)+(%f,%f*%s)$)", corner2_bin, inner_spread, outer_spead, mirror)
1271 else
1272 if orientation == 'e' then mirror = -1 end
1273 draw_string = string.format("($(%s.center)+(-%f*%s,%f)$)", corner1_bin, outer_spead, mirror, inner_spread)
1274 draw_string = draw_string .. string.format("{[rounded corners=3pt] -- ($(%s.center)+(%f*%s,%f)$)}", corner1_bin, inner_spread, mirror, inner_spread)
1275 draw_string = draw_string .. string.format("{[rounded corners=3pt] -- ($(%s.center)+(%f*%s,-%f)$)}", corner2_bin, inner_spread, mirror, inner_spread)
1276 draw_string = draw_string .. string.format("-- ($(%s.center)+(-%f*%s,-%f)$)", corner2_bin, outer_spead, mirror, inner_spread)
1277 end
1278 if cartonaugh_env_settings.bw == 0 then
1279 localPrint(string.format("\\fill[fill=%s,fill opacity=0.25,] {%s};", getColor(color_index), draw_string))
1280 end
1281 localPrint(string.format("\\draw[draw opacity=1.0] {%s};", draw_string))
1282 else
1283 localPrint(string.format("\\PackageWarning{cartonaugh}{You can only draw on existing sub maps. Ignoring instruction to draw on non existing sub map number %d}", s))
1284 end
1285 end
1286 end
1287
1288 -- Function to draw the corner implicants. Only usable on 4x4 matrices
1289 function manual_draw_corner_implicant(submaps_str)
1290 local corner_arr = {0, 2, 8, 10}
1291 local submap_arr = split(submaps_str, ',')
1292 local max_submaps = cartonaugh_env_settings.submaps
1293 local color = cartonaugh_env_settings.color_index
1294 local inner_spread = cartonaugh_env_settings.implicant_settings.inner_sep
1295 local outer_spread = cartonaugh_env_settings.implicant_settings.outer_sep
1296
1297 if cartonaugh_env_settings.cols ~= 4 or cartonaugh_env_settings.rows ~= 4 then
1298 localPrint("\\PackageError{cartonaugh}{Cannot use a corner implicant on anything but a 4x4. Sorry!}")
1299 return
1300 end
1301
1302 for s=1,table.getn(submap_arr),1 do
1303 current_submap = tonumber(submap_arr[s])
1304 if current_submap < max_submaps then
1305 for c=0,3,1 do
1306 local x_mirror = 1-(2*(c%2))
1307 local y_mirror = 1-(2*(c//2))
1308 local corner = decimalToGreyBin(current_submap, 2) .. decimalToBin(corner_arr[c+1],4)
1309 -- Create the string to draw the corners
1310 local draw_string = string.format("($(%s.center)+(-%f*%d,%f*%d)$)--($(%s.center)+(%f*%d,%f*%d)$)", corner, outer_spread, x_mirror, outer_spread, y_mirror, corner, inner_spread, x_mirror, outer_spread, y_mirror)
1311 draw_string = draw_string .. string.format("{ [rounded corners=3pt] --($(%s.center)+(%f*%d,-%f*%d)$) }", corner, inner_spread, x_mirror, inner_spread, y_mirror)
1312 draw_string = draw_string .. string.format("--($(%s.center)+(-%f*%d,-%f*%d)$) -- cycle", corner, outer_spread, x_mirror, inner_spread, y_mirror)
1313 if cartonaugh_env_settings.bw == 0 then
1314 localPrint(string.format("\\fill[fill=%s,fill opacity=0.25,] {%s};", getColor(color), draw_string))
1315 end
1316 localPrint(string.format("\\draw[sharp corners, draw opacity=1.0] {%s};", draw_string))
1317 end
1318 else
1319 localPrint(string.format("\\PackageWarning{cartonaugh}{You can only draw on existing sub maps. Ignoring instruction to draw on non existing sub map number %d}", s))
1320 end
1321 end
1322
1323 cartonaugh_env_settings.color_index = cartonaugh_env_settings.color_index+1
1324
1325 end
1326
1327 -- WORK IN PROGRESS/LONG TERM FUNCTION
1328 -- Goals is to eventually give \implicant{1}{x}{0}{x} for example and have it draw it out for you.
1329 -- May give up on this in favor of other things...don't know
1330 function draw_implicant(var_list)
1331 -- local var_list_arr = split(var_list, ',')
1332 local color_index = cartonaugh_env_settings.color_index
1333 local max_submaps = cartonaugh_env_settings.submaps
1334 localPrint("\\PackageWarning{cartonaugh}{This is a UNSTABLE and WIP function. Procede on your own}")
1335
1336 -- Check argument for submaps greater than 1
1337 -- TODO: before returning print out a package error
1338 if max_submaps > 1 then
1339 for s=0,max_submaps-1,1 do
1340 if var_list[5+s] == '' then
1341 -- TODO: Fix this
1342 localPrint(string.format("\\PackageError{cartonaugh}{Please feed either 1, 0, or x for sub map number %d's variable boolean}", s+1))
1343 return
1344 end
1345 end
1346 end
1347
1348 local low_limit = 0
1349 local high_limit = 0
1350
1351 for b=1,4,1 do
1352 if var_list[b] == 'x' then
1353 print('a',high_limit, greyBinToDecimal(high_limit), decimalToBin(greyBinToDecimal(high_limit), 4))
1354 print('b',high_limit | (1 << (b-1)), greyBinToDecimal(high_limit | (1 << (b-1))), decimalToBin(greyBinToDecimal(high_limit | (1 << (b-1))), 4))
1355 if greyBinToDecimal(high_limit) < greyBinToDecimal(high_limit | (1 << (b-1))) then
1356 high_limit = high_limit | (1 << (b-1))
1357 print(decimalToBin(greyBinToDecimal(high_limit),4), decimalToBin(greyBinToDecimal(high_limit & ~(1 << (b-2))),4))
1358 if greyBinToDecimal(high_limit) < greyBinToDecimal(high_limit & ~(1 << (b-2))) then
1359 high_limit = high_limit & ~(1 << (b-2))
1360 end
1361 end
1362 elseif var_list[b] == '1' then
1363 high_limit = high_limit | (1 << (b-1))
1364 low_limit = low_limit | (1 << (b-1))
1365 end
1366 print('low_limit=', low_limit, 'high_limit=', high_limit)
1367 end
1368
1369 local st = decimalToBin(low_limit,4)
1370 local en = decimalToBin(high_limit,4)
1371
1372 print('low_limit', greyBinToDecimal(low_limit), 'high_limit', greyBinToDecimal(high_limit), 'st=', st, 'en=', en)
1373
1374 for s=1,1,1 do
1375 current_submap = 0
1376 if current_submap < max_submaps then
1377 if cartonaugh_env_settings.bw == 0 then
1378 localPrint(string.format("\\fill[rounded corners=3pt,fill=%s,fill opacity=0.25,] {($(%s.center)+(-0.3,0.3)$) rectangle ($(%s.center)+(0.3,-0.3)$)};", getColor(color_index) , decimalToGreyBin(current_submap, 2) .. st, decimalToGreyBin(current_submap, 2) .. en))
1379 color_index = color_index+1
1380 end
1381 localPrint(string.format("\\draw[rounded corners=3pt,draw opacity=1.0,] {($(%s.center)+(-0.3,0.3)$)rectangle($(%s.center)+(0.3,-0.3)$)};", decimalToGreyBin(current_submap, 2) .. st, decimalToGreyBin(current_submap, 2) .. en))
1382 else
1383 localPrint(string.format("\\PackageWarning{cartonaugh}{You can only draw on existing sub maps. Ignoring instruction to draw on non existing sub map number %d}", s))
1384 end
1385 end
1386 cartonaugh_env_settings.color_index = color_index
1387 end
1388
1389 function autoterms(what_to_write)
1390 local max_cells = cartonaugh_env_settings.submaps * cartonaugh_env_settings.cols * cartonaugh_env_settings.rows
1391 for cell=0,max_cells-1,1 do
1392 if used_cells[cell] == nil then
1393 used_cells[cell] = true
1394 localPrint(string.format("\\path (%s) node {%s};", decimalToBin(cell, 6), what_to_write))
1395 end
1396 end
1397 end
1398
1399 function manualterms(what_to_write)
1400 local what_to_write_arr = split(what_to_write, ',')
1401 for cell=0,table.getn(what_to_write_arr)-1,1 do
1402 if used_cells[cell] == nil then
1403 used_cells[cell] = true
1404 localPrint(string.format("\\path (%s) node {%s};", decimalToBin(cell, 6), what_to_write_arr[cell+1]))
1405 end
1406 end
1407 end
1408
1409 function write_to_cell(cells, what_to_write)
1410 local cells_arr = split(cells, ',')
1411 for c=1,table.getn(cells_arr),1 do
1412 local cell = tonumber(cells_arr[c])
1413 if used_cells[cell] == nil then
1414 used_cells[cell] = true
1415 localPrint(string.format("\\path (%s) node {%s};", decimalToBin(cell, 6), what_to_write))
1416 end
1417 end
1418 end
1419 %\end{macrocode}
1420 %</luacode>
1421 %\fi
1422 %
1423 \endinput