~tex-sx/tex-sx/development

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
% \iffalse meta-comment
%<*internal>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
Copyright (C) 2011 by Clemens Koppensteiner
-------------------------------------------

This file may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either version 1.3
of this license or (at
 your option) any later version.
The latest version of this license is in:

   http://www.latex-project.org/lppl.txt

and version 1.3 or later is part of all distributions of LaTeX
version 2005/12/01 or later.
\endpreamble
\generate{\file{tikzlibraryshapes.tex-sx.code.tex} {\from{pgflibraryshapes.tex-sx.dtx}{tikzlibraryshapes.tex-sx}}}
\generate{\file{pgflibraryshapes.tex-sx.code.tex}  {\from{pgflibraryshapes.tex-sx.dtx}{pgflibraryshapes}}}
%</install>
%<install>\endbatchfile
%<*internal>
\generate{
  \file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{csquotes}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{shapes.tex-sx}
\usepackage[margin=3cm]{geometry}
\usepackage[numbered]{hypdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\tikzset{
  shape example/.style={
    color=black!30,
    draw,
    fill=yellow!30,
    line width=.5cm,
    inner xsep=2.5cm,
    inner ysep=0.5cm}
}
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{TikZ/PGF node shapes form TeX.SX --- version 0.2}
% \author{TeX.SX}
% \maketitle
% 
% \changes{v0.1}{2011/04/23}{The grid shape}
% \changes{v0.2}{2011/06/24}{Additional anchors for the rectangle shape}
%
% \section{Usage}
%
% The package is provided in form of a PGF library.
% It can be loaded with
%\begin{verbatim}
%\usetikzlibrary{shapes.tex-sx}
%\end{verbatim}
% or
%\begin{verbatim}
%\usepgflibrary{shapes.tex-sx}
%\end{verbatim}
% or the equivalent ConTeXt commands.
%
% \section{The \texttt{grid} shape}
% \changes{v0.3}{2011/07/06}{Rename \texttt{/pgf/grid lines} to \texttt{/tsx/grid lines}.}
% \changes{v0.3}{2011/07/06}{Make it possible to set the horizontal and vertical lines in the \texttt{grid} shape independently.}
% 
% \DescribeMacro{grid}^^A
% The \texttt{grid} shape is a rectangular shape that is subdivided by a grid.
% It was created by Clemens Koppensteiner in response to the question
% \enquote{\href{http://tex.stackexchange.com/questions/14846/drawing-a-tree-of-grids}{Drawing a tree of grids}}.
% The original use-case are trees of game states. 
% \DescribeMacro{/tsx/grid lines}^^A
% The number of subdivisions of the rectangle can
% be set with the \texttt{/tsx/grid lines} option (the default is two lines in
% each direction, resulting in nine smaller rectangles). 
% If only one number is given as argument to the option, the number of lines in vertical and horizontal direction is the same.
% To set the number of lines independently, use \texttt{/tsx/grid lines=\meta{horizontal lines} and \meta{vertical lines}}.
% 
% In addition to the anchors inherited from the \texttt{rectangle} shape,
% the \texttt{grid} shape provides anchors to access the centers of the smaller
% rectangles and the points of intersections of the grid lines. The anchors
% are demonstrated in Figure \ref{fig:grid_anchors}.
% \begin{figure}[htbp]
%   \centering\Huge
%   \begin{tikzpicture}
%       \node[name=s,shape=grid,shape example,minimum width=6cm, minimum height=4cm, /tsx/grid lines=2] {grid};
%       \foreach \anchor/\placement in
%       {south west/below left, 0 0/right, 3 3/left, north east/above right,
%        1 2/above, center 1 1/above, center 3 2/below}
%        \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
%           node[\placement=0.33ex,inner sep=0] {\scriptsize\texttt{(s.\anchor)}};
%   \end{tikzpicture}
%   \caption{Anchors of the \texttt{grid} shape.}
%   \label{fig:grid_anchors}
% \end{figure}
%
% \section{More anchors for the \texttt{rectangle} shape}
%
% In response to the question 
% \enquote{\href{http://tex.stackexchange.com/q/14769/}{Add more anchors to standard TikZ nodes}},
% Martin Scharrer posted code to add anchors to existing shapes and 
% demonstrated the idea by adding anchors to the rectangle node.
% Figure \ref{fig:new_rectangle_anchors} demonstrates the new anchors.
% \begin{figure}[htbp]
%   \centering\Huge
%   \begin{tikzpicture}
%       \node[name=s,shape=rectangle,shape example] {Rectangle\vrule width 1pt height 2cm};
%       \foreach \anchor/\placement in
%       {north west/above left, north north west/above, north/above,
%        north north east/above, north east/above right,
%        east north east/right, east/right, east south east/right, 
%        south east/below right, south south east/below, south/below, 
%        south south west/below, south west/below left, west south west/left,
%        west/left,  west north west/left}
%        \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
%           node[\placement] {\scriptsize\texttt{(s.\anchor)}};
%   \end{tikzpicture}
%   \caption{The new anchors for the \texttt{rectangle} shape.}
%   \label{fig:new_rectangle_anchors}
% \end{figure}
% Additionally, a \texttt{size} anchor is added that returns the width and
% height of the rectangle. This is useful for constructions like
%\begin{verbatim}
%\path let \y1 = (somenode.size) in node { width=\x1, height=\y1 };
%\end{verbatim}
% For easier typing of the anchor names, the shorthands \texttt{ne}, 
% \texttt{nne}, etc.\ are defined. 
% 
% To add the anchors to the shapes the new macro \cs{pgfaddtoshape} is used.
% See the section \ref{sec:impl:pgfaddtoshape} for details.
%
% \StopEventually{\PrintChanges}
% \section{Implementation}
%
% \subsection{\texttt{pgflibrary.shapes.tex-sx}}
%
% \iffalse
%<*pgflibraryshapes>
% \fi
% 
% \subsubsection{Macros to add code to already existing shapes}
% \label{sec:impl:pgfaddtoshape}^^A
% Martin Scharrer (http://tex.stackexchange.com/questions/14769/add-more-anchors-to-standard-tikz-nodes/14772\#14772)
%     \begin{macrocode}
\def\pgfaddtoshape#1#2{
  \begingroup
  \def\shape@name{#1}
  \let\anchor\pgf@sh@anchor
  #2
  \endgroup
}

\def\useanchor#1#2{\csname pgf@anchor@#1@#2\endcsname}

\def\anchorlet#1#2{%
    \global\expandafter
    \let\csname pgf@anchor@\shape@name @#1\expandafter\endcsname
    \csname pgf@anchor@\shape@name @#2\endcsname
}
\def\anchoralias#1#2{%
    \expandafter
    \gdef\csname pgf@anchor@\shape@name @#1\expandafter\endcsname
    \expandafter{\csname pgf@anchor@\shape@name @#2\endcsname}%
}
%    \end{macrocode}
% \subsubsection{More anchors for the rectangle shape}
%
% Martin Scharrer (http://tex.stackexchange.com/questions/14769/add-more-anchors-to-standard-tikz-nodes/14772\#14772)
% Some documentation text.
%
%
%    \begin{macrocode}
\def\@shiftback#1#2#3#4#5#6{%
    \advance\pgf@x by -#5\relax
    \advance\pgf@y by -#6\relax
}

\pgfaddtoshape{rectangle}{%
  \anchor{west south west}{%
    \pgf@process{\northeast}%
    \pgf@ya=.5\pgf@y%
    \pgf@process{\southwest}%
    \pgf@y=1.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
    \pgf@y=.5\pgf@y%
  }%
  \anchor{west north west}{%
    \pgf@process{\northeast}%
    \pgf@ya=1.5\pgf@y%
    \pgf@process{\southwest}%
    \pgf@y=.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
    \pgf@y=.5\pgf@y%
  }%
  \anchor{east north east}{%
    \pgf@process{\southwest}%
    \pgf@ya=.5\pgf@y%
    \pgf@process{\northeast}%
    \pgf@y=1.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
    \pgf@y=.5\pgf@y%
  }%
  \anchor{east south east}{%
    \pgf@process{\southwest}%
    \pgf@ya=1.5\pgf@y%
    \pgf@process{\northeast}%
    \pgf@y=.5\pgf@y%
    \advance\pgf@y by \pgf@ya%
    \pgf@y=.5\pgf@y%
  }%
  \anchor{north north west}{%
    \pgf@process{\southwest}%
    \pgf@xa=1.5\pgf@x%
    \pgf@process{\northeast}%
    \pgf@x=.5\pgf@x%
    \advance\pgf@x by \pgf@xa%
    \pgf@x=.5\pgf@x%
  }%
  \anchor{north north east}{%
    \pgf@process{\southwest}%
    \pgf@xa=.5\pgf@x%
    \pgf@process{\northeast}%
    \pgf@x=1.5\pgf@x%
    \advance\pgf@x by \pgf@xa%
    \pgf@x=.5\pgf@x%
  }%
  \anchor{south south west}{%
    \pgf@process{\northeast}%
    \pgf@xa=.5\pgf@x%
    \pgf@process{\southwest}%
    \pgf@x=1.5\pgf@x%
    \advance\pgf@x by \pgf@xa%
    \pgf@x=.5\pgf@x%
  }%
  \anchor{south south east}{%
    \pgf@process{\northeast}%
    \pgf@xa=1.5\pgf@x%
    \pgf@process{\southwest}%
    \pgf@x=.5\pgf@x%
    \advance\pgf@x by \pgf@xa%
    \pgf@x=.5\pgf@x%
  }%
  \anchor{width}{%
    \useanchor{rectangle}{west}%
    \pgf@xc=\pgf@x
    \useanchor{rectangle}{east}%
    \advance\pgf@x by -\pgf@xc
    \pgf@y=\z@
    \edef\pgf@temp{\csname pgf@sh@nt@\pgfreferencednodename\endcsname}%
    \expandafter\@shiftback\pgf@temp
  }
  \anchor{height}{%
    \useanchor{rectangle}{south}%
    \pgf@yc=\pgf@y
    \useanchor{rectangle}{north}%
    \advance\pgf@y by -\pgf@yc
    \pgf@x=\z@
    \edef\pgf@temp{\csname pgf@sh@nt@\pgfreferencednodename\endcsname}%
    \expandafter\@shiftback\pgf@temp
  }
  \anchor{size}{%
    \useanchor{rectangle}{south west}%
    \pgf@xc=\pgf@x
    \pgf@yc=\pgf@y
    \useanchor{rectangle}{north east}%
    \advance\pgf@x by -\pgf@xc
    \advance\pgf@y by -\pgf@yc
    \edef\pgf@temp{\csname pgf@sh@nt@\pgfreferencednodename\endcsname}%
    \expandafter\@shiftback\pgf@temp
  }
}

\pgfaddtoshape{rectangle}{%
  \anchorlet{se}{south east}%
  \anchorlet{sw}{south west}%
  \anchorlet{ne}{north east}%
  \anchorlet{nw}{north west}%
  \anchorlet{wsw}{west south west}%
  \anchorlet{wnw}{west north west}%
  \anchorlet{ene}{east north east}%
  \anchorlet{ese}{east south east}%
  \anchorlet{nnw}{north north west}%
  \anchorlet{nne}{north north east}%
  \anchorlet{ssw}{south south west}%
  \anchorlet{sse}{south south east}%
}
%    \end{macrocode}
%
% \subsubsection{The \texttt{grid} shape}
% By default, have two additional lines in the rectangle (i.e.\ three small
% rectangles in each direction).
%    \begin{macrocode}
\pgfkeys{/tsx/grid lines@x/.initial=2}
\pgfkeys{/tsx/grid lines@y/.initial=2}
%    \end{macrocode}
% The user-visible key to set the number of lines.
%    \begin{macrocode}
\pgfkeys{/tsx/grid lines/.code={
    \pgfutil@in@{ and }{#1}
    \ifpgfutil@in@
        \tsx@shapes@grid@lines@parse(#1)
    \else
        \tsx@shapes@grid@lines@parse(#1 and #1)
    \fi}
}
\def\tsx@shapes@grid@lines@parse(#1 and #2){
    \pgfkeys{/tsx/grid lines@x=#1, 
             /tsx/grid lines@y=#2}
}

%    \end{macrocode}
%    \begin{macrocode}
\pgfdeclareshape{grid}{
%    \end{macrocode}
% We inherit the anchors and the \texttt{backgroundpath} from the rectangle shape.
%     \begin{macrocode}
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritanchor[from=rectangle]{center}
    \inheritanchor[from=rectangle]{north}
    \inheritanchor[from=rectangle]{south}
    \inheritanchor[from=rectangle]{west}
    \inheritanchor[from=rectangle]{east}
    \inheritanchor[from=rectangle]{south east}
    \inheritanchor[from=rectangle]{south west}
    \inheritanchor[from=rectangle]{north east}
    \inheritanchor[from=rectangle]{north west}
    \inheritbackgroundpath[from=rectangle]

%    \end{macrocode}
% Save the \texttt{grid lines} keys for later use.
%    \begin{macrocode}
    \savedmacro\lines@x{%
        \pgfmathtruncatemacro\lines@x{\pgfkeysvalueof{/tsx/grid lines@x}}%
    }
    \savedmacro\lines@y{%
        \pgfmathtruncatemacro\lines@y{\pgfkeysvalueof{/tsx/grid lines@y}}%
    }

%    \end{macrocode}
% Draw the grid.
%    \begin{macrocode}
    \beforebackgroundpath{
        \pgfutil@tempdima=0.5\pgflinewidth
        % store lower right in xa/ya and upper right in xb/yb
        \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
        \advance\pgf@xa by \pgfutil@tempdima
        \advance\pgf@ya by \pgfutil@tempdima
        \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
        \advance\pgf@xb by -\pgfutil@tempdima
        \advance\pgf@yb by -\pgfutil@tempdima

        % compute distance between the lines
        \pgfmathparse{(\the\pgf@xb-\the\pgf@xa)/(\lines@x + 1)}
        \pgf@xc=\pgfmathresult pt
        \pgfmathparse{(\the\pgf@yb-\the\pgf@ya)/(\lines@y + 1)}
        \pgf@yc=\pgfmathresult pt

%    \end{macrocode}
% This does the actual drawing. 
% First the vertical lines, then the horizontal lines.
%    \begin{macrocode}
        \c@pgf@counta=0
        \c@pgf@countb\lines@x\relax
        \pgf@xb=\pgf@xa
        \advance\pgf@xb\pgf@xc\relax
        \pgfmathloop
            \ifnum\c@pgf@counta<\c@pgf@countb
                \pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@ya}}
                \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
                \advance\c@pgf@counta 1\relax
                \advance\pgf@xb\pgf@xc\relax
        \repeatpgfmathloop
        % set \pgf@xb to the right side
        \c@pgf@counta=0
        \pgf@yb=\pgf@ya
        \advance\pgf@yb\pgf@yc\relax
        \c@pgf@countb\lines@y\relax
        \pgfmathloop
            \ifnum\c@pgf@counta<\c@pgf@countb
                \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@yb}}
                \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
                \advance\c@pgf@counta 1\relax
                \advance\pgf@yb\pgf@yc\relax
        \repeatpgfmathloop
        \pgfusepath{stroke}
    }

%    \end{macrocode}
% The next code block adds all the anchors by directly setting the 
% corresponding internal macros.
%    \begin{macrocode}
    \pgfutil@g@addto@macro\pgf@sh@s@grid{%
        \c@pgf@counta\lines@x
        \advance\c@pgf@counta 1\relax
        \pgfmathloop\ifnum\c@pgf@counta>-1
            {% group to allow nesting of loops
                \c@pgf@countb\lines@y
                \advance\c@pgf@countb 1\relax
                \pgfmathloop\ifnum\c@pgf@countb>-1
                    \pgfutil@ifundefined{pgf@anchor@grid@\the\c@pgf@counta\space\the\c@pgf@countb}{%
                        % need to use xdef, so that \c@pgf@counta/b are expanded
                        % vertices
                        \expandafter\xdef\csname pgf@anchor@grid@\the\c@pgf@counta\space\the\c@pgf@countb\endcsname{%
                            \noexpand\pgfutil@tempdima=\the\pgflinewidth
                            \noexpand\pgfutil@tempdima=0.5\noexpand\pgfutil@tempdima
                            \noexpand\southwest \noexpand\pgf@xa=\noexpand\pgf@x \noexpand\pgf@ya=\noexpand\pgf@y
                            \noexpand\advance\noexpand\pgf@xa by \noexpand\pgfutil@tempdima
                            \noexpand\advance\noexpand\pgf@ya by \noexpand\pgfutil@tempdima
                            \noexpand\northeast \noexpand\pgf@xb=\noexpand\pgf@x \noexpand\pgf@yb=\noexpand\pgf@y
                            \noexpand\advance\noexpand\pgf@xb by -\noexpand\pgfutil@tempdima
                            \noexpand\advance\noexpand\pgf@yb by -\noexpand\pgfutil@tempdima
                            \noexpand\pgfmathparse{(\noexpand\the\noexpand\pgf@xb-\noexpand\the\noexpand\pgf@xa)/(\noexpand\lines@x + 1)*\the\c@pgf@counta}
                            \noexpand\pgf@x=\noexpand\pgf@xa\noexpand\relax
                            \noexpand\advance\noexpand\pgf@x\noexpand\pgfmathresult pt\noexpand\relax
                            \noexpand\pgfmathparse{(\noexpand\the\noexpand\pgf@yb-\noexpand\the\noexpand\pgf@ya)/(\noexpand\lines@y + 1)*\the\c@pgf@countb}
                            \noexpand\pgf@y=\noexpand\pgf@ya\noexpand\relax
                            \noexpand\advance\noexpand\pgf@y\noexpand\pgfmathresult pt\noexpand\relax
                        }
                        % centers
                        \expandafter\xdef\csname pgf@anchor@grid@center\space\the\c@pgf@counta\space\the\c@pgf@countb\endcsname{%
                            \noexpand\pgfutil@tempdima=\the\pgflinewidth
                            \noexpand\pgfutil@tempdima=0.5\noexpand\pgfutil@tempdima
                            \noexpand\southwest \noexpand\pgf@xa=\noexpand\pgf@x \noexpand\pgf@ya=\noexpand\pgf@y
                            \noexpand\advance\noexpand\pgf@xa by \noexpand\pgfutil@tempdima
                            \noexpand\advance\noexpand\pgf@ya by \noexpand\pgfutil@tempdima
                            \noexpand\northeast \noexpand\pgf@xb=\noexpand\pgf@x \noexpand\pgf@yb=\noexpand\pgf@y
                            \noexpand\advance\noexpand\pgf@xb by -\noexpand\pgfutil@tempdima
                            \noexpand\advance\noexpand\pgf@yb by -\noexpand\pgfutil@tempdima
                            \noexpand\pgfmathparse{(\noexpand\the\noexpand\pgf@xb-\noexpand\the\noexpand\pgf@xa)/(2*(\noexpand\lines@x + 1))*(2*\the\c@pgf@counta-1)}
                            \noexpand\pgf@x=\noexpand\pgf@xa\noexpand\relax
                            \noexpand\advance\noexpand\pgf@x\noexpand\pgfmathresult pt\noexpand\relax
                            \noexpand\pgfmathparse{(\noexpand\the\noexpand\pgf@yb-\noexpand\the\noexpand\pgf@ya)/(2*(\noexpand\lines@y + 1))*(2*\the\c@pgf@countb-1)}
                            \noexpand\pgf@y=\noexpand\pgf@ya\noexpand\relax
                            \noexpand\advance\noexpand\pgf@y\noexpand\pgfmathresult pt\noexpand\relax
                        }
                    }{\c@pgf@countb0\relax}
                    \advance\c@pgf@countb-1\relax
                \repeatpgfmathloop
            }
            \advance\c@pgf@counta-1\relax
        \repeatpgfmathloop
    }
}
%    \end{macrocode}
%
% \iffalse
%</pgflibraryshapes>
% \fi
%
% \subsection{\texttt{pgflibrary.shapes.tex-sx}}
%
% \iffalse
%<*tikzlibraryshapes.tex-sx>
% \fi
% A simple wrapper so that \verb|\tikzlibrary{}| can be used.
%    \begin{macrocode}
\usepgflibrary{shapes.tex-sx}

\endinput
%    \end{macrocode}
% \iffalse
%</tikzlibraryshapes.tex-sx>
% \fi
% \Finale