Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 % This is kvmap, (c) 2020 Ben Frank
3 %
4 % It may be distributed and/or modified under the conditions of the
5 % LaTeX Project Public License (LPPL), either version 1.3c of this
6 % license or (at your option) any later version. The latest version
7 % of this license is in the file
8 %
9 % http://www.latex-project.org/lppl.txt
10 %<*driver>
11 \def\nameofplainTeX{plain}
12 \ifx\fmtname\nameofplainTeX\else
13 \expandafter\begingroup
14 \fi
15 \input l3docstrip.tex
16 \askforoverwritefalse
17 \preamble
18 Copyright (C) 2020 Ben Frank
19
20 It may be distributed and/or modified under the conditions of
21 the LaTeX Project Public License (LPPL), either version 1.3c of
22 this license or (at your option) any later version. The latest
23 version of this license is in the file:
24
25 http://www.latex-project.org/lppl.txt
26
27 \endpreamble
28 \postamble
29 End of file
30 \endpostamble
31 \generate{\file{kvmap.sty}{\from{kvmap.dtx}{pkg,cfg}}}
32 \ifx\fmtname\nameofplainTeX
33 \expandafter\endbatchfile
34 \else
35 \expandafter\endgroup
36 \fi
37 %</driver>
38 %
39 %<*driver>
40 \RequirePackage{scrlfile}
41 \ReplaceClass{article}{scrartcl}
42 \documentclass[DIV=calc,BCOR=18mm,12pt]{l3doc}
43
44 \EnableCrossrefs
45 ^^A\PageIndex
46 ^^A\CodelineNumbered
47 \RecordChanges
48
49 \usepackage[english]{babel}
50
51 \usepackage[svgnames]{xcolor}
52 \usepackage{graphicx}
53 \usepackage{tcolorbox}
54 \tcbuselibrary{minted,listings,documentation,breakable}
55 \tcbset{listing engine=minted,minted language=latex,
56 documentation minted style=vs,documentation minted options={fontsize=\small},
57 color command=blue,color definition=blue,doc marginnote={halign=center,
58 colframe=DarkSlateGray!50!white,colback=Silver!20!white}, index colorize=true,
59 index annotate=true,index format=doc,
60 doclang/new={\textcolor{DarkGreen}{\textbf{\textsf{N}}}},
61 doclang/updated={\textcolor{DarkGreen}{\textbf{\textsf{U}}}},
62 minted options={linenos,xleftmargin=8.75pt,breaklines=true,
63 breakanywhere,fontsize=\small,escapeinside=§§,autogobble=false}}
64 \tcbset{enhanced,sharp corners=all}
65 \usemintedstyle{vs}
66
67 ^^A quick fix for tcolorbox's broken documentation library
68 ^^A by Skillmon, see https://chat.stackexchange.com/transcript/message/55558127#55558127
69 \makeatletter
70 \let\tcb@doc@index@doc\relax
71 \makeatother
72
73 \usepackage{fontspec}
74 \usepackage{unicode-math}
75 \setmainfont[Numbers={OldStyle}]{Libertinus Serif}
76 \setsansfont[Scale=MatchUppercase,Numbers={OldStyle}]{Libertinus Sans}
77 \setmonofont[Scale=MatchLowercase,Numbers={OldStyle}]{Fira Mono}
78 \setmathfont[Scale=MatchLowercase]{Libertinus Math}
79 \setmathfont[Scale=MatchLowercase,range=\setminus]{XITS Math}
80 \usepackage{hologo}
81 \recalctypearea
82
83 \usepackage{enumitem}
84 \setlist[description,itemize,enumerate]{noitemsep}
85
86 \usepackage{multicol}
87
88 % some fix from https://tex.stackexchange.com/questions/83204/how-can-i-make-source-code-included-with-minted-copyable
89 \usepackage{accsupp}
90 \newcommand\emptyaccsupp[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
91 % default definition is: \def\theFancyVerbLine{\rmfamily\tiny\arabic{FancyVerbLine}}
92 \let\theHFancyVerbLine\theFancyVerbLine% don't apply our patch to hyperref's version
93 \def\theFancyVerbLine{\rmfamily\tiny\emptyaccsupp{\arabic{FancyVerbLine}}}
94
95 \newcommand{\clsopt}[1]{\texttt{#1}}
96 \newmintinline{latex}{}
97 \hypersetup{pdfencoding=auto,linktoc=page}
98 % workaround from https://tex.stackexchange.com/questions/407873/using-minted-in-l3doc-documentation
99 \fvset{gobble=0}
100
101 \usepackage{kvmap}
102
103 \begin{document}
104 \DocInput{kvmap.dtx}
105 \end{document}
106 %</driver>
107 %
108 %<*pkg>
109 %<@@=kvmap>
110 \RequirePackage{expl3}
111 \ProvidesExplPackage{kvmap}{2020/09/16}{0.3.5}{Drawing karnaugh maps with LaTeX}
112 %</pkg>
113 % \fi
114 %
115 % \changes{v0.1.0}{2018/03/10}{Stable release}
116 % \changes{v0.2.0}{2018/08/17}{Rework documentation and syntax recommendations}
117 % \changes{v0.3.0}{2018/09/20}{Improve documentation}
118 % \changes{v0.3.1}{2018/09/20}{Fix INS file}
119 % \GetFileInfo{kvmap.sty}
120 %
121 % \title{The \pkg{kvmap} package\thanks{This document corresponds to
122 % \pkg{kvmap}~\fileversion, dated~\filedate.}}
123 % \author{Ben Frank\\\url{https://gitlab.com/benfrank/kvmap}}
124 % \date{\today}
125 % \maketitle
126 % \begin{abstract}
127 % \noindent This package provides a simple way to typeset Karnaugh maps
128 % including automated generation of gray code and options to draw bundles of
129 % adjacent cells (implicants).
130 % \end{abstract}
131 % \vfill
132 % \section*{Contents}
133 % \begin{multicols*}{2}
134 % \listoftoc*{toc}
135 % \end{multicols*}
136 % \clearpage
137 %
138 % \begin{documentation}
139 % \section{Introduction}
140 % \pkg{kvmap} aims to provide a \emph{user-friendly} (i.e. less typing) way to
141 % typeset Karnaugh maps including the surrounding gray code and bundles of
142 % cells (implicants). This package relies on \pkg{amsmath}, \pkg{xparse} (with
143 % \pkg{expl3}) and \pkg{tikz}.\par
144 % Drawing Karnaugh maps is not that uncommon and there are already packages
145 % available on CTAN that provide means to typeset them:\begin{itemize}
146 % \item \pkg{askmaps} -- This package lets you draw American style Karnaugh
147 % maps but restricts you to five variables.
148 % \item \pkg{karnaugh-map} -- This package allows you to typeset Karnaugh
149 % maps with up to 6 variables. Unfortunately, the user has to provide many
150 % arguments which makes changing the input error-prone.
151 % \item \pkg{karnaugh} -- This package lets you typeset Karnaugh maps up
152 % to ten variables but I wanted more of what \pkg{askmaps} calls ``American
153 % style''.
154 % \item \pkg{karnaughmap} -- This package aims to be a more customizable
155 % version of \pkg{karnaugh}, so unfortunately, it does not meet my style
156 % requirements either.
157 % \item There is also a TikZ library called \pkg{karnaugh} which allows you
158 % to use 12 variables (or 4096 cells) and has superior styling options, even
159 % for maps with Gray code. But personally I do not like the input format.
160 % \end{itemize}
161 % So these are basically the reasons, why I need yet another Karnaugh map
162 % package. And this package is based on my personal needs, so if you are
163 % missing some exciting feature, feel free to open an issue at Gitlab.\par
164 % \paragraph*{Acknowledgements} Special thanks to
165 % \href{https://tex.stackexchange.com/users/124577/texnician}{TeXnician} who
166 % provided a first version of the Karnaugh map code^^A
167 % \footnote{see \url{https://tex.stackexchange.com/a/425135}} and
168 % \href{https://tex.stackexchange.com/users/80496/marcel-kr%c3%bcger}{Marcel Krüger}
169 % who developed the relevant code to generate sequences of gray code
170 % \footnote{see \url{https://tex.stackexchange.com/a/418887}}.
171 % \section{Drawing Karnaugh maps}
172 % \subsection{Basic commands and environments}
173 % \begin{function}{\kvmapsetup}
174 % \begin{syntax}
175 % |\kvmapsetup|\marg{options}
176 % \end{syntax}
177 % This function sets key-value pairs. Please note that you may need a prefix
178 % like |bundle| in front of the key. Currently the range of supported keys is
179 % very limited, but you will learn about them in the description of
180 % \tn{bundle}.
181 % \end{function}
182 % \begin{docEnvironment}{kvmap}{\oarg{key-value pairs}}{}
183 % \DescribeEnv{kvmap}This environment is a semantic interface to
184 % \env{tikzpicutre} which should have a \env{kvmatrix} or \tn{kvlist} as
185 % first child element. Basically this should surround every Karnaugh map
186 % you typeset.
187 % \end{docEnvironment}
188 % \begin{docEnvironment}{kvmatrix}{\marg{a,b,c[,\dots]}}{}
189 % \DescribeEnv{kvmatrix}This environment is one of the two input modes. It
190 % provides a structured way of inputting rows and columns, similar to the
191 % |tabular| environment. You should prefer this over \tn{kvlist}.\par
192 % The environment itself takes one argument: a comma-separated list of
193 % variable names. Please note that they are typeset in math-mode, so you
194 % should not use |$| signs.
195 % \end{docEnvironment}
196 % \begin{function}{\kvlist}
197 % \begin{syntax}
198 % |\kvlist|\marg{width}\marg{height}\marg{1,0[,\dots]}\marg{a,b[,\dots]}
199 % \end{syntax}
200 % This function provides the alternative input mode. You specify width and
201 % height of the matrix and then input your elements row-wise. The last
202 % argument consists of a comma-separated list of variable names. They are
203 % typeset in math-mode.
204 % \end{function}
205 % \subsection{Drawing Bundles (implicants)}
206 % Another feature of this package is to draw bundles, at least that is how I
207 % prefer to call rectangles visualizing adjacent ones or zeros (also called
208 % implicants). Please note that currently this package does not compute the
209 % bundles for you.
210 % \begin{function}{\bundle}
211 % \begin{syntax}
212 % |\bundle|\oarg{key-value pairs}\marg{$x_1$}\marg{$y_1$}\marg{$x_2$}\marg{$y_2$}
213 % \end{syntax}
214 % This function draws a rectangle (bundle) around the area specified by the
215 % two corners. The option |invert| opens the bundle outwards and
216 % |overlapmargins| lets you specify a length which describes how far the
217 % edges will be drawn into the margin (both options are useful for corners).
218 % With |color| you may change the color of the border and |reducespace|
219 % allows you to specify whether you want the package to be narrower or wider.
220 % \par
221 % When using |invert|, the package tries to determine where to invert
222 % automatically. Sometimes it fails with the guesswork. You can manually
223 % disable the horizontal inversion part with |hinvert=false| (idem for the
224 % vertical inversion part with |vinvert=false|).\par
225 % Warning: This package is unable to draw a bundle including all four corners
226 % this way. If you need this specific edge case, please use TikZ to draw it
227 % yourself (see the last example in \autoref{sec:examples}).
228 % \end{function}
229 % \subsection{Styling the nodes}
230 % This package defines two TikZ styles to allow easy customizations: |kvnode|
231 % and |kvbundle|. The former is applied to every node within the output matrix,
232 % i.e. the zeros and ones. It styles a node, so all options applicable to a
233 % |\node| are available to you.\par
234 % The latter describes the styling of the bundles' paths. It is applied to a
235 % |\draw| command, so you have to use options available to paths.\par
236 % The whole Karnaugh map is a TikZ grid with many TikZ nodes. In the case you
237 % need to address a single node you may refer to it by its position, i.e.
238 % the node's name is |xy| (zero-based), e.g. |00| for the node in the upper
239 % left corner.
240 % \section{Examples}\label{sec:examples}
241 %\iffalse
242 %<*driver>
243 %\fi
244 \begin{tcblisting}{}
245 \kvlist{2}{4}{0,1,0,0,0,0,0,1}{a,b,c}
246 \hfill
247 \begin{kvmatrix}{a,b,c}
248 0 & 1 & 1 & 0\\
249 0 & 1 & 0 & 1
250 \end{kvmatrix}
251 \end{tcblisting}
252 \begin{tcblisting}{}
253 \kvlist{4}{4}{0,1,1,0,0,1,0,1,0,1,1,1,0,0,1,1}{a,b,c,d}
254 \end{tcblisting}
255 \begin{tcblisting}{}
256 \begin{kvmap}
257 \kvlist{4}{4}{0,1,1,0,0,1,0,1,0,1,1,1,0,0,1,1}{a,b,c,d,e,f}
258 \bundle[color=red]{1}{1}{1}{2}
259 \bundle{3}{2}{2}{3}
260 \bundle[color=blue,reducespace=3pt]{3}{2}{3}{1}
261 \end{kvmap}
262 \end{tcblisting}
263 \begin{tcblisting}{}
264 \begin{kvmap}
265 \begin{kvmatrix}{a,b,c,d}
266 0 & 1 & 1 & 0\\
267 1 & 0 & 0 & 1\\
268 0 & 0 & 0 & 1\\
269 0 & 1 & 1 & 1\\
270 \end{kvmatrix}
271 \bundle{3}{3}{2}{3}
272 \bundle[color=blue]{3}{2}{3}{1}
273 \bundle[invert=true,reducespace=2pt,overlapmargins=6pt]{1}{0}{2}{3}
274 \bundle[invert=true,reducespace=2pt]{0}{1}{3}{1}
275 \end{kvmap}
276 \end{tcblisting}
277 \begin{tcblisting}{}
278 \begin{kvmap}
279 \begin{kvmatrix}{a,b,c,d,e,f}
280 0 & 1 & 1 & 0 & 0 & 1 & 0 & 1\\
281 1 & 1 & 1 & 0 & 0 & 1 & 1 & 0\\
282 1 & 0 & 0 & 1 & 0 & 1 & 0 & 1\\
283 1 & 0 & 0 & 1 & 1 & 0 & 1 & 1\\
284 0 & 1 & 0 & 1 & 0 & 1 & 1 & 1\\
285 0 & 1 & 1 & 0 & 1 & 1 & 0 & 0\\
286 0 & 1 & 0 & 1 & 1 & 1 & 0 & 0\\
287 1 & 1 & 0 & 1 & 0 & 1 & 0 & 1
288 \end{kvmatrix}
289 \draw[fill=white, opacity=.6] (00.center) rectangle (22.center);
290 \draw[white, line width=.4cm] (07) -- (70);
291 \draw[fill=white, opacity=.6] (77.center) rectangle (55.center);
292 \end{kvmap}
293 \end{tcblisting}
294 %\iffalse
295 %</driver>
296 %\fi
297 % \end{documentation}
298 % \begin{implementation}
299 % \clearpage\section{Implementation}
300 % \changes{v0.3.5}{2020/09/16}{Add amsmath dependency to have smash option b available}
301 % \begin{macrocode}
302 \RequirePackage{amsmath}
303 \RequirePackage{xparse}
304 \RequirePackage{tikz}
305 % \end{macrocode}
306 % \begin{macro}[aux]{\seq_set_split:Nnx,\int_mod:VV,\int_div_truncate:VV}
307 % Define variants for better expansion.
308 % \begin{macrocode}
309 \cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }
310 \cs_generate_variant:Nn \int_mod:nn { VV }
311 \cs_generate_variant:Nn \int_div_truncate:nn { VV }
312
313 % \end{macrocode}
314 % \end{macro}
315 % \subsection{Generating the Gray code}
316 % This part of the code is primarily based on Marcel Krüger's StackExchange
317 % post (see
318 % \url{https://tex.stackexchange.com/questions/418853/latex3-pad-something-unexpandable}).
319 % \begin{macro}[int]{\@@_graycode_xor_bits:nn}
320 % This function will apply xor to two bits.
321 % \begin{arguments}
322 % \item bit (0 or 1)
323 % \item bit (0 or 1)
324 % \end{arguments}
325 % \begin{macrocode}
326 \cs_new:Npn \@@_graycode_xor_bits:nn #1#2
327 {
328 \int_compare:nTF { #1 = #2 }
329 { 0 } { 1 }
330 }
331 % \end{macrocode}
332 % \end{macro}
333 % \begin{macro}[int]{\@@_graycode_xor:w,\@@_graycode_xor:nn,\@@_graycode_xor:xx}
334 % This macro executes a bitwise xor on two expanded bit sequences. It is
335 % defined recursively, so that on every run the first two bits from the bit
336 % sequence are split.
337 % \begin{arguments}
338 % \item first bit sequence
339 % \item second bit sequence
340 % \end{arguments}
341 % \begin{macrocode}
342 \cs_new:Npn \@@_graycode_xor:w #1#2\q_stop#3#4\q_stop
343 {
344 \@@_graycode_xor_bits:nn { #1 } { #3 }
345 \tl_if_empty:nF { #2 }
346 {
347 \@@_graycode_xor:w #2\q_stop#4\q_stop
348 }
349 }
350 \cs_new:Npn \@@_graycode_xor:nn #1#2
351 {
352 \@@_graycode_xor:w #1\q_stop#2\q_stop
353 }
354 \cs_generate_variant:Nn \@@_graycode_xor:nn { xx }
355
356 % \end{macrocode}
357 % \end{macro}
358 % \begin{macro}{\kvmap_graycode_at:nn}
359 % Calculate the gray code at a specific digit, see the Wikipedia for details.
360 % \begin{arguments}
361 % \item digit
362 % \item number of bits
363 % \end{arguments}
364 % \begin{macrocode}
365 \cs_new:Npn \kvmap_graycode_at:nn #1#2
366 {
367 \@@_graycode_xor:xx
368 { \tl_tail:f { \int_to_bin:n { #1 - 1 + #2 } } }
369 { \tl_tail:f { \int_to_bin:n { \fp_eval:n { floor((#1-1)/2) + #2 } } } }
370 }
371 \cs_generate_variant:Nn \kvmap_graycode_at:nn { nV }
372
373 % \end{macrocode}
374 % \end{macro}
375 % \subsection{Internal code to automate output}
376 % \begin{variable}[int]{\l_@@_matrix_isintikz_bool}
377 % Are we in a tikzpicture?
378 % \begin{macrocode}
379 \bool_new:N \l_@@_matrix_isintikz_bool
380 % \end{macrocode}
381 % \end{variable}
382 % \begin{variable}[int]{\l_@@_matrix_height_int,\l_@@_matrix_width_int}
383 % Save the dimensions of the matrix (important for bundling).
384 % \begin{macrocode}
385 \int_new:N \l_@@_matrix_height_int
386 \int_new:N \l_@@_matrix_width_int
387 % \end{macrocode}
388 % \end{variable}
389 % \begin{macro}[int]{\@@_outputgraycode:}
390 % Output gray code around the grid.
391 % \begin{macrocode}
392 \cs_new:Nn \@@_outputgraycode:
393 {
394 % \end{macrocode}
395 % Iterate through the horizontal part first and add each item as node. Add $-1$
396 % to the coordinate calculation to convert one-based to zero-based numbers.
397 % \begin{macrocode}
398 \int_step_inline:nnnn { 1 } { 1 } { \l_@@_matrix_width_int }
399 {
400 \node ~ at ~ (\fp_eval:n { 0.5 + (##1-1) }, .3)
401 { \kvmap_graycode_at:nV { ##1 } \l_@@_matrix_width_int };
402 }
403 % \end{macrocode}
404 % Afterwards tackle the vertical part.
405 % \begin{macrocode}
406 \int_step_inline:nnnn { 1 } { 1 } { \l_@@_matrix_height_int }
407 {
408 \node[anchor = east] ~ at ~ (0, \fp_eval:n { -0.5 - (##1-1) })
409 { \kvmap_graycode_at:nV { ##1 } \l_@@_matrix_height_int };
410 }
411 }
412
413 % \end{macrocode}
414 % \end{macro}
415 % \begin{macro}[int]{\@@_outputmatrix:n}
416 % \changes{v0.3.2}{2019/12/26}{Allow empty elements}
417 % \changes{v0.3.4}{2020/04/25}{Fix node size}
418 % Define a TikZ style for easier customizability.
419 % \begin{macrocode}
420 \tikzset{kvnode/.style = { minimum ~ width=.75cm, minimum ~ height=.75cm }}
421 % \end{macrocode}
422 % This macro fill the grid with values.
423 % \begin{arguments}
424 % \item list
425 % \end{arguments}
426 % \begin{macrocode}
427 \cs_new:Npn \@@_outputmatrix:n #1
428 {
429 % \end{macrocode}
430 % We iterate using a for each loop, hence there is no counter and we need to
431 % define one.
432 % \begin{macrocode}
433 \int_zero:N \l_tmpa_int
434 % \end{macrocode}
435 % Use a temporary sequence to store the argument. This has to be a split because
436 % setting from clist would eliminate empty elements.
437 % \begin{macrocode}
438 \seq_set_split:Nnn \l_tmpa_seq { , } { #1 }
439 % \end{macrocode}
440 % Loop over the elements of the list. Every element will be output as node
441 % where $x = \text{counter}\mod\text{width}$ and
442 % $y=\left\lfloor\frac{\text{counter}}{\text{height}}\right\rfloor$.
443 % \begin{macrocode}
444 \seq_map_inline:Nn \l_tmpa_seq
445 {
446 \node[kvnode] ~
447 (\int_mod:VV \l_tmpa_int \l_@@_matrix_width_int
448 \int_div_truncate:VV \l_tmpa_int \l_@@_matrix_width_int ) ~
449 at ~
450 (.5+\int_mod:VV \l_tmpa_int \l_@@_matrix_width_int,
451 -.5-\int_div_truncate:nn \l_tmpa_int \l_@@_matrix_width_int ) ~
452 {\smash[b]{\makebox[0pt]{$##1$}}};
453 \int_incr:N \l_tmpa_int
454 }
455 }
456
457 % \end{macrocode}
458 % \end{macro}
459 % \subsection{Implicant-related code}
460 % \begin{macro}[updated=2018-03-17]{\bundle}
461 % \changes{v0.1.1}{2018/03/17}{Correct dimensions}
462 % \changes{v0.2.1}{2018/09/20}{Fix inversion}
463 % \changes{v0.3.3}{2020/03/27}{Introduce hinvert and vinvert}
464 % Draw a bundle with given corners.
465 % \begin{arguments}
466 % \item key-value pairs
467 % \item x coordinate of point 1
468 % \item y coordinate of point 1
469 % \item x coordinate of point 2
470 % \item y coordinate of point 2
471 % \end{arguments}
472 % \begin{macrocode}
473 \keys_define:nn { kvmap/bundle }
474 {
475 % \end{macrocode}
476 % |reducespace|: reduce inner sep of the node; negative values mean expansion
477 % \begin{macrocode}
478 reducespace .dim_set:N = \l_@@_bundle_reducespace_dim,
479 reducespace .initial:n = { 0pt },
480 % \end{macrocode}
481 % |color|: path color of the bundle
482 % \begin{macrocode}
483 color .tl_set:N = \l_@@_bundle_color_tl,
484 color .initial:n = { black },
485 % \end{macrocode}
486 % |invert|: open bundle instead of closed path
487 % \begin{macrocode}
488 invert .bool_set:N = \l_@@_bundle_invert_bool,
489 invert .default:n = true,
490 invert .initial:n = false,
491 % \end{macrocode}
492 % |vinvert|: perform inversion vertically
493 % \begin{macrocode}
494 vinvert .bool_set:N = \l_@@_bundle_vinvert_bool,
495 vinvert .default:n = true,
496 vinvert .initial:n = true,
497 % \end{macrocode}
498 % |hinvert|: perform inversion horizontally
499 % \begin{macrocode}
500 hinvert .bool_set:N = \l_@@_bundle_hinvert_bool,
501 hinvert .default:n = true,
502 hinvert .initial:n = true,
503 % \end{macrocode}
504 % |overlapmargins|: intrude into margin (when inverted)
505 % \begin{macrocode}
506 overlapmargins .dim_set:N = \l_@@_bundle_overlapmargins_dim,
507 overlapmargins .initial:n = { 0pt },
508 }
509 % \end{macrocode}
510 % TikZ-style to easily adapt the bundle design.
511 % \begin{macrocode}
512 \tikzset{kvbundle/.style = { rounded ~ corners = 5pt }}
513 % \end{macrocode}
514 % \begin{variable}[int]{\l_@@_bundle_minx_int,\l_@@_bundle_miny_int,\l_@@_bundle_maxx_int,\l_@@_bundle_maxy_int}
515 % Auxiliary variables for drawing bundles.
516 % \begin{macrocode}
517 \int_new:N \l_@@_bundle_minx_int
518 \int_new:N \l_@@_bundle_miny_int
519 \int_new:N \l_@@_bundle_maxx_int
520 \int_new:N \l_@@_bundle_maxy_int
521 % \end{macrocode}
522 % \end{variable}
523 % \begin{macrocode}
524 \NewDocumentCommand { \bundle } { O{} m m m m }
525 {
526 \group_begin:
527 % \end{macrocode}
528 % Set optional parameters and save the minima and maxima of x- and
529 % y-coordinates.
530 % \begin{macrocode}
531 \keys_set:nn { kvmap/bundle } { #1 }
532 \int_set:Nn \l_@@_bundle_minx_int { \int_min:nn { #2 } { #4 } }
533 \int_set:Nn \l_@@_bundle_miny_int { \int_min:nn { #3 } { #5 } }
534 \int_set:Nn \l_@@_bundle_maxx_int { \int_max:nn { #2 } { #4 } }
535 \int_set:Nn \l_@@_bundle_maxy_int { \int_max:nn { #3 } { #5 } }
536 % \end{macrocode}
537 % Check whether the bundle will be inverted. The current conformance test which
538 % is executed later on allows the edge case of inverting at every corner, which
539 % is currently unsupported (code-wise).
540 % \begin{macrocode}
541 \bool_if:NTF \l_@@_bundle_invert_bool
542 {
543 % \end{macrocode}
544 % If the bundle will be inverted, the first check is whether it will exceed the
545 % height. In that case there will be an opened rectangle on both sides (top and
546 % bottom) which is positioned according to the minima and maxima of the
547 % coordinates. The |reducespace| option is realized as shift.
548 % \begin{macrocode}
549 \bool_if:nT
550 {
551 \int_compare_p:n { \l_@@_matrix_height_int - 1 = \l_@@_bundle_maxy_int }
552 && \int_compare_p:n { 0 = \l_@@_bundle_miny_int }
553 && \l_@@_bundle_vinvert_bool
554 }
555 {
556 \draw[draw=\l_@@_bundle_color_tl,kvbundle] ~
557 ([xshift=\l_@@_bundle_reducespace_dim,
558 yshift=\l_@@_bundle_overlapmargins_dim]
559 \int_use:N \l_@@_bundle_minx_int
560 \int_use:N \l_@@_bundle_miny_int . north ~ west) --
561 ([xshift=\l_@@_bundle_reducespace_dim,
562 yshift=\l_@@_bundle_reducespace_dim]
563 \int_use:N \l_@@_bundle_minx_int
564 \int_use:N \l_@@_bundle_miny_int . south ~ west) --
565 ([xshift=-\l_@@_bundle_reducespace_dim,
566 yshift=\l_@@_bundle_reducespace_dim]
567 \int_use:N \l_@@_bundle_maxx_int
568 \int_use:N \l_@@_bundle_miny_int . south ~ east) --
569 ([xshift=-\l_@@_bundle_reducespace_dim,
570 yshift=\l_@@_bundle_overlapmargins_dim]
571 \int_use:N \l_@@_bundle_maxx_int
572 \int_use:N \l_@@_bundle_miny_int . north ~ east);
573 \draw[draw=\l_@@_bundle_color_tl,kvbundle] ~
574 ([xshift=\l_@@_bundle_reducespace_dim,
575 yshift=-\l_@@_bundle_overlapmargins_dim]
576 \int_use:N \l_@@_bundle_minx_int
577 \int_use:N \l_@@_bundle_maxy_int . south ~ west) --
578 ([xshift=\l_@@_bundle_reducespace_dim,
579 yshift=-\l_@@_bundle_reducespace_dim]
580 \int_use:N \l_@@_bundle_minx_int
581 \int_use:N \l_@@_bundle_maxy_int . north ~ west) --
582 ([xshift=-\l_@@_bundle_reducespace_dim,
583 yshift=-\l_@@_bundle_reducespace_dim]
584 \int_use:N \l_@@_bundle_maxx_int
585 \int_use:N \l_@@_bundle_maxy_int . north ~ east) --
586 ([xshift=-\l_@@_bundle_reducespace_dim,
587 yshift=-\l_@@_bundle_overlapmargins_dim]
588 \int_use:N \l_@@_bundle_maxx_int
589 \int_use:N \l_@@_bundle_maxy_int . south ~ east);
590 }
591 % \end{macrocode}
592 % Is it larger than the width? Then turn 90° and work as above.
593 % \begin{macrocode}
594 \bool_if:nT
595 {
596 \int_compare_p:n { \l_@@_matrix_width_int - 1 = \l_@@_bundle_maxx_int }
597 && \int_compare_p:n { 0 = \l_@@_bundle_minx_int }
598 && \l_@@_bundle_hinvert_bool
599 }
600 {
601 \draw[draw=\l_@@_bundle_color_tl,kvbundle] ~
602 ([yshift=-\l_@@_bundle_reducespace_dim,
603 xshift=-\l_@@_bundle_overlapmargins_dim]
604 \int_use:N \l_@@_bundle_minx_int
605 \int_use:N \l_@@_bundle_miny_int . north ~ west) --
606 ([yshift=-\l_@@_bundle_reducespace_dim,
607 xshift=-\l_@@_bundle_reducespace_dim]
608 \int_use:N \l_@@_bundle_minx_int
609 \int_use:N \l_@@_bundle_miny_int . north ~ east) --
610 ([yshift=\l_@@_bundle_reducespace_dim,
611 xshift=-\l_@@_bundle_reducespace_dim]
612 \int_use:N \l_@@_bundle_minx_int
613 \int_use:N \l_@@_bundle_maxy_int . south ~ east) --
614 ([yshift=\l_@@_bundle_reducespace_dim,
615 xshift=-\l_@@_bundle_overlapmargins_dim]
616 \int_use:N \l_@@_bundle_minx_int
617 \int_use:N \l_@@_bundle_maxy_int . south ~ west);
618 \draw[draw=\l_@@_bundle_color_tl,kvbundle] ~
619 ([yshift=-\l_@@_bundle_reducespace_dim,
620 xshift=\l_@@_bundle_overlapmargins_dim]
621 \int_use:N \l_@@_bundle_maxx_int
622 \int_use:N \l_@@_bundle_miny_int . north ~ east) --
623 ([yshift=-\l_@@_bundle_reducespace_dim,
624 xshift=\l_@@_bundle_reducespace_dim]
625 \int_use:N \l_@@_bundle_maxx_int
626 \int_use:N \l_@@_bundle_miny_int . north ~ west) --
627 ([yshift=\l_@@_bundle_reducespace_dim,
628 xshift=\l_@@_bundle_reducespace_dim]
629 \int_use:N \l_@@_bundle_maxx_int
630 \int_use:N \l_@@_bundle_maxy_int . south ~ west) --
631 ([yshift=\l_@@_bundle_reducespace_dim,
632 xshift=\l_@@_bundle_overlapmargins_dim]
633 \int_use:N \l_@@_bundle_maxx_int
634 \int_use:N \l_@@_bundle_maxy_int . south ~ east);
635 }
636 }
637 {
638 % \end{macrocode}
639 % If the package will not be inverted, it will be output by using the
640 % coordinates provided (min and max).
641 % \begin{macrocode}
642 \draw[draw=\l_@@_bundle_color_tl, kvbundle] ~
643 ([xshift=\l_@@_bundle_reducespace_dim,
644 yshift=-\l_@@_bundle_reducespace_dim]
645 \int_use:N \l_@@_bundle_minx_int
646 \int_use:N \l_@@_bundle_miny_int . north ~ west) ~
647 rectangle ~
648 ([xshift=-\l_@@_bundle_reducespace_dim,
649 yshift=\l_@@_bundle_reducespace_dim]
650 \int_use:N \l_@@_bundle_maxx_int
651 \int_use:N \l_@@_bundle_maxy_int . south ~ east);
652 }
653 \group_end:
654 }
655 % \end{macrocode}
656 % \end{macro}
657 % \subsection{User-interface code}
658 % \begin{macro}{\kvmap_map:nn,\kvmap_map:xn}
659 % Output the matrix (interface level).
660 % \begin{arguments}
661 % \item values
662 % \item variables (lables), first horizontal part, then vertical
663 % \end{arguments}
664 % Example: |\kvmap_map:nn{0,1,1,0,0,1,0,1,0,1,1,1,0,0,1,1}{a,b,c,d}|
665 % \begin{macrocode}
666 \cs_new:Npn \kvmap_map:nn #1#2
667 {
668 % \end{macrocode}
669 % Firstly, generate the map (lines).
670 % \begin{macrocode}
671 \draw ~ (0,0) ~ grid ~
672 (\int_use:N \l_@@_matrix_width_int, -\int_use:N \l_@@_matrix_height_int);
673 % \end{macrocode}
674 % After having drawn the grid, construct the nodes from the csv and output the
675 % gray code.
676 % \begin{macrocode}
677 \@@_outputmatrix:n { #1 }
678 \@@_outputgraycode:
679 \draw ~ (0,0) ~ -- ~ (-.7,.7);
680 % \end{macrocode}
681 % Now the labels will be output. The first part of the csv will be positioned
682 % at the top right, the second part bottom left. The point of separation will
683 % be saved.
684 % \begin{macrocode}
685 \int_set:Nn \l_tmpa_int
686 { \fp_eval:n { floor(ln(\l_@@_matrix_width_int)/ln(2)) } }
687 \tl_clear:N \l_tmpa_tl
688 \int_step_inline:nnnn { 1 } { 1 } { \l_tmpa_int }
689 {
690 \tl_put_right:Nn \l_tmpa_tl { \clist_item:nn { #2 } { ##1 } }
691 }
692 \node[anchor = west] ~ at ~ (-.5, .7) ~ { $\tl_use:N \l_tmpa_tl$ };
693 % \end{macrocode}
694 % After working on the horizontal part, work on the left entries. This will
695 % also become a node.
696 % \begin{macrocode}
697 \tl_clear:N \l_tmpa_tl
698 \int_step_inline:nnnn { \l_tmpa_int + 1 } { 1 }
699 { \l_tmpa_int + \fp_eval:n { floor(ln(\l_@@_matrix_height_int)/ln(2)) } }
700 {
701 \tl_put_right:Nn \l_tmpa_tl { \clist_item:nn { #2 } { ##1 } }
702 }
703 \node[anchor = east] ~ at ~ (-.4, .2) ~ { $\tl_use:N \l_tmpa_tl$ };
704 }
705 \cs_generate_variant:Nn \kvmap_map:nn { xn }
706
707 % \end{macrocode}
708 % \end{macro}
709 % \begin{environment}{kvmap}
710 % This is a |tikzpicture|, but for semantic reasons introduced as new
711 % environment. Furthermore this will clear the results of the last execution.
712 % \begin{arguments}
713 % \item optional: key-value pairs
714 % \end{arguments}
715 % \begin{macrocode}
716 \NewDocumentEnvironment { kvmap } { O{} }
717 {
718 \group_begin:
719 \keys_set:nn { kvmap } { #1 }
720 \int_gzero:N \l_@@_matrix_height_int
721 \int_gzero:N \l_@@_matrix_width_int
722 \begin{tikzpicture}
723 }
724 {
725 \end{tikzpicture}
726 \group_end:
727 }
728 % \end{macrocode}
729 % \end{environment}
730 % \begin{macro}{\kvlist}
731 % User wrapper around \cs{kvmap_map:nn}. It will insert a |tikzpicture| if not
732 % already present.
733 % \begin{macrocode}
734 \NewDocumentCommand { \kvlist } { m m m m }
735 {
736 \tikzifinpicture
737 { \bool_set_true:N \l_@@_matrix_isintikz_bool }
738 { \bool_set_false:N \l_@@_matrix_isintikz_bool }
739 \bool_if:NF \l_@@_matrix_isintikz_bool
740 { \begin{tikzpicture} }
741 \int_gset:Nn \l_@@_matrix_width_int { #1 }
742 \int_gset:Nn \l_@@_matrix_height_int { #2 }
743 \kvmap_map:nn { #3 } { #4 }
744 \bool_if:NF \l_@@_matrix_isintikz_bool
745 { \end{tikzpicture} }
746 }
747
748 % \end{macrocode}
749 % \end{macro}
750 % \begin{environment}{kvmatrix}
751 % \changes{v0.3.2}{2019/12/26}{Count empty columns}
752 % This environment enables a |tabular|-like input syntax.
753 % \begin{arguments}
754 % \item labels (variables)
755 % \end{arguments}
756 % \begin{variable}[int]{\l_@@_tmp_seq}
757 % Temporary variable to split the matrix.
758 % \begin{macrocode}
759 \seq_new:N \l_@@_tmp_seq
760 % \end{macrocode}
761 % \end{variable}
762 % \begin{macrocode}
763 \NewDocumentEnvironment { kvmatrix } { m +b }
764 {
765 % \end{macrocode}
766 % Split the environments body at |\\| and remove empty lines. Now the height
767 % of the map is just the count of the sequence. Split the first element at |&|
768 % and use the count of that as width.
769 % \begin{macrocode}
770 \seq_set_split:Nnn \l_tmpa_seq { \\ } { #2 }
771 \seq_remove_all:Nn \l_tmpa_seq { }
772 \seq_set_split:Nnx \l_tmpb_seq { & } { \seq_item:Nn \l_tmpa_seq { 1 } }
773 \int_gset:Nn \l_@@_matrix_width_int { \seq_count:N \l_tmpb_seq }
774 \int_gset:Nn \l_@@_matrix_height_int { \seq_count:N \l_tmpa_seq }
775 % \end{macrocode}
776 % Clean up the lists and convert the input into something \cs{kvmap_map:nn} may
777 % process. Maybe this could be optimised in terms of performance (TikZ matrix?).
778 % \begin{macrocode}
779 \seq_clear:N \l_@@_tmp_seq
780 \seq_map_inline:Nn \l_tmpa_seq
781 {
782 \seq_clear:N \l_tmpb_seq
783 \seq_set_split:Nnn \l_tmpb_seq { & } { ##1 }
784 \seq_concat:NNN \l_@@_tmp_seq \l_@@_tmp_seq \l_tmpb_seq
785 }
786 % \end{macrocode}
787 % Output the map by inserting commas between the elements of the
788 % temporary sequence.
789 % \begin{macrocode}
790 \tikzifinpicture
791 { \bool_set_true:N \l_@@_matrix_isintikz_bool }
792 { \bool_set_false:N \l_@@_matrix_isintikz_bool }
793 \bool_if:NF \l_@@_matrix_isintikz_bool
794 { \begin{tikzpicture} }
795 \kvmap_map:xn
796 { \seq_use:Nnnn \l_@@_tmp_seq { , } { , } { , } }
797 { #1 }
798 \bool_if:NF \l_@@_matrix_isintikz_bool
799 { \end{tikzpicture} }
800 } { }
801
802 % \end{macrocode}
803 % \end{environment}
804 % \begin{macro}{\kvmapsetup}
805 % Set options (key-value pairs).
806 % Example: |\kvmapsetup{bundle/color=red}|
807 % \begin{arguments}
808 % \item key-value pairs
809 % \end{arguments}
810 % \begin{macrocode}
811 \NewDocumentCommand { \kvmapsetup } { m }
812 {
813 \keys_set:nn { kvmap } { #1 }
814 }
815 % \end{macrocode}
816 % \end{macro}
817 % \end{implementation}
818 % \clearpage\PrintChanges
819 % ^^A\clearpage\PrintIndex
820 % \Finale
821 % \endinput