Debian Codegraph @

texmf-dist/tex/latex/tikzdotncross/tikzdotncross.sty

raw
1 %%%==============================================================================
2 %% Copyright 2024-present by Alceu Frigeri
3 %%
4 %% This work may be distributed and/or modified under the conditions of
5 %%
6 %% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
7 %% version 1.3c (or later), and/or
8 %% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html),
9 %% version 3 (or later)
10 %%
11 %% This work has the LPPL maintenance status *maintained*.
12 %%
13 %% The Current Maintainer of this work is Alceu Frigeri
14 %%
15 %% This is version {1.3b} {2025/11/10}
16 %%
17 %% The list of files that compose this work can be found in the README.md file at
18 %% https://ctan.org/pkg/tikzdotncross
19 %%
20 %%%==============================================================================
21 %% WARNING: These are personal packs/tests
22 %% They might and probably will change at will as needed
23 %%
24 %%%==============================================================================
25 \NeedsTeXFormat{LaTeX2e}[2022/06/01]
26
27 \ProvidesExplPackage
28 {tikzdotncross}
29 {2025/11/10}
30 {1.3b}
31 {Marking coordinates and crossing paths}
32
33 %%%%%%%
34 %%%
35 %%% Just an attempt of having my package's info in a regular way
36 %%% \Pkginfograb_set:nn {<pkg-name>} { props} for each and all.
37 %%%
38 %%%%%%%
39 \RequirePackage{pkginfograb}
40 \pkginfograb_set:nn { tikzdotncross }
41 {
42 name = {tikzdotncross} ,
43 prefix = {dotncross} ,
44 date = {2025/11/10},
45 version = {1.3b} ,
46 description = {Marking~ coordinates~ and~ crossing~ paths}
47 }
48 %%%%%%%
49 %%% End of cut-n-paste
50 %%%%%%%
51
52 %%%
53 %%% why that here? well, the token list's commands will be used outside expl3 'domain'
54 %%%
55 \makeatletter
56
57 \keys_define:nn { tikzdotncross }
58 {
59 pin size .tl_set:N = \l__dotncross_pinsep_tl ,
60 pin size .value_required:n = true ,
61 pin size .initial:n = 1.2 ,
62 pin size .usage:n = general ,
63
64 pin ang .tl_set:N = \l__dotncross_pinang_tl ,
65 pin ang .value_required:n = true ,
66 pin ang .initial:n = 45 ,
67 pin ang .usage:n = general ,
68
69 pin color .tl_set:N = \l__dotncross_pincolor_tl ,
70 pin color .value_required:n = true ,
71 pin color .initial:n = blue ,
72 pin color .usage:n = general ,
73
74 pin length .tl_set:N = \l__dotncross_pindistance_tl ,
75 pin length .value_required:n = true ,
76 pin length .initial:n = 4 ,
77 pin length .usage:n = general ,
78
79 coord color .tl_set:N = \l__dotncross_coordcolor_tl ,
80 coord color .value_required:n = true ,
81 coord color .initial:n = red ,
82 coord color .usage:n = general ,
83 }
84
85 \ProcessKeyOptions [ tikzdotncross ]
86
87 \NewDocumentCommand{\setpindefaults}{m} {
88 \keys_set:nn {tikzdotncross}{#1}
89 }
90
91
92 %%%%%%%%%%%%%%%%%%%
93 %%%%
94 %%%% some handy/auxiliary macros to define coordinate/node pairs (and selectively 'pin' them)
95 %%%% based on the idea from Redaelli et al. (CircuiTiKz)
96 %%%%
97 %%%% differences: variable number of paramenters (see below) and it always also adds an empty node n<coord>
98 %%%%
99 %%%%%%%%%%%%%%%%%%%
100
101 \keys_define:nn { tikzdotncross / show coords}
102 {
103 on .code:n = \__dotncross_showcoords: ,
104 on .value_forbidden:n = true ,
105
106 off .code:n = \__dotncross_hidecoords: ,
107 off .value_forbidden:n = true ,
108
109 true .code:n = \__dotncross_showcoords: ,
110 true .value_forbidden:n = true ,
111
112 false .code:n = \__dotncross_hidecoords: ,
113 false .value_forbidden:n = true ,
114 }
115
116 \cs_new:Npn \__dotncross_hidecoords:
117 {
118 \cs_set_eq:NN \ncoord \__dotncross_coordnode:w
119 \cs_set_eq:NN \dotcoord \__dotncross_dotcoordnode:w
120 \cs_set_eq:NN \odotcoord \__dotncross_odotcoordnode:w
121 }
122
123 \cs_new:Npn \__dotncross_showcoords:
124 {
125 \cs_set_eq:NN \ncoord \__dotncross_coordpin:w
126 \cs_set_eq:NN \dotcoord \__dotncross_dotcoordpin:w
127 \cs_set_eq:NN \odotcoord \__dotncross_odotcoordpin:w
128 }
129
130 \NewDocumentCommand{\showcoords}{m} {
131 \keys_set:nn {tikzdotncross / show coords}{#1}
132 }
133
134 \cs_new_eq:NN \showcoordsfalse \__dotncross_hidecoords:
135 \cs_new_eq:NN \showcoordstrue \__dotncross_showcoords:
136
137
138 \cs_new:Npn \__dotncross_coordpin:w (#1)
139 {
140 \pincoord(#1,\l__dotncross_coordcolor_tl,\l__dotncross_pinang_tl)
141 }
142 \cs_new:Npn \__dotncross_coordnode:w (#1)
143 {
144 coordinate(#1) node[minimum ~ size=\l__dotncross_pinsep_tl pt,inner ~ sep=\l__dotncross_pinsep_tl pt](n#1){}
145 }
146
147 \cs_new:Npn \__dotncross_dotcoordpin:w (#1)
148 {
149 \dotpincoord(#1,\l__dotncross_coordcolor_tl,\l__dotncross_pinang_tl)
150 }
151 \cs_new:Npn \__dotncross_dotcoordnode:w (#1)
152 {
153 coordinate(#1) node[circle,minimum ~ size=\l__dotncross_pinsep_tl pt,inner ~ sep=\l__dotncross_pinsep_tl pt,fill](n#1){}
154 }
155
156 \cs_new:Npn \__dotncross_odotcoordpin:w (#1)
157 {
158 \odotpincoord(#1,\l__dotncross_coordcolor_tl,\l__dotncross_pinang_tl)
159 }
160 \cs_new:Npn \__dotncross_odotcoordnode:w (#1)
161 {
162 coordinate(#1) node[circle,minimum ~ size=\l__dotncross_pinsep_tl pt,inner ~ sep=\l__dotncross_pinsep_tl pt,fill=white,draw](n#1){}
163 }
164
165
166 %%%%
167 %%
168 %% And some TeX to have a (expandable) variable number of parameters
169 %% \pincoord can be used as
170 %% \pincoord(coord-name) That's case D
171 %% \pincoord(coord-name,color) That's case C
172 %% \pincoord(coord-name,color,angle) That's case B
173 %% \pincoord(coord-name,color,angle,lenght) That's case A
174 %%
175 %%%%
176 %%
177 %% new/revised code
178 %% Note that: \dotncross@@pincoord expects 6 parameters !
179 %% That's why __dotncross_pincase:nn expects 2 !
180 %% The one that follows will be this last parameter
181 %%
182 %%%%
183 \scan_new:N \s__dotncross
184
185 \cs_new:Npn \__dotncross_pincase:nn #1
186 {
187 \__dotncross_pincase:w
188 #1 ,,,, \__dotncross_pincaseA:nnnnn \__dotncross_pincaseB:nnnnn \__dotncross_pincaseC:nnnnn \__dotncross_pincaseD:nnnnn \s__dotncross
189 }
190
191 %%%%
192 %%
193 %% Minimizing parameters juggling.
194 %% Note that, at first, the parameters order is reverted !
195 %% That Way:
196 %% pincaseA: can absorb them all 'directly'
197 %% pincaseB: just discards the first one (almost \use_none:n) and absorbs the 'rest'
198 %% pincaseC: just discards the first two (almost \use_none:nn) and absorbs the 'rest'
199 %% pincaseD: just discards the first three (almost \use_none:nnn) and absorbs the 'rest'
200 %%
201 %%%%
202
203 \cs_new:Npn \__dotncross_pincase:w #1,#2,#3,#4,#5#6#7#8#9 \s__dotncross
204 {
205 #8 {#4}{#3}{#2}{#1}
206 }
207 \cs_new:Npn \__dotncross_pincaseA:nnnnn
208 {
209 \dotncross@@pincoord \l__dotncross_pinsep_tl
210 }
211 \cs_new:Npn \__dotncross_pincaseB:nnnnn #1
212 {
213 \dotncross@@pincoord \l__dotncross_pinsep_tl \l__dotncross_pindistance_tl
214 }
215 \cs_new:Npn \__dotncross_pincaseC:nnnnn #1#2
216 {
217 \dotncross@@pincoord \l__dotncross_pinsep_tl \l__dotncross_pindistance_tl {-\l__dotncross_pinang_tl}
218 }
219 \cs_new:Npn \__dotncross_pincaseD:nnnnn #1#2#3
220 {
221 \dotncross@@pincoord \l__dotncross_pinsep_tl \l__dotncross_pindistance_tl {-\l__dotncross_pinang_tl} {\l__dotncross_pincolor_tl}
222 }
223
224
225 \cs_new:Npn \pincoord (#1)
226 {
227 \__dotncross_pincase:nn {#1}{}
228 }
229 \cs_new:Npn \dotpincoord (#1)
230 {
231 \__dotncross_pincase:nn {#1}{circle,fill,}
232 }
233 \cs_new:Npn \odotpincoord (#1)
234 {
235 \__dotncross_pincase:nn {#1}{circle,fill=white,draw,}
236 }
237
238 \showcoordsfalse
239
240 %%
241 %% Original/Old TeX code
242 %%
243 %% %%%%%%%%%%%%%%%%%%%
244 %% %%%%
245 %% %%%% some handy/auxiliary macros to define coordinate/node pairs (and selectively 'pin' them)
246 %% %%%% based on the idea from Redaelli et al. (CircuiTiKz)
247 %% %%%%
248 %% %%%% differences: variable number of paramenters (see below) and it always also adds an empty node n<coord>
249 %% %%%%
250 %% %%%%%%%%%%%%%%%%%%%
251 %%\newcommand\showcoordsfalse{\let\ncoord=\dotncross@@coordnode \let\dotcoord=\dotncross@@dotcoordnode \let\odotcoord=\dotncross@@odotcoordnode}
252 %%\newcommand\showcoordstrue{\let\ncoord=\dotncross@@coordpin \let\dotcoord=\dotncross@@dotcoordpin \let\odotcoord=\dotncross@@odotcoordpin}
253 %%
254 %%\def\dotncross@@coordpin(#1){\pincoord(#1,\dotncross@@coordcolor,\dotncross@@pinang)}
255 %%\def\dotncross@@coordnode(#1){coordinate(#1) node[minimum size=\dotncross@@pinsep pt,inner sep=\dotncross@@pinsep pt](n#1){}}
256 %%
257 %%\def\dotncross@@dotcoordpin(#1){\dotpincoord(#1,\dotncross@@coordcolor,\dotncross@@pinang)}
258 %%\def\dotncross@@dotcoordnode(#1){coordinate(#1) node[circle,minimum size=\dotncross@@pinsep pt,inner sep=\dotncross@@pinsep pt,fill](n#1){}}
259 %%
260 %%\def\dotncross@@odotcoordpin(#1){\odotpincoord(#1,\dotncross@@coordcolor,\dotncross@@pinang)}
261 %%\def\dotncross@@odotcoordnode(#1){coordinate(#1) node[circle,minimum size=\dotncross@@pinsep pt,inner sep=\dotncross@@pinsep pt,fill=white,draw](n#1){}}
262 %%
263 %% %%%%
264 %% %%
265 %% %% And some TeX to have a (expandable) variable number of parameters
266 %% %% \pincoord can be used as
267 %% %% \pincoord(coord-name) That's case D
268 %% %% \pincoord(coord-name,color) That's case C
269 %% %% \pincoord(coord-name,color,angle) That's case B
270 %% %% \pincoord(coord-name,color,angle,lenght) That's case A
271 %% %%
272 %% %%%%
273 %%\def\dotncross@@pincasew#1#2{\dotncross@@pincase#1,,,,\dotncross@@pincaseA\dotncross@@pincaseB\dotncross@@pincaseC\dotncross@@pincaseD;#2:}
274 %%\def\dotncross@@pincase#1,#2,#3,#4,#5#6#7#8#9:{#8:#1:#2:#3:#4:#9:}
275 %%\def\dotncross@@pincaseA:#1:#2:#3:#4:#5;#6:{\dotncross@@pincoord{#1}{#2}{#3}{#4}{#6}}
276 %%\def\dotncross@@pincaseB:#1:#2:#3:#4:#5;#6:{\dotncross@@pincoord{#1}{#2}{#3}{\dotncross@@pindistance}{#6}}
277 %%\def\dotncross@@pincaseC:#1:#2:#3:#4:#5;#6:{\dotncross@@pincoord{#1}{#2}{-\dotncross@@pinang}{\dotncross@@pindistance}{#6}}
278 %%\def\dotncross@@pincaseD:#1:#2:#3:#4:#5;#6:{\dotncross@@pincoord{#1}{\dotncross@@pincolor}{-\dotncross@@pinang}{\dotncross@@pindistance}{#6}}
279 %
280 %%\def\pincoord(#1){\dotncross@@pincasew{#1}{}}
281 %%\def\dotpincoord(#1){\dotncross@@pincasew{#1}{circle,fill,}}
282 %%\def\odotpincoord(#1){\dotncross@@pincasew{#1}{circle,fill=white,draw,}}
283 %
284 %%\newcommand\dotncross@@pincoord[5]{%
285 %% coordinate(#1) node[#5minimum size=\dotncross@@pinsep pt,inner sep=\dotncross@@pinsep pt](n#1){}
286 %% node[circle, #2, inner sep=\dotncross@@pinsep pt, outer sep=0pt, radius=1pt, fill=#2!20!white, fill opacity=0.2, draw opacity=0.4, draw,
287 %% pin={[#2, overlay, inner sep=0pt, outer sep=1pt, font=\tiny, pin distance=#4pt,
288 %% pin edge={#2, overlay}]#3:#1}]{}%
289 %%}
290
291
292 \ExplSyntaxOff
293
294 \usetikzlibrary{math,intersections}
295 \RequirePackage{etoolbox}
296
297 %%
298 %% There is no easy and clean way
299 %% to convert this all to expl3 (for instance underscore, under expl3 code régime, is a letter)
300 %% spaces (pgf/tikz) and then ':' (which is an active character under tikz but a letter for expl3)
301 %% ... and still being expandable
302 %%
303
304 %%%%%%%%%%%%%%%%%%%
305 %%%
306 %%% This will 'calculate' the intersection between a line (defined by the coordinates (#3) and (#4) and the path named #5
307 %%% at each intersection a coordinate named (#2-i) and a node (n#2-i) will be defined (i stands for a counter from 1 up to the number of crossings)
308 %%% A macro named <#1T> will have the number of crossings found.
309 %%%
310 %%% At each intersection a semi-circle will be drawn.
311 %%%
312 %%% the first parameter #1, star, flips the semi-circle.
313 %%%
314 %%%%%%%%%%%%%%%%%%%
315 \NewDocumentCommand{\pathcross}{sO{cross}mmmO{7pt}}{%
316 \IfBooleanTF{#1}{\def\dotncross@path@@sign{-}}{\def\dotncross@path@@sign{}}
317 \path[name path=#2 self] (#3) -- (#4);
318 \draw[name intersections={of={#5} and {#2 self},name=#2,total=\CrossTotal,sort by={#2 self}}]
319 \pgfextra{\csxdef{#2T}{\CrossTotal}}
320 \foreach \aux in {1,...,\csuse{#2T}}{(#2-\aux) node[minimum size=#6-\pgflinewidth,inner sep=0pt](n#2-\aux){}};
321 \tikzmath{%
322 coordinate \Ctmp; real \dtmp; int \idx;
323 \Ctmp{from} = (#3);
324 \Ctmp{to} = (#4);
325 \dtmp{x} = \Ctmpx{to} - \Ctmpx{from};
326 \dtmp{y} = \Ctmpy{to} - \Ctmpy{from};
327 \dtmp{ang} = atan2(\dtmp{y},\dtmp{x});
328 \Ctmp{1st} = (n#2-1);
329 \Ctmp{last} = (n#2-\csuse{#2T});
330 \idx{1st} = 1;
331 \idx{last} = \csuse{#2T};
332 if \Ctmpx{1st} == \Ctmpx{from} then {
333 if \Ctmpy{1st} == \Ctmpy{from} then {
334 \idx{1st} = 2;
335 };
336 };
337 if \Ctmpx{last} == \Ctmpx{to} then {
338 if \Ctmpy{last} == \Ctmpy{to} then {
339 \idx{last} = \idx{last}-1;
340 };
341 };
342 }
343 \ifnum\idx{last}<\idx{1st}
344 \draw (#3) -- (#4);
345 \else
346 \draw[line width=1.8\pgflinewidth,white,rotate=\dtmp{ang}]
347 \foreach \aux in {\idx{1st},...,\idx{last}}{(#2-\aux) +(-#6/2,0) arc[start angle=\dotncross@path@@sign180,end angle=0,radius=#6/2]};
348 \draw[rotate=\dtmp{ang},line cap=round]
349 \foreach \aux in {\idx{1st},...,\idx{last}}{(#2-\aux) +(-#6/2,0) arc[start angle=\dotncross@path@@sign180,end angle=0,radius=#6/2]};
350
351 \gdef\dotncross@path@@tmpA{#3}
352 \gdef\dotncross@path@@ListA{}
353 \foreach \x in {\idx{1st},...,\idx{last}} {\xappto{\dotncross@path@@ListA}{\dotncross@path@@tmpA/n#2-\x,}\xdef\dotncross@path@@tmpA{n#2-\x}}
354 \xappto{\dotncross@path@@ListA}{n#2-\idx{last}/#4}
355 \foreach \xa/\xb in \dotncross@path@@ListA {\draw[line cap=round] (\xa) -- (\xb);}
356 \fi
357 }
358
359
360 %%%%%%%
361 %%%%%%%
362 %%%
363 %%% Remember: The ':' is a pgf/tikz active character
364 %%%
365 %%%%%%%
366 %%%%%%%
367
368 %
369 % Old parameter sequence... note that, between them, #2..#5 are reverted
370 %
371 %\newcommand\dotncross@@pincoord[6]{%
372 % coordinate(#2) node[#6minimum size=#1 pt,inner sep=#1 pt](n#2){}
373 % node[circle, #3, inner sep=#1 pt, outer sep=0pt, radius=1pt, fill=#3!20!white, fill opacity=0.2, draw opacity=0.4, draw,
374 % pin={[#3, overlay, inner sep=0pt, outer sep=1pt, font=\tiny, pin distance=#5pt,
375 % pin edge={#3, overlay}]#4:#2}]{}%
376 %}
377
378
379 \newcommand\dotncross@@pincoord[6]{%
380 coordinate(#5) node[#6minimum size=#1 pt,inner sep=#1 pt](n#5){}
381 node[circle, #4, inner sep=#1 pt, outer sep=0pt, radius=1pt, fill=#4!20!white, fill opacity=0.2, draw opacity=0.4, draw,
382 pin={[#4, overlay, inner sep=0pt, outer sep=1pt, font=\tiny, pin distance=#2pt,
383 pin edge={#4, overlay}]#3:#5}]{}%
384 }
385
386