summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/pgf/text-en/pgfmanual-en-tikz-matrices.tex
blob: 265880d2b6e436382d8f9cce8bf443d982c2c42d (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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
% Copyright 2006 by Till Tantau
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\section{Matrices and Alignment}

\label{section-matrices}

\subsection{Overview}

When creating pictures, one often faces the problem of correctly
aligning parts of the picture. For example, you might wish that the
base lines of certain nodes should be on the same line and some
further nodes should be below these nodes with, say, their centers on
a vertical lines. There are different ways of solving such
problems. For example, by making clever use of anchors, nearly all
such alignment problems can be solved. However, this often leads to
complicated code. An often simpler way is to use \emph{matrices},
the use of which is explained in the current section.

A \tikzname\ matrix is similar to \LaTeX's |{tabular}| or
|{array}| environment, only instead of text each cell contains a
little picture or a node. The sizes of the cells are automatically
adjusted such that they are large enough to contain all the cell
contents.

Matrices are a powerful tool and they need to handled with some care.
For impatient readers who skip the rest of this section: you
\emph{must} end \emph{every} row with |\\|. In particular, the last
row \emph{must} be ended with |\\|.

Many of the ideas implemented in \tikzname's matrix support are due to
Mark Wibrow -- many thanks to Mark at this point!



\subsection{Matrices are Nodes}

Matrices are special in many ways, but for most purposes matrices are
treated like nodes. This means, that you use the |node| path command
to create a matrix and you only use a special option, namely the
|matrix| option, to signal that the node will contain a
matrix. Instead of the usual \TeX-box that makes up the |text| part of
the node's shape, the matrix is used. Thus, in particular, a matrix
can have a shape, this shape can be drawn or filled, it can be used in
a tree, and so on. Also, you can refer to the different anchors of a
matrix.

\begin{key}{/tikz/matrix=\meta{true or false} (default true)}
  This option can be passed to a |node| path command. It signals that
  the node will contain a matrix.
\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (4,2);
  \node [matrix,fill=red!20,draw=blue,very thick] (my matrix) at (2,1)
  {
    \draw (0,0)   circle (4mm); & \node[rotate=10] {Hello};        \\
    \draw (0.2,0) circle (2mm); & \fill[red]   (0,0) circle (3mm); \\
  };

  \draw [very thick,->] (0,0) |- (my matrix.west);
\end{tikzpicture}
\end{codeexample}
  The exact syntax of the matrix is explained in the course of this
  section.
  \begin{stylekey}{/tikz/every matrix (initially \normalfont empty)}
    This style is used in every matrix.
  \end{stylekey}
\end{key}

Even more so than nodes, matrices will often be the only object on a
path. Because of this, there is a special abbreviation for creating matrices:

\begin{command}{\matrix}
  Inside |{tikzpicture}| this is an abbreviation for |\path node[matrix]|.
\end{command}

Even though matrices are nodes, some options do not have the same
effect as for normal nodes:
\begin{enumerate}
\item Rotations and scaling have no effect on a matrix as a whole
  (however, you can still transform the contents of the cells
  normally). Before the matrix is typeset, the rotational and scaling
  part of the transformation matrix is reset.
\item For multi-part shapes you can only set the |text| part of the
  node.
\item All options starting with |text| such as |text width| have no
  effect.
\end{enumerate}



\subsection{Cell Pictures}

A matrix consists of rows of \emph{cells}. Each row (including the
last one!) is ended by the command |\\|. The character |&| is used
to separate cells. Inside each cell, you must place commands for
drawing a picture, called the \emph{cell picture} in the
following. (However, cell pictures are not enclosed in a complete
|{pgfpicture}| environment, they are a bit more light-weight. The main
difference is that cell pictures cannot have layers.) It is not
necessary to specify beforehand how many rows or columns there are
going to be and if a row contains less cell pictures than another
line, empty cells are automatically added as needed.


\subsubsection{Alignment of Cell Pictures}

For each cell picture a bounding box is computed. These bounding boxes
and the origins of the cell pictures determine how the cells are
aligned. Let us start with the rows: Consider the cell pictures on the first
row. Each has a bounding box and somewhere inside this bounding box
the origin of the cell picture can be found (the origin might even lie
outside the bounding box, but let us ignore this problem for the
moment). The cell pictures are then shifted around such that all
origins lie on the same horizontal line. This may make it necessary to
shift some cell pictures upwards and other downwards, but it can be
done and this yields the vertical alignment of the cell pictures this
row. The top of the row is then given by the top of the ``highest''
cell picture in the row, the bottom of the row is given by the bottom
of the lowest cell picture. (To be more precise, the height of the row
is the maximum $y$-value of any of the bounding boxes and the depth of
the row is the negated minimum $y$-value of the bounding boxes).

\begin{codeexample}[]
\begin{tikzpicture}
  [every node/.style={draw=black,anchor=base,font=\huge}]

  \matrix [draw=red]
  {
    \node {a}; \fill[blue] (0,0) circle (2pt); &
    \node {X}; \fill[blue] (0,0) circle (2pt); &
    \node {g}; \fill[blue] (0,0) circle (2pt); \\
  };
\end{tikzpicture}
\end{codeexample}

Each row is aligned in this fashion: For each row the cell pictures
are vertically aligned such that the origins lie on the same
line. Then the second row is placed below the first row such that the
bottom of the first row touches the top of the second row (unless a
|row sep| is used to add a bit of space). Then the bottom of the
second row touches the top of the third row, and so on. Typically,
each row will have an individual height and depth.

\begin{codeexample}[]
\begin{tikzpicture}
  [every node/.style={draw=black,anchor=base}]

  \matrix [draw=red]
  {
    \node {a}; & \node {X}; & \node {g}; \\
    \node {a}; & \node {X}; & \node {g}; \\
  };

  \matrix [row sep=3mm,draw=red] at (0,-2)
  {
    \node {a}; & \node {X}; & \node {g}; \\
    \node {a}; & \node {X}; & \node {g}; \\
  };
\end{tikzpicture}
\end{codeexample}

Let us now have a look at the columns. The rules for how the pictures
on any given column are aligned are very similar to the row
alignment: Consider all cell pictures in the first column. Each is
shifted horizontally such that the origins lie on the same vertical
line. Then, the left end of the column is at the left end of the
bounding box that protrudes furthest to the left. The right end of the
column is at the right end of the bounding box that protrudes furthest
to the left. This fixes the horizontal alignment of the cell pictures
in the first column and the same happens the cell pictures in the
other columns. Then, the right end of the first column touches the
left end of the second column (unless |column sep| is used). The right
end of the second column touches the left end of the third column, and
so on. (Internally, two columns are actually used to achieve the
desired horizontal alignment, but that is only and implementation
detail.)

\begin{codeexample}[]
\begin{tikzpicture}[every node/.style={draw}]
  \matrix [draw=red]
  {
    \node[left]  {Hallo}; \fill[blue] (0,0) circle (2pt); \\
    \node        {X};     \fill[blue] (0,0) circle (2pt); \\
    \node[right] {g};     \fill[blue] (0,0) circle (2pt); \\
  };
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[]
\begin{tikzpicture}[every node/.style={draw}]
  \matrix [draw=red,column sep=1cm]
  {
    \node {8}; & \node{1}; & \node {6}; \\
    \node {3}; & \node{5}; & \node {7}; \\
    \node {4}; & \node{9}; & \node {2}; \\
  };
\end{tikzpicture}
\end{codeexample}



\subsubsection{Setting and Adjusting Column and Row Spacing}

There are different ways of setting and adjusting the spacing between
columns and rows. First, you can use the options |column sep| and
|row sep| to set a default spacing for all rows and all
columns. Second, you can add options to the |&| character and the |\\|
command to adjust the spacing between two specific columns or
rows. Additionally, you can specify whether the space between two
columns or rows should be considered between the origins of cells in
the column or row or between their borders.

\begin{key}{/tikz/column sep=\meta{spacing list}}
  This option sets a default space that is added between every two
  columns. This space can be positive or negative and is zero by
  default. The \meta{spacing list} normally contains a single
  dimension like |2pt|.
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [draw,column sep=1cm,nodes=draw]
  {
    \node(a) {123}; & \node (b) {1};   & \node {1}; \\
    \node    {12};  & \node     {12};  & \node {1}; \\
    \node(c) {1};   & \node (d) {123}; & \node {1}; \\
  };
  \draw [red,thick]  (a.east) -- (a.east |- c)
                     (d.west) -- (d.west |- b);
  \draw [<->,red,thick] (a.east) -- (d.west |- b)
    node [above,midway] {1cm};
\end{tikzpicture}
\end{codeexample}
  More generally, the \meta{spacing list} may contain a whole list of
  numbers, separated by commas, and occurrences of the two key words
  |between origins| and |between borders|. The effect of specifying
  such a list is the following: First, all numbers occurring in the
  list are simply added to compute the final spacing. Second,
  concerning the two keywords, the last occurrence of one of the keywords is
  important. If the last occurrence is |between borders| or if neither
  occurs, then the space is inserted between the two columns
  normally. However, if the last occurs is |between origins|, then the
  following happens: The distance between the columns is adjusted such
  that the difference between the origins of all the cells in the
  first column (remember that they all lie on straight line) and the
  origins of all the cells in the second column is exactly the given
  distance.

  \emph{The |between origins| option can only be used for columns
    mentioned in the first row, that is, you cannot specify this
    option for columns introduced only in later rows.}

\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [draw,column sep={1cm,between origins},nodes=draw]
  {
    \node(a) {123}; & \node (b) {1};   & \node {1}; \\
    \node    {12};  & \node     {12};  & \node {1}; \\
    \node    {1};   & \node     {123}; & \node {1}; \\
  };
  \draw [<->,red,thick] (a.center) -- (b.center) node [above,midway] {1cm};
\end{tikzpicture}
\end{codeexample}
\end{key}

\begin{key}{/tikz/row sep=\meta{spacing list}}
  This option works like |column sep|, only for rows. Here, too, you
  can specify whether the space is added between the lower end of the
  first row and the upper end of the second row, or whether the space
  is computed between the origins of the two rows.
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [draw,row sep=1cm,nodes=draw]
  {
    \node (a) {123}; & \node {1};   & \node {1}; \\
    \node (b) {12};  & \node {12};  & \node {1}; \\
    \node     {1};   & \node {123}; & \node {1}; \\
  };
  \draw [<->,red,thick] (a.south) -- (b.north) node [right,midway] {1cm};
\end{tikzpicture}
\end{codeexample}
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [draw,row sep={1cm,between origins},nodes=draw]
  {
    \node (a) {123}; & \node {1};   & \node {1}; \\
    \node (b) {12};  & \node {12};  & \node {1}; \\
    \node     {1};   & \node {123}; & \node {1}; \\
  };
  \draw [<->,red,thick] (a.center) -- (b.center) node [right,midway] {1cm};
\end{tikzpicture}
\end{codeexample}
\end{key}

The row-end command |\\| allows you to provide an optional
argument, which must be a dimension. This dimension will be added to
the list in |row sep|. This means that, firstly, any numbers you list
in this argument will be added as an extra row separation between the
line being ended and the next line and, secondly, you can use the
keywords |between origins| and |between borders| to locally overrule
the standard setting for this line pair.
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [row sep=1mm]
  {
    \draw (0,0) circle (2mm); & \draw (0,0) circle (2mm); \\
    \draw (0,0) circle (2mm); & \draw (0,0) circle (2mm); \\[-1mm]
    \draw (0,0) coordinate (a) circle (2mm); &
    \draw (0,0) circle (2mm); \\[1cm,between origins]
    \draw (0,0) coordinate (b) circle (2mm); &
    \draw (0,0) circle (2mm); \\
  };
  \draw [<->,red,thick] (a.center) -- (b.center) node [right,midway] {1cm};
\end{tikzpicture}
\end{codeexample}

The cell separation character |&| also takes an optional
argument, which must also be a spacing list. This spacing list is
added to the |column sep| having a similar effect as the option for
the |\\| command for rows.

This optional spacing list can only be given the first time
a new column is started (usually in the first row), subsequent usages
of this option in later rows have no effect.
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [draw,nodes=draw,column sep=1mm]
  {
    \node {8}; &[2mm] \node{1}; &[-1mm] \node {6}; \\
    \node {3}; &      \node{5}; &       \node {7}; \\
    \node {4}; &      \node{9}; &       \node {2}; \\
  };
\end{tikzpicture}
\end{codeexample}
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [draw,nodes=draw,column sep=1mm]
  {
    \node {8}; &[2mm] \node(a){1}; &[1cm,between origins] \node(b){6}; \\
    \node {3}; &      \node   {5}; &                      \node   {7}; \\
    \node {4}; &      \node   {9}; &                      \node   {2}; \\
  };
  \draw [<->,red,thick] (a.center) -- (b.center) node [above,midway] {11mm};
\end{tikzpicture}
\end{codeexample}
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [draw,nodes=draw,column sep={1cm,between origins}]
  {
    \node (a) {8}; & \node (b) {1}; &[between borders] \node (c) {6}; \\
    \node     {3}; & \node     {5}; &                  \node     {7}; \\
    \node     {4}; & \node     {9}; &                  \node     {2}; \\
  };
  \draw [<->,red,thick] (a.center) -- (b.center) node [above,midway] {10mm};
  \draw [<->,red,thick] (b.east) -- (c.west) node [above,midway] {10mm};
\end{tikzpicture}
\end{codeexample}




\subsubsection{Cell Styles and Options}

For following style and option are useful for changing the appearance
of the all cell pictures:

\begin{stylekey}{/tikz/every cell=\marg{row}\marg{column} (initially \normalfont empty)}
  This style is installed at the beginning of each cell picture with
  the two parameters being the current \meta{row} and \meta{column} of
  the cell. Note that setting this style to |draw| will \emph{not}
  cause all nodes to be drawn since the |draw| option has to be passed
  to each node individually.

  Inside this style (and inside all cells), the current \meta{row} and
  \meta{column} number are also accessible via the counters
  |\pgfmatrixcurrentrow| and |\pgfmatrixcurrentcolumn|.
\end{stylekey}

\begin{key}{/tikz/cells=\meta{options}}
  This key adds the \meta{options} to the style |every cell|. It mainly
  just a shorthand for the code
  |every cell/.append style=|\meta{options}.
\end{key}

\begin{key}{/tikz/nodes=\meta{options}}
  This key adds the \meta{options} to the style |every node|. It mainly
  just a shorthand for the code |every node/.append style=|\meta{options}.

  The main use of this option is the install some options for the
  nodes \emph{inside} the matrix that should not apply to the matrix
  \emph{itself}.

\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [nodes={fill=blue!20,minimum size=5mm}]
  {
    \node {8}; & \node{1}; & \node {6}; \\
    \node {3}; & \node{5}; & \node {7}; \\
    \node {4}; & \node{9}; & \node {2}; \\
  };
\end{tikzpicture}
\end{codeexample}
\end{key}

The next set of styles can be used to change the appearance of certain
rows, columns, or cells. If more than one of these styles is defined,
they are executed in the below order (the |every cell| style is
executed before all of the below).
\begin{stylekey}{/tikz/column \meta{number}}
  This style is used for every cell in column \meta{number}.
\end{stylekey}

\begin{stylekey}{/tikz/every odd column}
  This style is used for every cell in an odd column.
\end{stylekey}

\begin{stylekey}{/tikz/every even column}
  This style is used for every cell in an even column.
\end{stylekey}

\begin{stylekey}{/tikz/row \meta{number}}
  This style is used for every cell in row \meta{number}.
\end{stylekey}

\begin{stylekey}{/tikz/every odd row}
  This style is used for every cell in an odd row.
\end{stylekey}

\begin{stylekey}{/tikz/every even row}
  This style is used for every cell in an even row.
\end{stylekey}

\begin{stylekey}{/tikz/row \meta{row number} column \meta{column number}}
  This style is used for the cell in row \meta{row number} and column
  \meta{column number}.
\end{stylekey}


\begin{codeexample}[]
\begin{tikzpicture}
  [row 1/.style={red},
   column 2/.style={green!50!black},
   row 3 column 3/.style={blue}]

  \matrix
  {
    \node {8}; & \node{1}; & \node {6}; \\
    \node {3}; & \node{5}; & \node {7}; \\
    \node {4}; & \node{9}; & \node {2}; \\
  };
\end{tikzpicture}
\end{codeexample}

You can use the |column |\meta{number} option to change the alignment
for different columns.

\begin{codeexample}[]
\begin{tikzpicture}
  [column 1/.style={anchor=base west},
   column 2/.style={anchor=base east},
   column 3/.style={anchor=base}]
  \matrix
  {
    \node {123}; & \node{456}; & \node {789}; \\
    \node {12}; & \node{45}; & \node {78}; \\
    \node {1}; & \node{4}; & \node {7}; \\
  };
\end{tikzpicture}
\end{codeexample}


In many matrices all cell pictures have nearly the same code. For
example, cells typically start with |\node{| and end |};|. The
following options allow you to execute such code in all cells:

\begin{key}{/tikz/execute at begin cell=\meta{code}}
  The code will be executed at the beginning of each nonempty cell.
\end{key}
\begin{key}{/tikz/execute at end cell=\meta{code}}
  The code will be executed at the end of each nonempty cell.
\end{key}
\begin{key}{/tikz/execute at empty cell=\meta{code}}
  The code will be executed inside each empty cell.
\end{key}

\begin{codeexample}[]
\begin{tikzpicture}
  [matrix of nodes/.style={
     execute at begin cell=\node\bgroup,
     execute at end cell=\egroup;%
   }]
  \matrix [matrix of nodes]
  {
    8 & 1 & 6 \\
    3 & 5 & 7 \\
    4 & 9 & 2 \\
  };
\end{tikzpicture}
\end{codeexample}
\begin{codeexample}[]
\begin{tikzpicture}
  [matrix of nodes/.style={
     execute at begin cell=\node\bgroup,
     execute at end cell=\egroup;,%
     execute at empty cell=\node{--};%
   }]
  \matrix [matrix of nodes]
  {
    8 & 1 &   \\
    3 &   & 7 \\
      &   & 2 \\
  };
\end{tikzpicture}
\end{codeexample}

The |matrix| library defines a number of styles that make use of the
above options.




\subsection{Anchoring a Matrix}

Since matrices are nodes, they can be anchored in the usual fashion
using the |anchor| option. However, there are two ways to influence
this placement further. First, the following option is often useful:

\begin{key}{/tikz/matrix anchor=\meta{anchor}}
  This option has the same effect as |anchor|, but the option applies
  only to the matrix itself, not to the cells inside. If you just say
  |anchor=north| as an option to the matrix node, all nodes inside
  matrix will also have this anchor, unless it is explicitly set
  differently for each node. By comparison, |matrix anchor| sets the
  anchor for the matrix, but for the nodes inside the value of
  |anchor| remain unchanged.

\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [matrix anchor=west] at (0,0)
  {
    \node {123}; \\ % still center anchor
    \node {12}; \\
    \node {1}; \\
  };
  \matrix [anchor=west] at (0,-2)
  {
    \node {123}; \\ % inherited west anchor
    \node {12}; \\
    \node {1}; \\
  };
\end{tikzpicture}
\end{codeexample}
\end{key}

The second way to anchor a matrix is to use \emph{an anchor of a node
  inside the matrix}. For this, the |anchor| option has a special
effect when given as an argument to a matrix:

\begin{key}{/tikz/anchor=\meta{anchor or node.anchor}}
  Normally, the argument of this option refers to anchor of the matrix
  node, which is the node than includes all of the stuff of the
  matrix. However, you can also provide an argument of the form
  \meta{node}|.|\meta{anchor} where \meta{node} must be node defined
  inside the matrix and \meta{anchor} is an anchor of this node. In
  this case, the whole matrix is shifted around in such a way that
  this particular anchor of this particular node lies at the |at|
  position of the matrix. The same is true for |matrix anchor|.

\begin{codeexample}[]
\begin{tikzpicture}
  \draw[help lines] (0,0) grid (3,2);
  \matrix[matrix anchor=inner node.south,anchor=base,row sep=3mm] at (1,1)
  {
    \node {a}; & \node             {b}; & \node {c}; & \node {d}; \\
    \node {a}; & \node(inner node) {b}; & \node {c}; & \node {d}; \\
    \node {a}; & \node             {b}; & \node {c}; & \node {d}; \\
  };
  \draw (inner node.south) circle (1pt);
\end{tikzpicture}
\end{codeexample}
\end{key}


\subsection{Considerations Concerning Active Characters}

Even though \tikzname\ seems to use |&| to separate cells, \pgfname\ actually
uses a different command to separate cells, namely the command
|\pgfmatrixnextcell| and using a normal |&| character will normally
fail. What happens is that, \tikzname\ makes |&| an active character
and then defines this character to be equal to
|\pgfmatrixnextcell|. In most situations this will work
nicely, but sometimes |&| cannot be made active; for
instance because the matrix is used in an argument of some macro or
the matrix contains nodes that contain normal |{tabular}|
environments. In this case you can use the following option to avoid
having to type |\pgfmatrixnextcell| each time:

\begin{key}{/tikz/ampersand replacement=\meta{macro name or empty}}
  If a macro name is provided, this macro will be defined to be equal
  to |\pgfmatrixnextcell| inside matrices and |&| will not be made
  active. For instance, you could say |ampersand replacement=\&| and
  then use \& to separate columns as in the following example:
\begin{codeexample}[]
\tikz
  \matrix [ampersand replacement=\&]
  {
    \draw (0,0)   circle (4mm); \& \node[rotate=10] {Hello};        \\
    \draw (0.2,0) circle (2mm); \& \fill[red]   (0,0) circle (3mm); \\
  };
\end{codeexample}
\end{key}


\subsection{Examples}

The following examples are adapted from code by Mark Wibrow. The first
two redraw pictures from Timothy van Zandt's PStricks documentation:

{\catcode`\|=12
\begin{codeexample}[]
\begin{tikzpicture}
  \matrix [matrix of math nodes,row sep=1cm]
  {
    |(U)| U &[2mm]                       &[8mm]    \\
            &      |(XZY)| X \times_Z Y  &      |(X)| X \\
            &      |(Y)|   Y             &      |(Z)| Z \\
  };
  \begin{scope}[every node/.style={midway,auto,font=\scriptsize}]
    \draw [double, dashed] (U)   -- node {$x$} (X);
    \draw                  (X)   -- node {$p$} (X -| XZY.east)
                           (X)   -- node {$f$} (Z)
                                 -- node {$g$} (Y)
                                 -- node {$q$} (XZY)
                                 -- node {$y$} (U);
   \end{scope}
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[]
\begin{tikzpicture}[>=stealth,->,shorten >=2pt,looseness=.5,auto]
  \matrix [matrix of math nodes,
           column sep={2cm,between origins},
           row sep={3cm,between origins},
           nodes={circle, draw, minimum size=7.5mm}]
  {
            & |(A)| A &         \\
    |(B)| B & |(E)| E & |(C)| C \\
            & |(D)| D           \\
  };
  \begin{scope}[every node/.style={font=\small\itshape}]
    \draw (A) to [bend left] node [midway]   {g} (B);
    \draw (B) to [bend left] node [midway]   {f} (A);
    \draw (D) --             node [midway]   {c} (B);
    \draw (E) --             node [midway]   {b} (B);
    \draw (E) --             node [near end] {a} (C);
    \draw [-,line width=8pt,draw=graphicbackground]
          (D) to [bend right, looseness=1] (A);
    \draw (D) to [bend right, looseness=1]
            node [near start] {b} node [near end] {e} (A);
  \end{scope}
\end{tikzpicture}
\end{codeexample}

\begin{codeexample}[]
\begin{tikzpicture}
  \matrix (network)
    [matrix of nodes,%
     nodes in empty cells,
     nodes={outer sep=0pt,circle,minimum size=4pt,draw},
     column sep={1cm,between origins},
     row sep={1cm,between origins}]
  {
                  &                &                 & \\
                  &                &                 & \\
    |[draw=none]| & |[xshift=1mm]| & |[xshift=-1mm]|   \\
  };
  \foreach \a in {1,...,4}{
    \draw (network-3-2) -- (network-2-\a);
    \draw (network-3-3) -- (network-2-\a);
    \draw [-stealth] ([yshift=5mm]network-1-\a.north) -- (network-1-\a);
    \foreach \b in {1,...,4}
      \draw (network-1-\a) -- (network-2-\b);
  }
  \draw [stealth-] ([yshift=-5mm]network-3-2.south) -- (network-3-2);
  \draw [stealth-] ([yshift=-5mm]network-3-3.south) -- (network-3-3);
\end{tikzpicture}
\end{codeexample}

The following example is adapted from code written by Kjell Magne
Fauske, which is based on the following paper: K.~Bossley, M.~Brown,
and C.~Harris, Neurofuzzy identification of an autonomous underwater
vehicle, \emph{International Journal of Systems Science}, 1999, 30, 901--913.

\begin{codeexample}[]
\begin{tikzpicture}
  [auto,
   decision/.style={diamond, draw=blue, thick, fill=blue!20,
                    text width=4.5em,align=flush center,
                    inner sep=1pt},
   block/.style   ={rectangle, draw=blue, thick, fill=blue!20,
                    text width=5em,align=center, rounded corners,
                    minimum height=4em},
   line/.style    ={draw, thick, -latex',shorten >=2pt},
   cloud/.style   ={draw=red, thick, ellipse,fill=red!20,
                    minimum height=2em}]

  \matrix [column sep=5mm,row sep=7mm]
  {
    % row 1
      \node [cloud] (expert)   {expert}; &
      \node [block] (init)     {initialize model}; &
      \node [cloud] (system)   {system}; \\
    % row 2
      & \node [block] (identify) {identify candidate model}; & \\
    % row 3
      \node [block] (update)   {update model};  &
      \node [block] (evaluate) {evaluate candidate models}; & \\
    % row 4
      & \node [decision] (decide) {is best candidate}; & \\
    % row 5
      & \node [block] (stop)      {stop}; & \\
  };
  \begin{scope}[every path/.style=line]
    \path          (init)     -- (identify);
    \path          (identify) -- (evaluate);
    \path          (evaluate) -- (decide);
    \path          (update)   |- (identify);
    \path          (decide)   -| node [near start] {yes} (update);
    \path          (decide)   -- node [midway] {no} (stop);
    \path [dashed] (expert)   -- (init);
    \path [dashed] (system)   -- (init);
    \path [dashed] (system)   |- (evaluate);
  \end{scope}
\end{tikzpicture}
\end{codeexample}
}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "pgfmanual"
%%% End: