Debian Codegraph @

texmf-dist/doc/latex/tikz-ext/tikz-ext-manual-en-library-misc.tex

raw
1 % !TeX spellcheck = en_US
2 % !TeX root = tikz-ext-manual.tex
3 % Copyright 2022 by Qrrbrbirlbel
4 %
5 % This file may be distributed and/or modified
6 %
7 % 1. under the LaTeX Project Public License and/or
8 % 2. under the GNU Free Documentation License.
9 %
10
11 \section{And a little bit more}
12 \begin{tikzlibrary}{ext.misc}
13 This library adds miscellaneous utilities to \pgfname math, \pgfname\space or \tikzname.
14 \inspiration{FullArc-Q}{FullArc-A}
15 \end{tikzlibrary}
16
17 \subsection{\pgfname math}
18
19 \begin{multicols}{2}\
20 \pgfkeys{/codeexample/every codeexample/.append style={width=3cm}}
21 \subsubsection{Postfix operator \texttt{R}}
22
23 Similar to |\segments[<num>]| in PSTricks, the postfix operator |R| allows the user
24 to use an arbitrary number of segments of a circle to be used instead of an angle.
25
26 \begin{key}{/\pgfext/full arc=\meta{num} (default |\{\}|)}\keycompat{pgf}
27 The number \meta{num} of segments will be set up.
28 Using |full arc| with an empty value disables the segmentation and |1R| equals $1^\circ$.
29
30 The given value \meta{num} is evaluated when the key is used and doesn't change when
31 \meta{num} contains variables that change.
32 \end{key}
33 The |R| operator can then be used.
34 \begin{math-operator}{R}{postfix}{extfullarc}
35 Multiplies \mvar{x} with $\frac{360}{\meta{num}}$.
36 \end{math-operator}
37
38 \subsubsection{Functions}
39
40 \begin{math-function}{extstrrepeat("\mvar{Text}", \mvar{x})}\mathcompat{strrepeat}
41 \mathcommand
42 Returns a string with \mvar{Text} repeated \mvar{x} times.
43
44 \begin{codeexample}[]
45 \pgfmathparse{extstrrepeat("foo", 5)}
46 \pgfmathresult
47 \end{codeexample}
48 \end{math-function}
49
50 \begin{math-function}{extisInString("\mvar{String}", "\mvar{Text}")}\mathcompat{isInString}
51 \mathcommand
52 Returns |1| (true) if \mvar{Text} contains \mvar{String},
53 otherwise |0| (false).
54
55 \begin{codeexample}[]
56 \pgfmathparse{extisInString("foo", "bar")}
57 \pgfmathresult \ and\
58 \pgfmathparse{extisInString("foo", "foobar")}
59 \pgfmathresult
60 \end{codeexample}
61 \end{math-function}
62
63 \begin{math-function}{extstrcat("\mvar{Text A}", "\mvar{Text B}", …)}\mathcompat{strcat}
64 \mathcommand
65 Returns the concatenation of all given parameters.
66
67 \begin{codeexample}[]
68 \pgfmathparse{extstrcat("blue!", int(7*3), "!green")}
69 \pgfmathresult
70 \end{codeexample}
71 \end{math-function}
72
73
74 \begin{math-function}{extisEmpty("\mvar{Text}")}\mathcompat{isEmpty}
75 \mathcommand
76 Returns |1| (true) if \mvar{Text} is empty, otherwise |0| (false).
77 %
78 \begin{codeexample}[]
79 \pgfmathparse{extisEmpty("foo")} \pgfmathresult\ and\
80 \pgfmathparse{extisEmpty("")} \pgfmathresult\ and\
81 \def\emptyText{}
82 \pgfmathparse{extisEmpty("\emptyText")} \pgfmathresult
83 \end{codeexample}
84 \end{math-function}
85
86 \begin{math-function}{extatanXY(\mvar{x},\mvar{y})}\mathcompat{atanXY}
87 \mathcommand
88 Arctangent of $\mvar y\div \mvar x$ in degrees. This also takes into account the quadrant.
89 This is just a argument-swapped version of |atan2|\indexMathFunctionO{atan2} which makes it easier to use
90 the |\p| commands of the |calc|\indexLibraryO{calc} library.
91 %
92 \begin{codeexample}[]
93 \pgfmathparse{extatanXY(3,4)} \pgfmathresult
94 \end{codeexample}
95 \end{math-function}
96 \begin{math-function}{extatanYX(\mvar{y},\mvar{x})}\mathcompat{atanYX}
97 \mathcommand
98 Arctangent of $y\div x$ in degrees. This also takes into account the quadrant.
99 \begin{codeexample}[]
100 \pgfmathparse{extatanYX(4,3)} \pgfmathresult
101 \end{codeexample}
102 \end{math-function}
103
104 \subsubsection{Functions: using coordinates}
105 The following functions can only be used with \pgfname\space and/or \tikzname.
106 Since the arguments are usually plain text (and not numbers) one has to wrap
107 them in |"|.
108 \begin{math-function}{extanglebetween("\mvar{p1}", "\mvar{p2}")}\mathcompat{anglebetween}\mathcommand
109 Return the angle between the centers of the nodes \mvar{p1} and \mvar{p2}.
110 \end{math-function}
111 \begin{math-function}{extqanglebetween("\mvar{p}")}\mathcompat{qanglebetween}\mathcommand
112 Return the angle between the origin and the center of the node \mvar{p}.
113 \end{math-function}
114 \begin{math-function}{extdistancebetween("\mvar{p1}", "\mvar{p2}")}\mathcompat{distancebetween}\mathcommand
115 Return the distance (in pt) between the centers of the nodes \mvar{p1} and \mvar{p2}.
116 \end{math-function}
117 \begin{math-function}{extqdistancebetween("\mvar{p}")}\mathcompat{qdistancebetween}\mathcommand
118 Return the distance (in pt) between the origin and the center of the node \mvar{p}.
119 \end{math-function}
120
121 \end{multicols}
122
123 \begin{codeexample}[width=6cm,preamble=\usetikzlibrary{calc,ext.misc,through}]
124 \begin{tikzpicture}
125 \path (0,0) coordinate (A) + (0:4) coordinate (B) +(75:4) coordinate (C);
126 \draw (A) -- (B) -- (C) -- cycle;
127 \foreach \cnt in {1,...,4}{
128 \pgfmathsetmacro\triA{extdistancebetween("B","C")}
129 \pgfmathsetmacro\triB{extdistancebetween("C","A")}
130 \pgfmathsetmacro\triC{extdistancebetween("A","B")}
131 \path (barycentric cs:A=\triA,B=\triB,C=\triC) coordinate (M)
132 node [draw, circle through=($(A)!(M)!(C)$)] (M) {};
133 \draw ($(C)-(A)$) coordinate (vecB)
134 (M.75-90) coordinate (@)
135 (intersection of @--[shift=(vecB)]@ and B--C) coordinate (C) --
136 (intersection of @--[shift=(vecB)]@ and B--A) coordinate (A);}
137 \end{tikzpicture}
138 \end{codeexample}
139
140 \subsection{\pgfname for}
141
142 This library loads also most of the functions of the
143 \referencePackageandIndex{pgffor-ext} of section~\ref{pkg:pgffor-ext}
144 on page~\pageref{pkg:pgffor-ext}.
145
146 \subsection{\pgfname keys}
147 \begin{pgfkeyslibrary}{ext.pgfkeys-plus}
148 This extends \pgfname keys and adds helpful |/utils| keys as well as handlers.
149 This library gets loaded by the |ext.misc| library.
150 \end{pgfkeyslibrary}
151
152 \begin{multicols}{2}
153 \subsubsection{Conditionals}
154
155 \begin{key}{/\utilsext/pgfmath if=\marg{cond}\marg{true}\opt{\marg{false}}}{\def\myname{if}\keycompat{utils}}
156 This key checks the conditional \meta{cond} and applies the styles \meta{true}
157 if \meta{cond} is true, otherwise \meta{false}.
158 \meta{cond} can be anything that \pgfname math understands.
159
160 As a side effect on how \pgfname keys parses argument, the \meta{false} argument is
161 actually optional.
162 \end{key}
163
164 The following keys use \TeX' macros |\if|, |\ifx|, |\ifnum| and |\ifdim| for faster
165 executions.
166
167 \begin{key}{/\utilsext/if=\meta{token A}\meta{token B}\marg{true}\opt{\marg{false}}}\keycompat{utils/TeX}
168 This key checks via |\if| if \meta{token A} matches \meta{token B}
169 and applies the styles \meta{true} if it does, otherwise \meta{false}.
170
171 As a side effect on how \pgfname keys parses argument, the \meta{false} argument is
172 actually optional.
173 \end{key}
174
175 \begin{key}{/\utilsext/ifx=\meta{token A}\meta{token B}\marg{true}\opt{\marg{false}}}\keycompat{utils/TeX}
176 As above but via |\ifx|.
177 \end{key}
178
179 \begin{key}{/\utilsext/ifnum=\marg{num cond}\marg{true}\opt{\marg{false}}}\keycompat{utils/TeX}
180 This key checks |\ifnum|\meta{num cond}
181 and applies the styles \meta{true} if true, otherwise \meta{false}.
182 A delimiting |\relax| will be inserted after \meta{num cond}.
183
184 As a side effect on how \pgfname keys parses arguments, the \meta{false} argument is
185 actually optional.
186 \end{key}
187
188 \begin{key}{/\utilsext/ifdim=\meta{dim cond}\meta{true}\opt{\meta{false}}}\keycompat{utils/TeX}
189 As above but with |\ifdim|.
190 \end{key}
191
192 \begin{key}{/\utilsext/ifempty=\meta{Text}\meta{true}\opt{\meta{false}}}\keycompat{utils/TeX}
193 This checks whether \meta{Text} is empty and applies styles \meta{true} if true,
194 otherwise \meta{false}.
195 \end{key}
196 \begin{key}{/\utilsext/ifxempty=\meta{Text}\meta{true}\opt{\meta{false}}}\keycompat{utils/TeX}
197 This checks whether fully expanded \meta{Text} is empty and applies styles \meta{true} if true,
198 otherwise \meta{false}.
199 \end{key}
200
201 \subsubsection{Handlers}
202
203 While already a lot of values given to keys are evaluated by \pgfname math at some point,
204 not all of them are.
205
206 \begin{ext_handler}{{pgfmath}|=|\meta{eval}}\handlercompat{.pgfmath}
207 This handler evaluates \meta{eval} before it is handed to the key.
208
209 This handler works almost the same as the |.evaluated|\indexHandlerO{.evaluated}
210 handler but it does its evaluation in a group so that the result will
211 not overwrite any other results.
212 \end{ext_handler}
213
214 \begin{ext_handler}{{pgfmath int}|=|\meta{eval}}\handlercompat{.pgfmath int}
215 As above but truncates the result.
216 \end{ext_handler}
217
218 \begin{ext_handler}{{pgfmath wrap}|=|\marg{wrapper}\marg{eval}}\handlercompat{.pgfmath wrap}
219 This feeds the result of \meta{eval} as |#1| to \meta{wrapper}.
220
221 In the example below, one could have used the \referenceKeyandIndexO[/pgf/foreach/]{evaluate}
222 key from the |\foreach| loop.
223 \begin{codeexample}[width=3.7cm,preamble=\usepgfkeyslibrary{ext.pgfkeys-plus}]
224 \tikz\foreach \i in {0,10,...,100}
225 \draw[
226 line width=+.25cm,
227 % needs ## because its inside the \foreach body
228 color/.ext_pgfmath wrap={red!##1!blue}{sqrt(\i)*10}
229 ]
230 (0,\i/40) -- +(right:3);
231 \end{codeexample}
232 \end{ext_handler}
233
234 \begin{ext_handler}{{pgfmath if}|=|\marg{cond}\marg{true}\opt{\marg{false}}}\handlercompat{.pgfmath if}
235 Evaluates \meta{cond} with \pgfname Math and returns \meta{true} or \meta{false} to the used key respectively.
236 \end{ext_handler}
237 \begin{ext_handler}{{if}|=|\meta{token A}\meta{token B}\marg{true}\opt{\marg{false}}}\handlercompat{.if}
238 Checks via |\if| if \meta{token A} matches \meta{token B}
239 and applies the value \meta{true} if it does, otherwise \meta{false}.
240 \end{ext_handler}
241 \begin{ext_handler}{{ifx}|=|\meta{token A}\meta{token B}\marg{true}\opt{\marg{false}}}\handlercompat{.ifx}
242 As above but via |\ifx|.
243 \end{ext_handler}
244 \begin{ext_handler}{{ifnum}|=|\marg{ifnum cond}\marg{true}\opt{\marg{false}}}\handlercompat{.ifnum}
245 Checks via |\ifnum| if \meta{ifnum cond}
246 and applies the value \meta{true} if it does, otherwise \meta{false}.
247 \end{ext_handler}
248 \begin{ext_handler}{{ifdim}|=|\marg{ifdim cond}\marg{true}\opt{\marg{false}}}\handlercompat{.ifdim}
249 As above but via |\ifdim|.
250 \end{ext_handler}
251 \begin{ext_handler}{{ifxempty}|=|\marg{Text}\marg{true}\opt{\marg{false}}}\handlercompat{.ifxempty}
252 Checks whether a fully expanded \meta{Text} is empty
253 and applies the value \meta{true} if it does, otherwise \meta{false}.
254 \end{ext_handler}
255 \begin{ext_handler}{{ifempty}|=|\marg{Text}\marg{true}\opt{\marg{false}}}
256 Checks whether \meta{Text} is empty
257 and applies the value \meta{true} if it does, otherwise \meta{false}.
258 \end{ext_handler}
259 \begin{ext_handler}{{List}|=|\marg{\meta{e1}, \meta{e2}, \dots, \meta{en}}}\handlercompat{.List}
260 This handler evaluates the given list with |\foreach| and concatenates the element and
261 the result is then given to the used key.
262 \end{ext_handler}
263 \end{multicols}
264 \begin{codeexample}[width=6cm,preamble=\usetikzlibrary{fit,ext.misc}]
265 \begin{tikzpicture}[nodes={draw, dashed, inner sep=+10pt}]
266 \foreach \point [count=\cnt] in {(0,0), (0,2), (2,0), (2,2), (3,3), (-1,-1)}
267 \node[circle, fill, inner sep=1pt, text=white] (point-\cnt) at \point {\cnt};
268 \node[gray, fit/.ext_List={(point-1),(point-...),(point-4)}] {};
269 \node[red, fit/.ext_List={(point-1),(point-...),(point-5)}] {};
270 \node[blue, fit/.ext_List={(point-1),(point-...),(point-6)}] {};
271 \end{tikzpicture}
272 \end{codeexample}
273
274 \pagebreak
275 \subsection{\tikzname}
276 \begin{multicols}{2}
277 \begin{key}{/\tikzext/reverse clip=\meta{direction} (default counter clockwise)}\keycompat{tikz}
278 This key installs a very big rectangle which is either constructed
279 \texttt{counter clockwise} (like the \referencePathOperationandIndexO{circle}
280 path operation) or |clockwise|.
281 \end{key}
282 \begin{key}{/\tikzext/clip rule=\meta{direction} (default even odd)}\keycompat{tikz}
283 This key switches directly\footnote{%
284 Meaning, it directly executes
285 \referenceCommandandIndexO{\pgfseteorule}/%
286 \referenceCommandandIndexO{\pgfsetnonzerorule}
287 and doesn't accumulates where \tikzname\space
288 throws an error.}
289 to the specified rule which is
290 either |even odd| or |nonzero|.
291 This corresponds to the \referenceKeyandIndexO{even odd rule} and
292 \referenceKeyandIndexO{nonzero rule} keys.
293 \end{key}
294 \end{multicols}
295 \begingroup\catcode`\&=\active
296 \begin{codeexample}[preamble=\usetikzlibrary{ext.misc},width=6cm]
297 \newcommand*\myDiagram[1]{
298 \fill[left color=blue, right color=green] (0, 0) rectangle (2, 1);
299 \clip (1, .5) #1 [ext/reverse clip];
300 \fill[left color=green, right color=blue] (0, 0) rectangle (2, 1);
301 }
302 \begin{tikzpicture}[radius=.4, row sep=5mm, column sep=5mm]
303 \matrix[
304 row 2/.append style={ext/clip rule=even odd},
305 column 1/.append style={ext/reverse clip/.default=clockwise}
306 ]{
307 \myDiagram{circle[]} &
308 \myDiagram{+(0:.4) arc[start angle=0, delta angle=-360] -- cycle}
309 \\
310 \myDiagram{circle[]} &
311 \myDiagram{+(0:.4) arc[start angle=0, delta angle=-360] -- cycle}
312 \\};
313 \end{tikzpicture}
314 \end{codeexample}
315 \endgroup
316 \endinput