summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/drawmatrix/drawmatrix.dtx
blob: 857bc15ac4c78567e721aa1edc4e065db99dfd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
% \iffalse meta-comment
%
% Copyright (C) 2016 by Elmar Peise
% -----------------------------------------
%
% This package is distributed under the MIT license.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{drawmatrix}[2016/08/25 v1.2.0 drawmatrix package]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{hypdoc}
\usepackage{drawmatrix}
\usepackage{xspace}
\EnableCrossrefs
\RecordChanges
\CodelineIndex
\OnlyDescription
\begin{document}
  \DocInput{drawmatrix.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{131}
%
% \CharacterTable
%   {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
%    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
%    Digits        \0\1\2\3\4\5\6\7\8\9
%    Exclamation   \!     Double quote  \"     Hash (number) \#
%    Dollar        \$     Percent       \%     Ampersand     \&
%    Acute accent  \'     Left paren    \(     Right paren   \)
%    Asterisk      \*     Plus          \+     Comma         \,
%    Minus         \-     Point         \.     Solidus       \/
%    Colon         \:     Semicolon     \;     Less than     \<
%    Equals        \=     Greater than  \>     Question mark \?
%    Commercial at \@     Left bracket  \[     Backslash     \\
%    Right bracket \]     Circumflex    \^     Underscore    \_
%    Grave accent  \`     Left brace    \{     Vertical bar  \|
%    Right brace   \}     Tilde         \~}
%
% \changes{v1.0.0}{2014/02/23}{Initial Version}
% \changes{v1.0.1}{2014/09/08}{Bugfix: Collapsible bbox (label placement for
% vectors)}
% \changes{v1.0.2}{2015/03/20}{Bugfix: Bbox had a linewidth/2 offset}
% \changes{v1.0.3}{2015/11/18}{Introduced |diag| option}
% \changes{v1.1.0}{2015/11/26}{Separate lower and upper banding, externalization
% control, fixed matrix style collection, lots of simplifications, moved
% ``every'' keys to |/drawmatrix| path}
% \changes{v1.1.1}{2016/01/29}{Bugfix: Remove extra space after vectors}
% \changes{v1.2.0}{2016/01/29}{Add coordinate transformations}
%
%
% \GetFileInfo{drawmatrix.sty}
%
% \title{The \textsf{drawmatrix} package}
% \author{Elmar Peise \\ \texttt{peise@aices.rwth-aachen.de}}
% \date{\filedate \ \fileversion}
%
% \newenvironment{example}{
%   \begin{center}\begin{minipage}{.9\textwidth}
%       \scriptsize
% }{
%   \end{minipage}\end{center}
% }
% \newcommand\TikZ{Ti{\it k}Z\xspace}
%
% \maketitle
%
% \begin{abstract}
%   \textsf{drawmatrix} provides macros to visually represent matrices.  Various
%   options allow to change the visualizations, e.g., drawing rectangular,
%   triangular, or banded matrices.
% \end{abstract}
%
% \tableofcontents
%
% \section{Introduction}
% In many situations, visual representations of matrices facilitate the
% understanding of linear algebra properties, relations, and operations
% enormously.  This package provides simple tools to bring such representations
% to \LaTeX.  For instance,
% $$
%   \drawmatrix[upper]A \;
%   \drawmatrix[width=.5]X +
%   \drawmatrix[width=.5]X \;
%   \drawmatrix[upper, size=.5, bbox height=1]B =
%   \drawmatrix[width=.5]C
% $$
% is typeset as follows:
% \begin{verbatim}
% $$
%   \drawmatrix[upper]A \;
%   \drawmatrix[width=.5]X +
%   \drawmatrix[width=.5]X \;
%   \drawmatrix[upper, size=.5, bbox height=1]B =
%   \drawmatrix[width=.5]C
% $$
% \end{verbatim}
%
%
% \section{Drawing Matrices}
% \DescribeMacro\drawmatrix |\drawmatrix|\oarg{options}\marg{label} draws a
% matrix labeled \meta{label}: |\drawmatrix A| produces \drawmatrix A.  The
% \meta{options}, which modify various aspects of drawn matrix through PGF's
% key-value system, are introduced in the following sections.
%
% By default, the matrix is centered around its label, which is aligned with the
% surrounding text.  The label is typeset in the surrounding mode and style.
% \begin{example}
%   |$\drawmatrix A$|:
%    $\drawmatrix A$
%
%   |{\bf \drawmatrix A}|:
%    {\bf \drawmatrix A}
%
%   |{\large \drawmatrix A}|:
%    {\large \drawmatrix A}
% \end{example}
% In equations, parentheses (spanned with |\left| and |\right|), subscripts, and
% superscripts naturally extend to the drawn shape: $\left(\drawmatrix{A}_i +
% \drawmatrix{B}^{-1}\right) \drawmatrix{C}$.  Used in matrix products such as
% $\drawmatrix{A} \drawmatrix{B}$, a little space (|\;|) helps to yield a more
% natural result: $\drawmatrix{A} \; \drawmatrix{B}$.
%
% \subsection{Size}
% By default, matrices are $1 \times 1$ large in terms of \TikZ units.  The
% width and height of a matrix are set through, respectively,
% \DescribeMacro{height} |width=|\meta{dimension} and \DescribeMacro{width}
% |height=|\meta{dimension}.  A width or height of 0 are useful to represent
% vectors:
% \begin{example}
%   |\drawmatrix[width=0]A|:
%    \drawmatrix[width=0]A
% \end{example}
%
% \DescribeMacro{size} |size=|\meta{dimension} sets both the width and height to
% \meta{dimension}, resulting in a square matrix.
%
% \subsection{Shape}
% By default matrices are rectangular.
% \subsubsection{Triangular and Trapezoidal Matrices}
% Lower and upper triangular matrices are obtained by, respectively, setting the
% keys \DescribeMacro{lower} |lower| and \DescribeMacro{upper} |upper|.  Hereby,
% non-square matrices become trapezoidal.
% \begin{example}
%   |\drawmatrix[lower]L|:
%    \drawmatrix[lower]L
%
%   |\drawmatrix[upper, width=1.5]U|:
%    \drawmatrix[upper, width=1.5]U
% \end{example}
%
% \subsubsection{Banded Matrices}
% Matrices are drawn as banded through the key \DescribeMacro{banded} |banded|.
% The band width, i.e., the horizontal/vertical extent from the diagonal, is
% specified through \DescribeMacro{bandwidth} |bandwidth=|\meta{dimension}
% (default: 0.3);
% \begin{example}
%   |\drawmatrix[banded]B|:
%    \drawmatrix[banded]B
%
%   |\drawmatrix[bandwidth=.5]B|:
%    \drawmatrix[bandwidth=.5]B
% \end{example}
%
% Banding for the lower and upper part of the matrices can be specified
% separately through \DescribeMacro{lower banded} |lower banded| and
% \DescribeMacro{upper banded} |upper banded|.  Separate bandwidths are set
% through \DescribeMacro{lower bandwidth} |lower bandwidth=|\meta{dimension} and
% \DescribeMacro{upper bandwidth} |upper bandwidth=|\meta{dimension}:
% \begin{example}
%   |\drawmatrix[lower banded]B|:
%    \drawmatrix[lower banded]B
%
%   |\drawmatrix[lower bandwidth=.5, upper bandwidth=.2]B|:
%    \drawmatrix[lower bandwidth=.5, upper bandwidth=.2]B
% \end{example}
%
% Banding on rectangular matrices applies to the smaller of the two dimensions:
% \begin{example}
%   |\drawmatrix[banded, width=.8]B|:
%    \drawmatrix[banded, width=.8]B
%
%   |\drawmatrix[upper banded, height=.7]B|:
%    \drawmatrix[upper banded, height=.7]B
% \end{example}
%
% |banded| can be combined with |lower| or |upper| to draw the intersection of
% both shapes.
% \begin{example}
%   |\drawmatrix[banded, lower]L|:
%    \drawmatrix[banded, lower]L
% \end{example}
%
% \subsubsection{Diagonal Matrices}
% \DescribeMacro{diag} |diag| is a shorthand for |banded| with |bandwidth=0|:
% \begin{example}
%   |\drawmatrix[diag]D|:
%    \drawmatrix[diag]D
% \end{example}
%
% \subsection{Colors and Style}
% By default, matrices are drawn in gray and filled white.  The \TikZ keys
% |draw=|\meta{color} and |fill=|\meta{color} change these colors.  In fact, all
% keys not recognized by this package are passed to the \TikZ |\filldraw|
% command drawing the matrix.
% \begin{example}
%   |\drawmatrix[fill=yellow, draw=blue]A|:
%    \drawmatrix[fill=yellow, draw=blue]A
%
%   |\drawmatrix[very thick, dashed]A|:
%    \drawmatrix[very thick, dashed]A
% \end{example}
%
% \subsection{The Bounding Box}
% All matrices are contained in a rectangular bounding box.  To draw this
% bounding box (e.g., to visualize the 0 entries in the matrix), use
% \DescribeMacro{bbox style} |bbox style=|\marg{style};  this style is applied
% to the \TikZ |\node| that is the bounding box.
% \begin{example}
%   |\drawmatrix[lower, bbox style={fill=blue!10}]L|:
%    \drawmatrix[lower, bbox style={fill=blue!10}]L
% \end{example}
%
% By default, the bounding box is just large enough to contain the matrix. Its
% size is changed through the keys \DescribeMacro{bbox height}
% |bbox height=|\meta{dimension} and \DescribeMacro{bbox width}
% |bbox width=|\meta{dimension} (or \DescribeMacro{bbox size}
% |bbox size=|\meta{dimension} to set them both).  The label of the matrix (and
% thus the alignment with respect to the surrounding text) are fixed at the
% center of the bounding box, while the matrix is positioned at its top-left
% corner.
% \begin{example}
%   |\drawmatrixset{bbox style={fill=blue!10}}|
%    \drawmatrixset{bbox style={fill=blue!10}}
%
%   |\drawmatrix[bbox width=2, bbox height=1.5]A|:
%    \drawmatrix[bbox width=2, bbox height=1.5]A
% \end{example}
%
% \sloppy The matrix can be positioned within its bounding box through
% \DescribeMacro{offset height} |offset height=|\meta{dimension} and
% \DescribeMacro{offset width} |offset width=|\meta{dimension} (or just
% \DescribeMacro{offset} |offset=|\meta{dimension} to shift along the diagonal).
% \begin{example}
%   |\drawmatrixset{bbox style={fill=blue!10}}|
%    \drawmatrixset{bbox style={fill=blue!10}}
%
%   |\drawmatrix[bbox size=2, offset width=.5, offset height=.75]A|:
%    \drawmatrix[bbox size=2, offset width=.5, offset height=.75]A
% \end{example}
%
% \subsection{Coordinate system transformations}
%
% |scale=|\meta{factor} scales all dimensions passed to a matrix:
% \begin{example}
%  |\drawmatrix[scale=.6]A \drawmatrix[scale=.6, width=.5]B|:
%   \drawmatrix[scale=.6]A \drawmatrix[scale=.6, width=.5]B
% \end{example}
%
% |x=|\meta{value} and |y=|\meta{value} define the coordinate system for all
% unit-less dimensions.
% \begin{example}
%  |\drawmatrix[x=.6cm, y=.4cm]A \drawmatrix[x=.6cm, y=.4cm, width=1cm]B|:
%   \drawmatrix[x=.6cm, y=.4cm]A \drawmatrix[x=.6cm, y=.4cm, width=1cm]B
% \end{example}
%
%
% \subsection{Position of the Label and Baseline}
% By default, the label's |mid| is positioned at the bounding box's |center| and
% its |base| is used as the whole drawing's baseline.  This is controlled by the
% keys \DescribeMacro{label anchor} |label anchor=|\meta{anchor},
% \DescribeMacro{label pos} |label pos=|\meta{position}, and
% \DescribeMacro{baseline} |baseline=|\meta{position}. Here, \meta{position} has
% to be an anchor of one of the following nodes: |bbox| (the bounding box),
% |matrix| (the matrix itself), or |label| (the label).
% \begin{example}
%   |\drawmatrixset{bbox height=1, height=.5, bbox style={fill=blue!10}}|
%    \drawmatrixset{bbox height=1, height=.5, bbox style={fill=blue!10}}
%
%   |\drawmatrix[label pos=bbox.south, label anchor=south]A|:
%    \drawmatrix[label pos=bbox.south, label anchor=south]A
%
%   |\drawmatrix[label pos=matrix.north west]A|:
%    \drawmatrix[label pos=matrix.north west]A
%
%   |\drawmatrix[baseline=label.north]A|:
%    \drawmatrix[baseline=label.north]A
%
%   |\drawmatrix[baseline=bbox.south]A|:
%    \drawmatrix[baseline=bbox.south]A
% \end{example}
%
% \section{Changing Defaults}
% Specifying \meta{options} with \DescribeMacro{\drawmatrixset}
% |\drawmatrixset|\marg{options} applies them to all following uses of
% |\drawmatrix| within the current scope.
% \begin{example}
%   |\drawmatrixset{height=.5, lower}|
%    \drawmatrixset{height=.5, lower}
%
%   |$\drawmatrix A \; \drawmatrix B$|:
%    $\drawmatrix A \; \drawmatrix B$
% \end{example}
%
% Furthermore, \TikZ keys for the entire picture, the bounding box, the matrix
% itself and the label can be set through the styles \DescribeMacro{every bbox}
% |every bbox|, \DescribeMacro{every drawmatrix} |every drawmatrix|, and
% \DescribeMacro{every label} |every label|.
% \begin{example}
%    |\drawmatrixset{every drawmatrix/.append style={rounded corners=5pt}}|
%     \drawmatrixset{every drawmatrix/.append style={rounded corners=5pt}}
%
%    |$\drawmatrix A \; \drawmatrix[lower]B$|:
%     $\drawmatrix A \; \drawmatrix[lower]B$
% \end{example}
%
% \section{Externalization}
% |\drawmatrix| behaves as any other \TikZ picture, therefore when
% externalization is enabled, all matrix visualizations are also externalized.
% However, since there are usually many |\drawmatrix| pictures, each of which is
% very small and fast to produce, their externalization would mean a tremendous
% overhead.  To avoid this overhead without explicitly dis- and re-enabling
% externalization throughout the document, \DescribeMacro{externalize}
% |externalize=false| disables externalization for all |\drawmatrix| pictures:
% \begin{example}
%   |\drawmatrixset{externalize=false}|
%    \drawmatrixset{externalize=false}
% \end{example}
%
%
% \section{Implementation}
%
% \DoNotIndex{
%   \dm@bboxheight, \dm@bboxwidth, \dm@currname, \dm@currval, \dm@height,
%   \dm@labeltext, \dm@lowerbandwidth, \dm@minsize, \dm@offsetheight,
%   \dm@offsetwidth, \dm@upperbandwidth, \dm@width, \dm@zero, \RequirePackage,
%   \begin, \def, \edef, \else, \end, \expandafter, \fi, \filldraw,
%   \ifdrawmatrix@externalize, \ifmmode, \ifx, \let, \newcommand, \newif, \node,
%   \path, \pgfkeys, \pgfkeysalso, \pgfkeyscurrentname, \pgfkeyscurrentvalue,
%   \pgfkeysnovalue, \pgflinewidth, \pgfgetlastxy, \pgfmathsetlengthmacro,
%   \pgfresetboundingbox, \pgfqkeys, \tikz@library@external@loaded, \tikzset,
%   \undefined
% }
%
% This section describes the implementation details of the \textsf{drawmatrix}
% package.
%
% \subsection{Package: \TikZ}
%
% The \textsf{tikz} package is used for drawing.
%    \begin{macrocode}
\RequirePackage{tikz}
%    \end{macrocode}
%
% \subsection{If for externalization}
%
% \TeX{} |if| representing whether to explicitly disable \TikZ externalization.
% \begin{macro}{\ifdrawmatrix@externalize}
%    \begin{macrocode}
\newif\ifdrawmatrix@externalize
%    \end{macrocode}
% \end{macro}
%
% \subsection{Key Declarations and Defaults}
%
% We rely on PGF keys as much as we can.
%    \begin{macrocode}
\pgfkeys{
%    \end{macrocode}
%
% Everything happens in the path |/drawmatrix|.
%    \begin{macrocode}
    drawmatrix/.is family,
    drawmatrix/.cd,
%    \end{macrocode}
%
% \begin{macro}{picture}
% \begin{macro}{baseline}
%   |picture| is the style for the |\tikzpicture| in which the matrix is drawn.
%   |baseline| sets the baseline of the picture to a named coordinate of the
%   matrix (default: base of the label).
%    \begin{macrocode}
    picture/.style={},
    path/.style={},
    baseline/.style={picture/.append style={baseline=(drawmatrix #1)}},
    scale/.style={path/.append style={scale=#1}},
    x/.style={path/.append style={x=#1}},
    y/.style={path/.append style={y=#1}},
    baseline=label.base,
%    \end{macrocode}
% \end{macro}\end{macro}
%
% \begin{macro}{bbox}\begin{macro}{bbox style}
%   |bbox| is the style of the bounding box, to which |bbox style| appends keys.
%    \begin{macrocode}
    bbox/.style={},
    bbox style/.style={bbox/.append style={#1}},
%    \end{macrocode}
% \end{macro}\end{macro}
%
% \begin{macro}{bbox height}
% \begin{macro}{bbox width}
% \begin{macro}{bbox size}
%   |bbox height| and |bbox width| don't have default values.  |bbox size| sets
%   them both to the same value.
%    \begin{macrocode}
    bbox height/.initial,
    bbox width/.initial,
    bbox size/.style={bbox height=#1, bbox width=#1},
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}
%
% \begin{macro}{offset height}
% \begin{macro}{offset width}
% \begin{macro}{offset}
%   |offset height| and |offset width| are |0| by default. |offset| sets them
%   both to the same value.
%    \begin{macrocode}
    offset height/.initial=0,
    offset width/.initial=0,
    offset/.style={offset height=#1, offset width=#1},
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}
%
% \begin{macro}{height}
% \begin{macro}{width}
% \begin{macro}{size}
%   |width| and |height| are |1| (\TikZ unit) by default. |size| sets them both
%   to the same value.
%    \begin{macrocode}
    height/.initial=1,
    width/.initial=1,
    size/.style={height=#1, width=#1},
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}
%
% \begin{macro}{lower bandwidth}
% \begin{macro}{upper bandwidth}
% \begin{macro}{bandwidth}
%   The |lower bandwidth| and |upper bandwidth| don't have default values.
%   |bandwidth| sets them both to the same value.
%    \begin{macrocode}
    lower bandwidth/.initial,
    upper bandwidth/.initial,
    bandwidth/.style={lower bandwidth=#1, upper bandwidth=#1},
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}
%
% \begin{macro}{lower banded}
% \begin{macro}{upper banded}
% \begin{macro}{banded}
%   |lower banded| and |upper banded| are shortcuts to set the corresponding
%   bandwidths to the default value of |0.3| (\TikZ units). |banded| sets them
%   both.
%    \begin{macrocode}
    lower banded/.style={lower bandwidth=.3},
    upper banded/.style={upper bandwidth=.3},
    banded/.style={lower banded, upper banded},
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}
%
% \begin{macro}{lower}
% \begin{macro}{upper}
% \begin{macro}{diag}
%   |lower| and |upper| are implemented by setting the opposite bandwidth to
%   |0|. |diag| sets them both.
%    \begin{macrocode}
    lower/.style={upper bandwidth=0},
    upper/.style={lower bandwidth=0},
    diag/.style={lower, upper},
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}
%
% \begin{macro}{externalize}
%   |externalize| sets a \TeX{} if (default: |true| = behave as all pictures).
%    \begin{macrocode}
    externalize/.is if=drawmatrix@externalize,
    externalize=true,
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{label}
% \begin{macro}{label pos}
% \begin{macro}{label anchor}
%   |label| is the style for the label. |label pos| sets the label at a named
%   coordinate of the matrix (default: center of the bounding box).
%   |label anchor| sets the label's |anchor| (default: in the middle).
%    \begin{macrocode}
    label/.style={},
    label pos/.style={label/.append style={at=(drawmatrix #1)}},
    label pos=bbox.center,
    label anchor/.style={label/.append style={anchor=#1}},
    label anchor=mid,
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}
%
% Unknown keys are collected in |/drawmatrix/drawmatrix|.
%    \begin{macrocode}
    drawmatrix/.style={},
    .unknown/.code={%
        \let\dm@currname\pgfkeyscurrentname%
        \let\dm@currval\pgfkeyscurrentvalue%
        \ifx#1\pgfkeysnovalue\pgfkeysalso{
            drawmatrix/.append style/.expand once={\dm@currname}
        }\else\pgfkeysalso{
            drawmatrix/.append style/.expand twice={%
                \expandafter\dm@currname\expandafter=\dm@currval%
            }
        }\fi%
    },
%    \end{macrocode}
%
% \begin{macro}{every picture}
% \begin{macro}{every bbox}
% \begin{macro}{every drawmatrix}
% \begin{macro}{every label}
%   The default style for matrices: |every picture| applies to all
%   |\tikzpicture|s the matrices are drawn in, |every bbox| applies to all
%   bounding boxes, |every drawmatrix| applies to the matrices themselves, and
%   |every label| applies to the labels.
%    \begin{macrocode}
    every picture/.style={},
    every bbox/.style={
        name=drawmatrix bbox,
        inner sep=0,
    },
    every drawmatrix/.style={
        fill=white,
        draw=gray,
    },
    every label/.style={
        name=drawmatrix label,
        outer sep=0,
        inner sep=0,
    },
    every node/.style={
        name=drawmatrix matrix,
        outer sep=0,
        inner sep=0,
        anchor=north west,
    }
}
%    \end{macrocode}
% \end{macro}\end{macro}\end{macro}\end{macro}
%
% \subsection{User Macros}
%
% |\drawmatrixset| as a simple shortcut like |\tikzset|.
% \begin{macro}{\drawmatrixset}
%    \begin{macrocode}
\newcommand\drawmatrixset[1]{\pgfqkeys{/drawmatrix}{#1}}
%    \end{macrocode}
% \end{macro}
%
% Here we go, the main thing: |\drawmatrix|. First, apply the options and
% extract the sizes from the PGF keys.
% \begin{macro}{\drawmatrix}
%    \begin{macrocode}
\newcommand\drawmatrix[2][]{{%
    \drawmatrixset{
        #1,
        height/.get=\dm@height,
        width/.get=\dm@width,
        lower bandwidth/.get=\dm@lowerbandwidth,
        upper bandwidth/.get=\dm@upperbandwidth,
        offset height/.get=\dm@offsetheight,
        offset width/.get=\dm@offsetwidth,
        bbox height/.get=\dm@bboxheight,
        bbox width/.get=\dm@bboxwidth,
    }%
%    \end{macrocode}
%
% Prepare the label text (keep math mode).
%    \begin{macrocode}
    \ifmmode\def\dm@labeltext{$#2$}\else\def\dm@labeltext{#2}\fi%
%    \end{macrocode}
%
% Disable externalization if |externalize=false|.  Start the picture.
%    \begin{macrocode}
    \ifdrawmatrix@externalize\else%
        \ifx\tikz@library@external@loaded\undefined\else%
            \tikzset{external/export=false}%
        \fi%
    \fi%
    \begin{tikzpicture}[/drawmatrix/every picture, /drawmatrix/picture]
%    \end{macrocode}
%
% Parse width, height, the minimum dimension and zero for comparison purposes.
%    \begin{macrocode}
        \path[/drawmatrix/path] (\dm@width, \dm@height);
        \pgfgetlastxy\dm@width\dm@height
        \path[/drawmatrix/path] (\dm@offsetwidth, \dm@offsetheight);
        \pgfgetlastxy\dm@offsetwidth\dm@offsetheight
        \pgfmathsetlengthmacro\dm@minsize{min(\dm@width, \dm@height)}
        \pgfmathsetlengthmacro\dm@zero{0.0}
%    \end{macrocode}
%
% Prepare the band widths:  First, if the matrix is not banded, the bandwidth is
% set to the smaller matrix dimension.  Then, the band width is limited by this
% smaller dimension.
%    \begin{macrocode}
        \expandafter\ifx\dm@lowerbandwidth\pgfkeysnovalue
            \edef\dm@lowerbandwidth{\dm@minsize}
        \else
            \path[/drawmatrix/path] (\dm@lowerbandwidth, 0);
            \pgfgetlastxy\dm@lowerbandwidth\dm@zero
        \fi
        \expandafter\ifx\dm@upperbandwidth\pgfkeysnovalue
            \edef\dm@upperbandwidth{\dm@minsize}
        \else
            \path[/drawmatrix/path] (0, \dm@upperbandwidth);
            \pgfgetlastxy\dm@zero\dm@upperbandwidth
        \fi
        \pgfmathsetlengthmacro\dm@lowerbandwidth{
            min(\dm@minsize, \dm@lowerbandwidth)
        }
        \pgfmathsetlengthmacro\dm@upperbandwidth{
            min(\dm@minsize, \dm@upperbandwidth)
        }
%    \end{macrocode}
%
% Set the default bounding box size.
%    \begin{macrocode}
        \expandafter\ifx\dm@bboxheight\pgfkeysnovalue
            \pgfmathsetlengthmacro\dm@bboxheight{
                \dm@height + \dm@offsetheight
            }
        \else
            \path[/drawmatrix/path] (0, \dm@bboxheight);
            \pgfgetlastxy\dm@zero\dm@bboxheight
        \fi
        \expandafter\ifx\dm@bboxwidth\pgfkeysnovalue
            \pgfmathsetlengthmacro\dm@bboxwidth{
                \dm@width + \dm@offsetwidth
            }
        \else
            \path[/drawmatrix/path] (\dm@bboxwidth, 0);
            \pgfgetlastxy\dm@bboxwidth\dm@zero
        \fi
%    \end{macrocode}
%
% Reset the bounding box and begin with (drawing) the path for the bounding box.
%    \begin{macrocode}
        \pgfresetboundingbox
        \node[/drawmatrix/every bbox, /drawmatrix/bbox,
            minimum height=\dm@bboxheight,
            minimum width=\dm@bboxwidth] {};
%    \end{macrocode}
%
% Whether needed or not, declare all matrix corners.
%    \begin{macrocode}
        \path (drawmatrix bbox.north west)
            ++(\dm@offsetwidth, -\dm@offsetheight)
            ++(.5\pgflinewidth, -.5\pgflinewidth)
            coordinate (drawmatrix north west)
            ++(\dm@width, 0)
            +(-\dm@minsize + \dm@upperbandwidth, 0)
            coordinate (drawmatrix north)
            +(0, -\dm@minsize + \dm@upperbandwidth)
            coordinate (drawmatrix east)
            ++(0, -\dm@height)
            coordinate (drawmatrix south east)
            ++(-\dm@width, 0)
            +(\dm@minsize - \dm@lowerbandwidth, 0)
            coordinate (drawmatrix south)
            +(0, \dm@minsize - \dm@lowerbandwidth)
            coordinate (drawmatrix west);
%    \end{macrocode}
%
% Add an invisible node the size of the matrix.
%    \begin{macrocode}
        \node[/drawmatrix/every node,
            minimum height=\dm@height,
            minimum width=\dm@width]
            at (drawmatrix north west) {};
%    \end{macrocode}
%
% Now, draw only what is needed of the matrix.  Otherwise path modifications
% (e.g., such as rounded corners) might not work.
%    \begin{macrocode}
        \filldraw[/drawmatrix/every drawmatrix, /drawmatrix/drawmatrix]
            (drawmatrix north west)
            \ifx\dm@upperbandwidth\dm@zero
                \ifx\dm@width\dm@minsize\else -- (drawmatrix north) \fi
                \ifx\dm@height\dm@minsize\else -- (drawmatrix east) \fi
            \else
                -- (drawmatrix north)
                \ifx\dm@upperbandwidth\dm@minsize\else
                    -- (drawmatrix east)
                \fi
            \fi
            -- (drawmatrix south east)
            \ifx\dm@lowerbandwidth\dm@zero
                \ifx\dm@width\dm@minsize\else -- (drawmatrix south) \fi
                \ifx\dm@height\dm@minsize\else -- (drawmatrix west) \fi
            \else
                -- (drawmatrix south)
                \ifx\dm@lowerbandwidth\dm@minsize\else
                    -- (drawmatrix west)
                \fi
            \fi
            -- cycle;
%    \end{macrocode}
%
% The label.
%    \begin{macrocode}
        \node[/drawmatrix/every label, /drawmatrix/label]
            {\dm@labeltext};
    \end{tikzpicture}%
}}
%    \end{macrocode}
% \end{macro}
%
% \Finale
% \PrintIndex
% \PrintChanges
%
\endinput