Debian Codegraph @

texmf-dist/doc/latex/matrix-skeleton/matrix.skeleton.tex

raw
1 \documentclass{scrartcl}
2
3 \usepackage{tikz}
4 \usetikzlibrary{matrix.skeleton}
5 \usetikzlibrary{positioning}
6
7 \usepackage[justification=centering,labelfont={sf,bf,up},labelsep=period,font=small]{caption}
8 \captionsetup[figure]{position=bottom,singlelinecheck=false}
9 \usepackage[font=small,justification=centering]{subcaption}
10 \usepackage{float}
11 \floatstyle{komabelow}
12 \restylefloat{figure}
13
14 \usepackage{xspace}
15
16 \usepackage{hyperref}
17 \hypersetup{ colorlinks=true
18 , linkcolor=blue!75
19 , citecolor=black
20 , urlcolor=blue!75
21 }
22
23 \usepackage[noabbrev, capitalize]{cleveref}
24
25 \tikzset{highlight/.style={draw=#1!75, fill=#1!25, rounded corners=1pt}}
26 \newcommand\code\texttt
27 \newcommand{\TikZ}{Ti\textit{k}Z\xspace}
28
29 \title{\texttt{matrix.skeleton}'s Manual}
30 \author{Nicolas Dudebout}
31 \date{Version \versionnumber}
32
33 \begin{document}
34
35 \maketitle
36
37 \section{Introduction}
38
39 The \TikZ \code{matrix} library places nodes on a grid.
40 However, this grid is discarded after the nodes have been placed.
41 As a result, certain constructions involving multiple nodes become cumbersome.
42 The following two examples highlight some of the difficulties.
43
44 \subsection{Alignment Issues with \code{fit}}
45
46 The \code{fit} library is used to highlight a subset of nodes in a matrix.
47 If all the nodes in the matrix have the same dimension, as in~\cref{fig:highlighting_identical_dimensions}, \code{fit} produces the desired output.
48
49 \begin{figure}[h]
50 \centering
51
52 \begin{subfigure}{0.45\textwidth}
53 \centering
54 \begin{tikzpicture}
55 \matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
56 1 & 2 \\
57 3 & 4 \\
58 5 & 6 \\
59 };
60 \end{tikzpicture}
61 \caption{Input matrix}
62 \end{subfigure}
63 %
64 \begin{subfigure}{0.45\textwidth}
65 \centering
66 \begin{tikzpicture}
67 \matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
68 1 & 2 \\
69 3 & 4 \\
70 5 & 6 \\
71 };
72
73 \fitandstyle[background]{(m-1-1) (m-2-1)}{highlight = yellow}
74 \fitandstyle[background]{(m-1-2) (m-2-2)}{highlight = red}
75 \fitandstyle[background]{(m-3-1) (m-3-2)}{highlight = green}
76 \end{tikzpicture}
77 \caption{Desired output and result with \code{fit}}
78 \end{subfigure}
79
80 \caption{Highlighting in a matrix with nodes of identical dimensions}
81 \label{fig:highlighting_identical_dimensions}
82 \end{figure}
83
84 However, if the nodes have different heights and widths, as illustrated in~\cref{fig:highlighting_different_dimensions}, some alignment issues arise.
85
86 \begin{figure}[h]
87 \centering
88 \begin{subfigure}{0.3\textwidth}
89 \centering
90
91 \begin{tikzpicture}
92 \matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
93 1 & \frac{\frac{16}{4}}{1 + \frac{2}{2}} \\
94 2.999999 & 4 \\
95 5 & 6.000001 \\
96 };
97 \end{tikzpicture}
98 \caption{Input matrix}
99 \end{subfigure}
100 %
101 \begin{subfigure}{0.3\textwidth}
102 \centering
103
104 \begin{tikzpicture}
105 \matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt, label skeleton] {
106 1 & \frac{\frac{16}{4}}{1 + \frac{2}{2}} \\
107 2.999999 & 4 \\
108 5 & 6.000001 \\
109 };
110
111 \fitandstyle[background]{(m-cell-1-1) (m-cell-2-1)}{highlight = yellow}
112 \fitandstyle[background]{(m-cell-1-2) (m-cell-2-2)}{highlight = red}
113 \fitandstyle[background]{(m-cell-3-1) (m-cell-3-2)}{highlight = green}
114 \end{tikzpicture}
115 \caption{Desired output}
116 \end{subfigure}
117 %
118 \begin{subfigure}{0.3\textwidth}
119 \centering
120
121 \begin{tikzpicture}
122 \matrix (m) [matrix of math nodes, column sep = 3pt, row sep = 3pt] {
123 1 & \frac{\frac{16}{4}}{1 + \frac{2}{2}} \\
124 2.999999 & 4 \\
125 5 & 6.000001 \\
126 };
127
128 \fitandstyle[background]{(m-1-1) (m-2-1)}{highlight = yellow}
129 \fitandstyle[background]{(m-1-2) (m-2-2)}{highlight = red}
130 \fitandstyle[background]{(m-3-1) (m-3-2)}{highlight = green}
131 \end{tikzpicture}
132 \caption{Result with \code{fit}}
133 \end{subfigure}
134
135 \caption{Highlighting in a matrix with nodes of different dimensions}
136 \label{fig:highlighting_different_dimensions}
137 \end{figure}
138
139 These problems can be addressed using \code{minimum width} and \code{minimum height}.
140 However, adjusting manually these parameters in every matrix is a waste of time.
141
142 The \code{matrix.skeleton} library provides a clean solution through the use of nodes called~\code{cells}.
143 These \code{cells} and other skeleton nodes are described in~\cref{sec:skeleton}.
144
145 \subsection{Working with Rows and Columns}
146
147 The readability of a matrix can sometimes be improved by adding a background on every other row.
148 This simple task is not easily achievable with \code{matrix} alone.
149 The style \code{every odd column} only affects the nodes of the said columns.
150 There is no real column object to work with.
151
152 The \code{matrix.skeleton} library provides \TikZ styles to achieve this goal easily.
153 These styles are described in~\cref{sec:styling}
154
155 \section{Skeleton}
156 \label{sec:skeleton}
157
158 \subsection{Nodes}
159
160 \code{matrix.skeleton} works by positioning a set of nodes to recreate the \code{matrix} grid.
161 The eight types of such nodes are illustrated in~\cref{fig:skeleton_nodes}.
162
163 \begin{figure}[h]
164 \centering
165 \begin{subfigure}{0.3\textwidth}
166 \centering
167 \begin{tikzpicture}
168 \matrix (m) [draw, matrix of nodes, column sep=10pt, row sep=10pt, label skeleton] {
169 1 & 2 & 3 \\
170 4 & 5 & 6 \\
171 7 & 8 & 9 \\
172 };
173
174 \fitandstyle[background]{(m-cell-1-1)}{fill=yellow!30}
175 \fitandstyle[background]{(m-row-2)}{fill=red!25}
176 \fitandstyle[background]{(m-column-3)}{fill=green!25, opacity=.75}
177 \end{tikzpicture}
178 \caption{\textcolor{yellow!80!orange}{Cell}, \textcolor{red!50}{row}, and \textcolor{green!60}{column}}
179 \end{subfigure}
180 %
181 \begin{subfigure}{0.3\textwidth}
182 \centering
183 \begin{tikzpicture}
184 \matrix (m) [draw, matrix of nodes, column sep=10pt, row sep=10pt, label skeleton] {
185 1 & 2 & 3 \\
186 4 & 5 & 6 \\
187 7 & 8 & 9 \\
188 };
189
190 \fitandstyle[background]{(m-inter-row-1)}{fill=red!25}
191 \fitandstyle[background]{(m-inter-column-2)}{fill=green!25, opacity=.75}
192 \end{tikzpicture}
193 \caption{\textcolor{red!50}{Inter-row} and \textcolor{green!60}{inter-column}}
194 \end{subfigure}
195 %
196 \begin{subfigure}{0.3\textwidth}
197 \centering
198 \begin{tikzpicture}
199 \matrix (m) [draw, matrix of nodes, column sep=10pt, row sep=10pt, label skeleton] {
200 1 & 2 & 3 \\
201 4 & 5 & 6 \\
202 7 & 8 & 9 \\
203 };
204 \fitandstyle[background]{(m-tiling-cell-1-1)}{fill=yellow!30}
205 \fitandstyle[background]{(m-tiling-row-2)}{fill=red!25}
206 \fitandstyle[background]{(m-tiling-column-3)}{fill=green!25, opacity=.75}
207 \end{tikzpicture}
208 \caption{\textcolor{yellow!80!orange}{Tiling cell}, \textcolor{red!50}{tiling row}, and \textcolor{green!60}{tiling column}}
209 \end{subfigure}
210 \caption{Skeleton nodes}
211 \label{fig:skeleton_nodes}
212 \end{figure}
213
214 \subsection{Using \code{matrix.skeleton}}
215
216 The recommended way of using \code{matrix.skeleton} is through \TikZ.
217 First, load the library with:
218 \begin{verbatim}
219 \usetikzlibrary{matrix.skeleton}
220 \end{verbatim}
221 Then add an option to your matrix:
222 \begin{verbatim}
223 \matrix (m) [label skeleton] {...};
224 \end{verbatim}
225 This creates a set of nodes that can be used for styling.
226 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}.
227
228 \section{Styling}
229 \label{sec:styling}
230
231 The skeleton nodes are PGF nodes not meant to be styled.
232 Styles should be applied to nodes whose shapes depend on the skeleton ones.
233
234 \subsection{Macros}
235
236 Styling in \code{matrix.skeleton} is done with the~\code{fit} library.
237 The following macro creates a \code{fit} node with the specified style:
238 \begin{verbatim}
239 \fitandstyle{(m-cell-1-1) (m-cell-2-2)}{draw=red};
240 \end{verbatim}
241
242 It takes an optional argument to place the node in a \code{pgfonlayer} environment:
243 \begin{verbatim}
244 \fitandstyle[background]{(m-cell-1-1) (m-cell-2-2)}{fill=red};
245 \end{verbatim}
246
247 \subsection{\TikZ \code{matrix} Options}
248
249 Common styling options are also provided as \TikZ options.
250 These options call~\code{label skeleton} before styling the appropriate nodes.
251 They take the following form:
252 \begin{verbatim}
253 \matrix (m) [style odd rows = {draw=red}] {...};
254 \end{verbatim}
255
256 \begin{verbatim}
257 \matrix (m) [style odd tiling rows = {draw=red}] {...};
258 \end{verbatim}
259
260 \begin{verbatim}
261 \matrix (m) [style grid = {draw}] {...};
262 \end{verbatim}
263
264 \begin{verbatim}
265 \matrix (m) [style tiling grid = {draw}] {...};
266 \end{verbatim}
267
268 All of these options have an \code{on layer} variant taking the following form:
269 \begin{verbatim}
270 \matrix (m) [style odd rows on layer = {background}{fill=red}] {...};
271 \end{verbatim}
272
273 \section{Examples}
274
275 The following examples illustrate the styling capabilities offered by \code{matrix.skeleton}.
276
277 \subsection{Grid}
278
279 \begin{figure}[h]
280 \centering
281 \begin{tikzpicture}
282 \matrix (m) [matrix of math nodes, style contour = {draw, very thick}, style grid = {draw, thin}] {
283 1 & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3 \\
284 3.999999 & 5 & 6 \\
285 7 & 8.000001 & 3 + 3 + 3 \\
286 };
287 \end{tikzpicture}
288 \end{figure}
289
290 \begin{verbatim}
291 \begin{tikzpicture}
292 \matrix (m) [matrix of math nodes, style contour = {draw, very thick},
293 style grid = {draw, thin}] {
294 1 & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3 \\
295 3.999999 & 5 & 6 \\
296 7 & 8.000001 & 3 + 3 + 3 \\
297 };
298 \end{tikzpicture}
299 \end{verbatim}
300
301 \subsection{Rows}
302
303 \begin{figure}[h]
304 \centering
305 \begin{tikzpicture}
306 \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}] {
307 1 & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3 \\
308 3.999999 & 5 & 6 \\
309 7 & 8.000001 & 3 + 3 + 3 \\
310 };
311
312 \fitandstyle{(m-inter-row-1)}{fill=red!25}
313 \fitandstyle{(m-inter-row-2)}{fill=red!25}
314 \end{tikzpicture}
315 \end{figure}
316
317 \begin{verbatim}
318 \begin{tikzpicture}
319 \matrix (m) [matrix of math nodes, row sep = 10pt,
320 style odd rows on layer={background}{fill=green!25},
321 style even rows on layer={background}{fill=yellow!30}] {
322 1 & \frac{\frac{16}{4}}{1 + \frac{2}{2}} & 3 \\
323 3.999999 & 5 & 6 \\
324 7 & 8.000001 & 3 + 3 + 3 \\
325 };
326
327 \fitandstyle{(m-inter-row-1)}{fill=red!25}
328 \fitandstyle{(m-inter-row-2)}{fill=red!25}
329 \end{tikzpicture}
330 \end{verbatim}
331
332 \subsection{Checker Board}
333
334 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?}
335
336 \begin{figure}[h]
337 \centering
338 \begin{tikzpicture}
339 \matrix (m) [draw, matrix of nodes, row sep=2mm, column sep=1mm, nodes={draw, thick, circle, inner sep=1pt}, label skeleton] {
340 & 1 & &[2mm]|[gray]|1\\
341 & & 2 &|[gray]|2\\
342 |[gray]|2 & & &|[gray]|2\\[4mm]
343 3 & & & 3\\
344 };
345 \foreach \row in {1, ..., 4} {
346 \foreach \col in {1, ..., 4} {
347 \pgfmathparse{Mod(\row + \col, 2) ? "red!25" : "yellow!30"}
348 \colorlet{squarebg}{\pgfmathresult}
349 \fitandstyle[background]{(m-tiling-cell-\row-\col)}{fill = squarebg}
350 }
351 }
352 \end{tikzpicture}
353 \end{figure}
354
355 \newpage
356
357 \begin{verbatim}
358 \begin{tikzpicture}
359 \matrix (m) [draw, matrix of nodes, row sep=2mm, column sep=1mm,
360 nodes={draw, thick, circle, inner sep=1pt}, label skeleton] {
361 & 1 & &[2mm]|[gray]|1\\
362 & & 2 &|[gray]|2\\
363 |[gray]|2 & & &|[gray]|2\\[4mm]
364 3 & & & 3\\
365 };
366 \foreach \row in {1, ..., 4} {
367 \foreach \col in {1, ..., 4} {
368 \pgfmathparse{Mod(\row + \col, 2) ? "red!25" : "yellow!30"}
369 \colorlet{squarebg}{\pgfmathresult}
370 \fitandstyle[background]{(m-tiling-cell-\row-\col)}{fill = squarebg}
371 }
372 }
373 \end{tikzpicture}
374 \end{verbatim}
375
376 \section{Internals}
377
378 \code{matrix.skeleton} was heavily inspired by \href{http://tex.stackexchange.com/users/86/andrew-stacey}{Andrew Stacey}'s \code{matrixcells} \LaTeX{} package.
379 It has three distinctive features.
380 First, it works with any \code{anchor}.
381 Second, it provides finer control with respect to \code{row sep}, \code{column sep}, and \code{inner sep}.
382 Third, the skeleton node positioning relies only on \TeX{} and PGF, not on \LaTeX{} or \TikZ.
383
384 \code{matrixcells} properly aligns its \code{cells} when the node \code{anchor} is \code{base}.
385 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}.
386 This shortcoming is the result of some loss of information in \code{pgfmodulematrix.code.tex}.
387 A dimension used during the placement of nodes is overwritten.
388 Therefore, this information is not available to build the grid.
389 In \code{matrixcells}, this lost dimension is reconstructed as the average of two other dimensions.
390 This method only gives the right dimension when the nodes are anchored at \code{base}.
391 To always get proper alignment, the~\code{pgfmodulematrix.code.tex} macro erasing the dimension was rewritten.
392 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.
393
394 \code{matrixcells} only provides \code{cells} corresponding the \code{tiling-cells} in \code{matrix.skeleton}.
395 This tiling behavior is sometimes desired.
396 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.
397
398 \section{Acknowledgments}
399
400 Onur Tolga Şehitoğlu implemented support for multiple matrices.
401
402 \end{document}