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
|
% !TeX spellcheck = en_US
% !TeX root = tikz-ext-manual.tex
% Copyright 2022 by Qrrbrbirlbel
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Free Documentation License.
%
\section{And a little bit more}
\begin{tikzlibrary}{ext.misc}
This library adds miscellaneous utilities to \pgfname math, \pgfname\space or \tikzname.
\inspiration{FullArc-Q,ForeachUse-Q}{FullArc-A,ForeachUse-A}
\end{tikzlibrary}
\subsection{\pgfname math}
\begin{multicols}{2}
\pgfkeys{/codeexample/every codeexample/.append style={width=3cm}}
\subsubsection{Postfix operator \texttt{R}}
Similar to |\segments[<num>]| in PSTricks, the postfix operator |R| allows the user
to use an arbitrary number of segments of a circle to be used instead of an angle.
\begin{key}{/tikz/full arc=\meta{num} (default |{}|)}
The number \meta{num} of segments will be set up.
Using |full arc| with an empty value disables the segmentation and |1R| equals $1^\circ$.
The given value \meta{num} is evaluated when the key is used and doesn't change when
\meta{num} contains variables that change.
\end{key}
The |R| operator can then be used.
\begin{math-operator}{R}{postfix}{fullarc}
Multiplies \mvar{x} with $\frac{360}{\meta{num}}$.
\end{math-operator}
\subsubsection{Functions}
\begin{math-function}{strrepeat("\mvar{Text}", \mvar{x})}
\mathcommand
Returns a string with \mvar{Text} repeated \mvar{x} times.
\begin{codeexample}[]
\pgfmathparse{strrepeat("foo", 5)}
\pgfmathresult
\end{codeexample}
\end{math-function}
\begin{math-function}{isInString("\mvar{String}", "\mvar{Text}")}
\mathcommand
Returns |1| (true) if \mvar{Text} contains \mvar{String},
otherwise |0| (false).
\begin{codeexample}[]
\pgfmathparse{isInString("foo", "bar")}
\pgfmathresult \ and\
\pgfmathparse{isInString("foo", "foobar")}
\pgfmathresult
\end{codeexample}
\end{math-function}
\begin{math-function}{strcat("\mvar{Text A}", "\mvar{Text B}", …)}
\mathcommand
Returns the concatenation of all given parameters.
\begin{codeexample}[]
\pgfmathparse{strcat("blue!", int(7*3), "!green")}
\pgfmathresult
\end{codeexample}
\end{math-function}
\begin{math-function}{isEmpty("\mvar{Text}")}
\mathcommand
Returns |1| (true) if \mvar{Text} is empty, otherwise |0| (false).
%
\begin{codeexample}[]
\pgfmathparse{isEmpty("foo")} \pgfmathresult\ and\
\pgfmathparse{isEmpty("")} \pgfmathresult\ and\
\def\emptyText{}
\pgfmathparse{isEmpty("\emptyText")} \pgfmathresult
\end{codeexample}
\end{math-function}
\begin{math-function}{atanXY(\mvar{x},\mvar{y})}
\mathcommand
Arctangent of $\mvar y\div \mvar x$ in degrees. This also takes into account the quadrant.
This is just a argument-swapped version of |atan2|\indexMathFunctionO{atan2} which makes it easier to use
the |\p| commands of the |calc|\indexLibraryO{calc} library.
%
\begin{codeexample}[]
\pgfmathparse{atanXY(3,4)} \pgfmathresult
\end{codeexample}
\end{math-function}
\begin{math-function}{atanYX(\mvar{y},\mvar{x})}
\mathcommand
Arctangent of $y\div x$ in degrees. This also takes into account the quadrant.
\begin{codeexample}[]
\pgfmathparse{atanYX(4,3)} \pgfmathresult
\end{codeexample}
\end{math-function}
\subsubsection{Functions: using coordinates}
The following functions can only be used with \pgfname\space and/or \tikzname.
Since the arguments are usually plain text (and not numbers) one has to wrap
them in |"|.
\begin{math-function}{anglebetween("\mvar{p1}", "\mvar{p2}")}\mathcommand
Return the angle between the centers of the nodes \mvar{p1} and \mvar{p2}.
\end{math-function}
\begin{math-function}{qanglebetween("\mvar{p}")}\mathcommand
Return the angle between the origin and the center of the node \mvar{p}.
\end{math-function}
\begin{math-function}{distancebetween("\mvar{p1}", "\mvar{p2}")}\mathcommand
Return the distance (in pt) between the centers of the nodes \mvar{p1} and \mvar{p2}.
\end{math-function}
\begin{math-function}{qdistancebetween("\mvar{p}")}\mathcommand
Return the distance (in pt) between the origin and the center of the node \mvar{p}.
\end{math-function}
\end{multicols}
\begin{codeexample}[width=6cm,preamble=\usetikzlibrary{calc,ext.misc,through}]
\begin{tikzpicture}
\path (0,0) coordinate (A) + (0:4) coordinate (B) +(75:4) coordinate (C);
\draw (A) -- (B) -- (C) -- cycle;
\foreach \cnt in {1,...,4}{
\pgfmathsetmacro\triA{distancebetween("B","C")}
\pgfmathsetmacro\triB{distancebetween("C","A")}
\pgfmathsetmacro\triC{distancebetween("A","B")}
\path (barycentric cs:A=\triA,B=\triB,C=\triC) coordinate (M)
node [draw, circle through=($(A)!(M)!(C)$)] (M) {};
\draw ($(C)-(A)$) coordinate (vecB)
(M.75-90) coordinate (@)
(intersection of @--[shift=(vecB)]@ and B--C) coordinate (C) --
(intersection of @--[shift=(vecB)]@ and B--A) coordinate (A);}
\end{tikzpicture}
\end{codeexample}
\pagebreak
\subsection{\pgfname keys}
%
\begin{multicols}{2}
%
\subsubsection{Conditionals}
\begin{key}{/utils/if=\meta{cond}\meta{true}\opt{\meta{false}}}
This key checks the conditional \meta{cond} and applies the styles \meta{true}
if \meta{cond} is true, otherwise \meta{false}.
\meta{cond} can be anything that \pgfname math understands.
As a side effect on how \pgfname keys parses argument, the \meta{false} argument is
actually optional.
\end{key}
The following keys use \TeX' macros |\if|, |\ifx|, |\ifnum| and |\ifdim| for faster
executions.
\begin{key}{/utils/TeX/if=\meta{token A}\meta{token B}\meta{true}\opt{\meta{false}}}
This key checks via |\if| if \meta{token A} matches \meta{token B}
and applies the styles \meta{true} if it does, otherwise \meta{false}.
As a side effect on how \pgfname keys parses argument, the \meta{false} argument is
actually optional.
\end{key}
\begin{key}{/utils/TeX/ifx=\meta{token A}\meta{token B}\meta{true}\opt{\meta{false}}}
As above.
\end{key}
\begin{key}{/utils/TeX/ifnum=\meta{num cond}\meta{true}\\opt{\meta{false}}}
This key checks |\ifnum|\meta{num cond}
and applies the styles \meta{true} if true, otherwise \meta{false}.
A delimiting |\relax| will be inserted after \meta{num cond}.
As a side effect on how \pgfname keys parses argument, the \meta{false} argument is
actually optional.
\end{key}
\begin{key}{/utils/TeX/ifdim=\meta{dim cond}\meta{true}\opt{\meta{false}}}
As above.
\end{key}
\begin{key}{/utils/TeX/ifempty=\meta{Text}\meta{true}\opt{\meta{false}}}
This checks whether \meta{Text} is empty and applies styles \meta{true} if true,
otherwise \meta{false}.
\end{key}
\subsubsection{Handlers}
While already a lot of values given to keys are evaluated by \pgfname math at some point,
not all of them are.
\begin{handler}{{.pgfmath}|=|\meta{eval}}
This handler evaluates \meta{eval} before it is handed to the key.
\end{handler}
\begin{handler}{{.pgfmath int}|=|\meta{eval}}
As above but truncates the result.
\end{handler}
\begin{handler}{{.pgfmath strcat}|=|\meta{eval}}
As above but uses the |strcat| function.
In the example below, one could have used the |/pgf/foreach/evaluate| key from |\foreach|.
\begin{codeexample}[width=3.7cm,preamble=\usetikzlibrary{misc}]
\tikz\foreach \i in {0,10,...,100}
\draw[
line width=+.2cm,
color/.pgfmath strcat={"red!",sqrt(\i)*10,"!blue"}
]
(0,\i/50) -- +(right:3);
\end{codeexample}
\end{handler}
\begin{handler}{{.List}|=|\meta{\meta{e1}, \meta{e2}, \dots, \meta{en}}}
This handler evaluates the given list with |\foreach| and concatenates the element and
the result is then given to the used key.
\end{handler}
\end{multicols}
\begin{codeexample}[width=6cm,preamble=\usetikzlibrary{fit,ext.misc}]
\begin{tikzpicture}[nodes={draw, dashed, inner sep=+10pt}]
\foreach \point [count=\cnt] in {(0,0), (0,2), (2,0), (2,2), (3,3), (-1,-1)}
\fill \point circle[radius=.1] coordinate (point-\cnt);
\node[gray, fit/.List={(point-1),(point-...),(point-4)}] {};
\node[red, fit/.List={(point-1),(point-...),(point-5)}] {};
\node[blue, fit/.List={(point-1),(point-...),(point-6)}] {};
\end{tikzpicture}
\end{codeexample}
\subsection{\pgfname for}
Instead of |\foreach \var in {start, start + delta, ..., end}| one can use
|\foreach \var[use int=start to end step delta]|.
\begin{key}{/pgf/foreach/use int=\meta{start}|to|\meta{end}\opt{|step|\meta{delta}}}
The values \meta{start}, \meta{end} and \meta{delta} are evaluates by \pgfname math at initialization.
The part |step |\meta{delta} is optional (\meta{delta} = 1).
\end{key}
\begin{key}{/pgf/foreach/use float=\meta{start}|to|\meta{end}\opt{|step|\meta{delta}}}
Same as above, however the results are not truncated.
\end{key}
%TODO: edges to and edges through
\endinput
|