summaryrefslogtreecommitdiff
path: root/graphics/pgf/contrib/matrix-skeleton/manual.tex
blob: 14ee41c69a6213d52ec18ad14d8128cced11ad35 (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
\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{matrix.skeleton}

\usepackage[justification=centering,labelfont={sf,bf,up},labelsep=period,font=small]{caption}
\captionsetup[figure]{position=bottom,singlelinecheck=false}
\usepackage[font=small,justification=centering]{subcaption}
\usepackage{float}
\floatstyle{komabelow}
\restylefloat{figure}

\usepackage{xspace}

\usepackage{hyperref}
\hypersetup{ colorlinks=true
           , linkcolor=blue!75
           , citecolor=black
           , urlcolor=blue!75
           }

\usepackage[noabbrev, capitalize]{cleveref}

\tikzset{highlight/.style={draw=#1!75, fill=#1!25, rounded corners=1pt}}
\newcommand\code\texttt
\newcommand{\TikZ}{Ti\textit{k}Z\xspace}

\title{\texttt{matrix.skeleton}'s Manual}
\author{Nicolas Dudebout}
\date{}

\begin{document}

\maketitle

\section{Introduction}

The \TikZ \code{matrix} library places nodes on a grid.
However, this grid is discarded after the nodes have been placed.
As a result, certain constructions involving multiple nodes become cumbersome.
The following two examples highlight some of the difficulties.

\subsection{Alignment Issues with \code{fit}}

The \code{fit} library is used to highlight a subset of nodes in a matrix.
If all the nodes in the matrix have the same dimension, as in~\cref{fig:highlighting_identical_dimensions}, \code{fit} produces the desired output.

\begin{figure}[h]
\centering

\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
1 & 2 \\
3 & 4 \\
5 & 6 \\
};
\end{tikzpicture}
\caption{Input matrix}
\end{subfigure}
%
\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
1 & 2 \\
3 & 4 \\
5 & 6 \\
};

\fitandstyle[background]{(m-1-1) (m-2-1)}{highlight = yellow}
\fitandstyle[background]{(m-1-2) (m-2-2)}{highlight = red}
\fitandstyle[background]{(m-3-1) (m-3-2)}{highlight = green}
\end{tikzpicture}
\caption{Desired output and result with \code{fit}}
\end{subfigure}

\caption{Highlighting in a matrix with nodes of identical dimensions}
\label{fig:highlighting_identical_dimensions}
\end{figure}

However, if the nodes have different heights and widths, as illustrated in~\cref{fig:highlighting_different_dimensions}, some alignment issues arise.

\begin{figure}[h]
\centering
\begin{subfigure}{0.3\textwidth}
\centering

\begin{tikzpicture}
\matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
1        & \frac{\frac{16}{4}}{1 + \frac{2}{2}} \\
2.999999 & 4                                    \\
5        & 6.000001                             \\
};
\end{tikzpicture}
\caption{Input matrix}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\centering

\begin{tikzpicture}
\matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt, label skeleton] {
1        & \frac{\frac{16}{4}}{1 + \frac{2}{2}} \\
2.999999 & 4                                    \\
5        & 6.000001                             \\
};

\fitandstyle[background]{(m-cell-1-1) (m-cell-2-1)}{highlight = yellow}
\fitandstyle[background]{(m-cell-1-2) (m-cell-2-2)}{highlight = red}
\fitandstyle[background]{(m-cell-3-1) (m-cell-3-2)}{highlight = green}
\end{tikzpicture}
\caption{Desired output}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\centering

\begin{tikzpicture}
\matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
1        & \frac{\frac{16}{4}}{1 + \frac{2}{2}} \\
2.999999 & 4                                    \\
5        & 6.000001                             \\
};

\fitandstyle[background]{(m-1-1) (m-2-1)}{highlight = yellow}
\fitandstyle[background]{(m-1-2) (m-2-2)}{highlight = red}
\fitandstyle[background]{(m-3-1) (m-3-2)}{highlight = green}
\end{tikzpicture}
\caption{Result with \code{fit}}
\end{subfigure}

\caption{Highlighting in a matrix with nodes of different dimensions}
\label{fig:highlighting_different_dimensions}
\end{figure}

These problems can be addressed using \code{minimum width} and \code{minimum height}.
However, adjusting manually these parameters in every matrix is a waste of time.

The \code{matrix.skeleton} library provides a clean solution through the use of nodes called~\code{cells}.
These \code{cells} and other skeleton nodes are described in~\cref{sec:skeleton}.

\subsection{Working with Rows and Columns}

The readability of a matrix can sometimes be improved by adding a background on every other row.
This simple task is not easily achievable with \code{matrix} alone.
The style \code{every odd column} only affects the nodes of the said columns.
There is no real column object to work with.

The \code{matrix.skeleton} library provides \TikZ styles to achieve this goal easily.
These styles are described in~\cref{sec:styling}

\section{Skeleton}
\label{sec:skeleton}

\subsection{Nodes}

\code{matrix.skeleton} works by positioning a set of nodes to recreate the \code{matrix} grid.
The eight types of such nodes are illustrated in~\cref{fig:skeleton_nodes}.

\begin{figure}[h]
\centering
\begin{subfigure}{0.3\textwidth}
\centering
\begin{tikzpicture}
\matrix (m) [draw, matrix of nodes, column sep=10pt, row sep=10pt, label skeleton] {
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
};

\fitandstyle[background]{(m-cell-1-1)}{fill=yellow!30}
\fitandstyle[background]{(m-row-2)}{fill=red!25}
\fitandstyle[background]{(m-column-3)}{fill=green!25, opacity=.75}
\end{tikzpicture}
\caption{\textcolor{yellow!80!orange}{Cell}, \textcolor{red!50}{row}, and \textcolor{green!60}{column}}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\centering
\begin{tikzpicture}
\matrix (m) [draw, matrix of nodes, column sep=10pt, row sep=10pt, label skeleton] {
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
};

\fitandstyle[background]{(m-inter-row-1)}{fill=red!25}
\fitandstyle[background]{(m-inter-column-2)}{fill=green!25, opacity=.75}
\end{tikzpicture}
\caption{\textcolor{red!50}{Inter-row} and \textcolor{green!60}{inter-column}}
\end{subfigure}
%
\begin{subfigure}{0.3\textwidth}
\centering
\begin{tikzpicture}
\matrix (m) [draw, matrix of nodes, column sep=10pt, row sep=10pt, label skeleton] {
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
};
\fitandstyle[background]{(m-tiling-cell-1-1)}{fill=yellow!30}
\fitandstyle[background]{(m-tiling-row-2)}{fill=red!25}
\fitandstyle[background]{(m-tiling-column-3)}{fill=green!25, opacity=.75}
\end{tikzpicture}
\caption{\textcolor{yellow!80!orange}{Tiling cell}, \textcolor{red!50}{tiling row}, and \textcolor{green!60}{tiling column}}
\end{subfigure}
\caption{Skeleton nodes}
\label{fig:skeleton_nodes}
\end{figure}

\subsection{Using \code{matrix.skeleton}}

The recommended way of using \code{matrix.skeleton} is through \TikZ.
First, load the library with:
\begin{verbatim}
  \usetikzlibrary{matrix.skeleton}
\end{verbatim}
Then add an option to your matrix:
\begin{verbatim}
  \matrix (m) [label skeleton] {...};
\end{verbatim}
This creates a set of nodes that can be used for styling.
For example, the nodes illustrated in~\cref{fig:skeleton_nodes} are named: \code{m-cell-1-1}, \code{m-row-2}, \code{m-column-3}, \code{m-inter-row-1}, \code{m-inter-column-2}, \code{m-tiling-cell-1}, \code{m-tiling-row-2}, and \code{m-tiling-column-3}.

\section{Styling}
\label{sec:styling}

The skeleton nodes are PGF nodes not meant to be styled.
Styles should be applied to nodes whose shapes depend on the skeleton ones.

\subsection{Macros}

Styling in \code{matrix.skeleton} is done with the~\code{fit} library.
The following macro creates a \code{fit} node with the specified style:
\begin{verbatim}
  \fitandstyle{(m-cell-1-1) (m-cell-2-2)}{draw=red};
\end{verbatim}

It takes an optional argument to place the node in a \code{pgfonlayer} environment:
\begin{verbatim}
  \fitandstyle[background]{(m-cell-1-1) (m-cell-2-2)}{fill=red};
\end{verbatim}

\subsection{\TikZ \code{matrix} Options}

Common styling options are also provided as \TikZ options.
These options call~\code{label skeleton} before styling the appropriate nodes.
They take the following form:
\begin{verbatim}
  \matrix (m) [style odd rows = {draw=red}] {...};
\end{verbatim}

\begin{verbatim}
  \matrix (m) [style odd tiling rows = {draw=red}] {...};
\end{verbatim}

\begin{verbatim}
  \matrix (m) [style grid = {draw}] {...};
\end{verbatim}

\begin{verbatim}
  \matrix (m) [style tiling grid = {draw}] {...};
\end{verbatim}

All of these options have an \code{on layer} variant taking the following form:
\begin{verbatim}
  \matrix (m) [style odd rows on layer = {background}{fill=red}] {...};
\end{verbatim}

\section{Examples}

The following examples illustrate the styling capabilities offered by \code{matrix.skeleton}.

\subsection{Grid}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, style contour = {draw, very thick}, style grid = {draw, thin}] {
1        & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3         \\
3.999999 & 5                                    & 6         \\
7        & 8.000001                             & 3 + 3 + 3 \\
};
\end{tikzpicture}
\end{figure}

\begin{verbatim}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, style contour = {draw, very thick},
             style grid = {draw, thin}] {
1        & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3         \\
3.999999 & 5                                    & 6         \\
7        & 8.000001                             & 3 + 3 + 3 \\
};
\end{tikzpicture}
\end{verbatim}

\subsection{Rows}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep = 10pt, style odd rows on layer={background}{fill=green!25}, style even rows on layer={background}{fill=yellow!30}] {
1        & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3         \\
3.999999 & 5                                    & 6         \\
7        & 8.000001                             & 3 + 3 + 3 \\
};

\fitandstyle{(m-inter-row-1)}{fill=red!25}
\fitandstyle{(m-inter-row-2)}{fill=red!25}
\end{tikzpicture}
\end{figure}

\begin{verbatim}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep = 10pt,
             style odd rows on layer={background}{fill=green!25},
             style even rows on layer={background}{fill=yellow!30}] {
1        & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3         \\
3.999999 & 5                                    & 6         \\
7        & 8.000001                             & 3 + 3 + 3 \\
};

\fitandstyle{(m-inter-row-1)}{fill=red!25}
\fitandstyle{(m-inter-row-2)}{fill=red!25}
\end{tikzpicture}
\end{verbatim}

\subsection{Checker Board}

This example is inspired by the following \href{http://tex.stackexchange.com}{\TeX{} - \LaTeX{} Stack Exchange} question: \href{http://tex.stackexchange.com/questions/14061/how-can-i-set-the-background-color-of-the-rows-and-columns-of-a-matrix-node-in-t}{How can I set the background color of the rows and columns of a matrix node in Tikz?}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\matrix (m) [draw, matrix of nodes, row sep=2mm, column sep=1mm, nodes={draw, thick, circle, inner sep=1pt}, label skeleton] {
  & 1 & &[2mm]|[gray]|1\\
  & & 2 &|[gray]|2\\
  |[gray]|2 & & &|[gray]|2\\[4mm]
  3 & & & 3\\
};
\foreach \row in {1, ..., 4} {
  \foreach \col in {1, ..., 4} {
    \pgfmathparse{Mod(\row + \col, 2) ? "red!25" : "yellow!30"}
    \colorlet{squarebg}{\pgfmathresult}
    \fitandstyle[background]{(m-tiling-cell-\row-\col)}{fill = squarebg}
  }
}
\end{tikzpicture}
\end{figure}

\newpage

\begin{verbatim}
\begin{tikzpicture}
\matrix (m) [draw, matrix of nodes, row sep=2mm, column sep=1mm,
             nodes={draw, thick, circle, inner sep=1pt}, label skeleton] {
  & 1 & &[2mm]|[gray]|1\\
  & & 2 &|[gray]|2\\
  |[gray]|2 & & &|[gray]|2\\[4mm]
  3 & & & 3\\
};
\foreach \row in {1, ..., 4} {
  \foreach \col in {1, ..., 4} {
    \pgfmathparse{Mod(\row + \col, 2) ? "red!25" : "yellow!30"}
    \colorlet{squarebg}{\pgfmathresult}
    \fitandstyle[background]{(m-tiling-cell-\row-\col)}{fill = squarebg}
  }
}
\end{tikzpicture}
\end{verbatim}

\section{Internals}

\code{matrix.skeleton} was heavily inspired by \href{http://tex.stackexchange.com/users/86/andrew-stacey}{Andrew Stacey}'s \code{matrixcells} \LaTeX{} package.
It has three distinctive features.
First, it works with any \code{anchor}.
Second, it provides finer control with respect to \code{row sep}, \code{column sep}, and \code{inner sep}.
Third, the skeleton node positioning relies only on \TeX{} and PGF, not on \LaTeX{} or \TikZ.

\code{matrixcells} properly aligns its \code{cells} when the node \code{anchor} is \code{base}.
However, when the alignment is different it runs into problems, as exposed in the following \href{http://tex.stackexchange.com}{\TeX{} - \LaTeX{} Stack Exchange} question: \href{http://tex.stackexchange.com/questions/128045/matrixcells-problem-with-the-y-axis-only}{Matrixcells problem with the y-axis only}.
This shortcoming is the result of some loss of information in \code{pgfmodulematrix.code.tex}.
A dimension used during the placement of nodes is overwritten.
Therefore, this information is not available to build the grid.
In \code{matrixcells}, this lost dimension is reconstructed as the average of two other dimensions.
This method only gives the right dimension when the nodes are anchored at \code{base}.
To always get proper alignment, the~\code{pgfmodulematrix.code.tex} macro erasing the dimension was rewritten.
Following \href{http://tex.stackexchange.com/users/3235/percusse}{\code{@percusse}}'s recommendation this change is transparent to the user and does not require updating PGF/\TikZ.

\code{matrixcells} only provides \code{cells} corresponding the \code{tiling-cells} in \code{matrix.skeleton}.
This tiling behavior is sometimes desired.
However, it can result in unexpected behaviors when: using a non-base \code{anchor}, using \code{row sep} or \code{column sep}, or when working on boundary nodes.

\end{document}