Debian Codegraph @

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

raw
1 %
2 % Copyright 2018 Luca Cappelletti
3 %
4 % Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 %
6 % The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 %
8 % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 %
10 \def\AdigraphVersionNumber{v1.7.2}
11
12 \NeedsTeXFormat{LaTeX2e}[1994/06/01]
13 \ProvidesPackage{adigraph}
14 [2018/03/27 \AdigraphVersionNumber LaTeX package for creating augmenting directed graphs]
15
16 \RequirePackage{etoolbox}
17 \RequirePackage{fp}
18 \RequirePackage{xparse}
19 \RequirePackage{xstring}
20 \RequirePackage{tikz}
21 \usetikzlibrary{calc}
22
23 \def\AdigraphZero{0}
24 \def\AdigraphTextualZero{zero}
25 \def\AdigraphDefaultColor{black}
26 \def\AdigraphDefaultWidth{0.4}
27 \def\AdigraphRed{red!90}
28 \def\AdigraphCyan{cyan!90}
29 \providebool{AdigraphDrawGraphs}
30 \booltrue{AdigraphDrawGraphs}
31
32 \makeatletter
33 \newcommand{\AdigraphRom}[1]{\expandafter\@slowromancap\romannumeral #1@}
34 \makeatother
35
36 \newcounter{AdigraphTotalNodeCounter}
37 \newcounter{AdigraphCurrentNodeCounter}
38 \newcounter{AdigraphNumberOfPaths}
39 \newcounter{AdigraphCurrentPathNumber}
40 \newcounter{AdigraphAdjacentNodes}
41
42 \NewDocumentCommand{\AdigraphDrawNode}{m}{%
43 %
44 % #1 is node normalized id
45 %
46 \letcs{\nodeX}{adigraphNode#1X}
47 \letcs{\nodeY}{adigraphNode#1Y}
48 \letcs{\nodeLabel}{adigraphNode#1Label}
49 \letcs{\nodeColor}{adigraphNode#1Color}
50 \letcs{\nodeWidth}{adigraphNode#1Width}
51 %
52 \node[vertex,color=\nodeColor,line width=\nodeWidth pt] (#1) at (\nodeX,\nodeY) {\nodeLabel};
53 }
54
55 \NewDocumentCommand{\sumOfOrientations}{m}{}
56
57 \NewDocumentCommand{\AdigraphDrawEdge}{m m}{%
58 %
59 % #1 is first node normalized id
60 % #2 is second node normalized id
61 %
62 \ifbool{adigraphEdge#1v#2Drawn}{%
63 % This edge was already drawn
64 }{%
65 \booltrue{adigraphEdge#1v#2Drawn}
66 \letcs{\edgeLabelPosition}{adigraphEdge#1v#2LabelPosition}
67 \letcs{\edgeWeight}{adigraphEdge#1v#2Weight}
68 \letcs{\edgeWidth}{adigraphEdge#1v#2Width}
69 \ifcsdef{adigraphEdge#2v#1Weight}{%
70 \letcs{\AdigraphTwinEdgeWeight}{adigraphEdge#2v#1Weight}
71 }{%
72 \let\AdigraphTwinEdgeWeight\AdigraphZero
73 }
74 \providebool{firstWeight}
75 \FPifeq{\edgeWeight}{0}
76 \booltrue{firstWeight}
77 \else
78 \boolfalse{firstWeight}
79 \fi
80
81 \providebool{secondWeight}
82 \FPifeq{\AdigraphTwinEdgeWeight}{0}
83 \booltrue{secondWeight}
84 \else
85 \boolfalse{secondWeight}
86 \fi
87
88 \letcs{\edgeColor}{adigraphEdge#1v#2Color}
89 \ifbool{adigraphEdge#1v#2HasLabel}{%
90 \letcs{\edgeLabel}{adigraphEdge#1v#2Label}
91 }{%
92 \letcs{\edgeLabel}{adigraphEdge#1v#2Weight}
93 }
94 \ifbool{firstWeight}{%
95 % We don't draw edges with weight 0
96 }{%
97 \ifbool{secondWeight}{%
98 % The twin edge won't be drawn
99 % This edge will be a straight line
100 \ifbool{adigraphEdge#1v#2HasEmptyLabel}{
101 \draw[edge,color=\edgeColor,line width=\edgeWidth pt] (#1) to (#2);
102 }{
103 \draw[edge,color=\edgeColor,line width=\edgeWidth pt] (#1) to node[\edgeLabelPosition, fill=white] {\edgeLabel}(#2);
104 }
105 }{%
106 \IfEq{#1}{#2}{
107 % This is a self loop
108 \def\mySinSum{0}
109 \def\myCosSum{0}
110 \setcounter{AdigraphAdjacentNodes}{0}
111 \RenewDocumentCommand{\sumOfOrientations}{m}{
112 %
113 % #1 is name of node
114 %
115 \IfEq{#1}{##1}{
116 % This is the autoloop
117 }{
118 \newcount\myedgecounter
119 \myedgecounter 0\relax
120 \ifcsdef{adigraphEdge#1v##1Weight}{
121 \advance\myedgecounter 1\relax
122 }{}
123 \ifcsdef{adigraphEdge##1v#1Weight}{
124 \advance\myedgecounter 1\relax
125 }{}
126 \ifnumgreater{\the\myedgecounter}{0}{
127 \stepcounter{AdigraphAdjacentNodes}
128 \letcs{\firstX}{adigraphNode#1X}
129 \letcs{\firstY}{adigraphNode#1Y}
130 \letcs{\secondX}{adigraphNode##1X}
131 \letcs{\secondY}{adigraphNode##1Y}
132 \pgfmathsetmacro{\mySinSum}{\mySinSum+sin(atan2(\secondY-\firstY,\secondX-\firstX))}
133 \pgfmathsetmacro{\myCosSum}{\myCosSum+cos(atan2(\secondY-\firstY,\secondX-\firstX))}
134 }{}
135 }
136 }
137 \forlistloop{\sumOfOrientations}{\AdigraphNodeList}
138 \newcount\nodesNumber
139 \nodesNumber \value{AdigraphAdjacentNodes}\relax
140 \IfEq{\the\nodesNumber}{0}{
141 %No elements
142 \pgfmathsetmacro{\centralAngle}{0}
143 }{
144 \newcount\nodesNumber
145 \nodesNumber \value{AdigraphAdjacentNodes}\relax
146 \pgfmathsetmacro{\meanAngle}{atan2(\mySinSum/\the\nodesNumber,\myCosSum/\the\nodesNumber)}
147 \pgfmathsetmacro{\centralAngle}{180+360-max(\meanAngle,360-(\meanAngle))}
148 }
149 \pgfmathsetmacro{\leftAngle}{\centralAngle-35}
150 \pgfmathsetmacro{\rightAngle}{\centralAngle+35}
151 \ifbool{adigraphEdge#1v#2HasEmptyLabel}{
152 \draw[edge,color=\edgeColor,line width=\edgeWidth pt] (#1) to [out=\leftAngle,in=\rightAngle,looseness=8](#1);
153 }{
154 \draw[edge,color=\edgeColor,line width=\edgeWidth pt] (#1) to [out=\leftAngle,in=\rightAngle,looseness=8] node[\edgeLabelPosition, fill=white] {\edgeLabel}(#1);
155 }
156 }{
157 % Both edges have to be drawn, they will be curly
158 \ifbool{adigraphEdge#1v#2HasEmptyLabel}{
159 \draw[edge,color=\edgeColor,line width=\edgeWidth pt] (#1) to [bend right=20] (#2);
160 }{
161 \draw[edge,color=\edgeColor,line width=\edgeWidth pt] (#1) to [bend right=20] node[\edgeLabelPosition, fill=white] {\edgeLabel}(#2);
162 }
163 }
164 }%
165 }%
166 }
167 }
168
169 \NewDocumentCommand{\AdigraphGenerateNodeName}{m G{\AdigraphDefaultColor} G{\AdigraphDefaultWidth}}{}
170 \NewDocumentCommand{\AdigraphMemorizeNode}{m G{}}{}
171
172 \NewDocumentCommand{\AdigraphBuildNode}{> { \SplitArgument{ 2 } {,} } m > { \SplitArgument{ 1 } {,} } m m}{%
173 %
174 % #1 -> given node name, not normalized, color and width
175 % #2 -> list of coordinates
176 % #3 -> optional label
177 %
178 \RenewDocumentCommand{\AdigraphGenerateNodeName}{m G{\AdigraphDefaultColor} G{\AdigraphDefaultWidth}}{%
179 %
180 % ##1 -> node name, not normalized
181 % ##2 -> color
182 % ##3 -> width
183 %
184 \IfInteger{##1}{%
185 \IfEq{##1}{\AdigraphZero}{
186 \def\AdigraphNodeName{\AdigraphTextualZero}%
187 }{
188 \def\AdigraphNodeName{\AdigraphRom{##1}}%
189 }
190 }{%
191 \def\AdigraphNodeName{##1}%
192 }%
193 \listxadd{\AdigraphNodeList}{\AdigraphNodeName}
194 \ifblank{##2}{
195 \csdef{adigraphNode\AdigraphNodeName Color}{\AdigraphDefaultColor}%
196 }{
197 \csdef{adigraphNode\AdigraphNodeName Color}{##2}%
198 }
199 \ifblank{##3}{
200 \csdef{adigraphNode\AdigraphNodeName Width}{\AdigraphDefaultWidth}%
201 }{
202 \csdef{adigraphNode\AdigraphNodeName Width}{##3}%
203 }
204
205 \ifblank{#3}{
206 \csdef{adigraphNode\AdigraphNodeName Label}{##1}%
207 }{
208 \csdef{adigraphNode\AdigraphNodeName Label}{#3}%
209 }
210 }
211 \RenewDocumentCommand{\AdigraphMemorizeNode}{m G{}}{%
212 \ifblank{##2}{
213 \newcount\radius
214 \newcount\myi
215 \newcount\myn
216 \newcount\mydpi
217 \radius ##1\relax
218 \myi \value{AdigraphCurrentNodeCounter}\relax
219 \myn \value{AdigraphTotalNodeCounter}\relax
220 \FPset{\mydpi}{6.283185307179586476}
221 \FPmul{\numerator}{\the\myi}{\mydpi}
222 \FPdiv{\anomaly}{\numerator}{\the\myn}
223 \FPcos{\nodeX}{\anomaly}
224 \FPsin{\nodeY}{\anomaly}
225 \FPmul{\nodeX}{\nodeX}{\the\radius}
226 \FPmul{\nodeY}{\nodeY}{\the\radius}
227 \csedef{adigraphNode\AdigraphNodeName X}{\nodeX}%
228 \csedef{adigraphNode\AdigraphNodeName Y}{\nodeY}%
229 \stepcounter{AdigraphCurrentNodeCounter}
230 }{
231 \csdef{adigraphNode\AdigraphNodeName X}{##1}%
232 \csdef{adigraphNode\AdigraphNodeName Y}{##2}%
233 }
234 }%
235 \AdigraphGenerateNodeName#1
236 \AdigraphMemorizeNode#2
237 }
238
239 \NewDocumentCommand{\AdigraphMemorizeEdge}{m m G{\AdigraphDefaultColor} G{\AdigraphDefaultWidth}}{}
240
241 \NewDocumentCommand{\AdigraphBuildEdge}{> {\SplitArgument{3}{,}} m m m m}{%
242 %
243 % #1 -> List of edge nodes, its color and width
244 % #2 -> Weight
245 % #3 -> Label, defaults to weight
246 % #4 -> Label position, defaults to midway
247 %
248 \RenewDocumentCommand{\AdigraphMemorizeEdge}{m m G{\AdigraphDefaultColor} G{\AdigraphDefaultWidth}}{%
249 %
250 % ##1 -> first edge
251 % ##2 -> second edge
252 % ##3 -> color
253 % ##4 -> width
254 %
255 \IfInteger{##1}{%
256 \IfEq{##1}{\AdigraphZero}{
257 \csedef{AdigraphFirstNode}{\AdigraphTextualZero}%
258 }{
259 \csedef{AdigraphFirstNode}{\AdigraphRom{##1}}%
260 }
261 }{%
262 \csedef{AdigraphFirstNode}{##1}%
263 }%
264 \IfInteger{##2}{%
265 \IfEq{##2}{\AdigraphZero}{
266 \csedef{AdigraphSecondNode}{\AdigraphTextualZero}%
267 }{
268 \csedef{AdigraphSecondNode}{\AdigraphRom{##2}}%
269 }
270 }{%
271 \csedef{AdigraphSecondNode}{##2}%
272 }%
273
274 \xifinlist{\AdigraphFirstNode,\AdigraphSecondNode}{\AdigraphEdgeList}{
275 %This edge was previously added
276 }{
277 \xifinlist{\AdigraphFirstNode}{\AdigraphNodeList}{
278 \xifinlist{\AdigraphSecondNode}{\AdigraphNodeList}{
279 %This edge is new
280 \csedef{AdigraphEdgeName}{\AdigraphFirstNode v\AdigraphSecondNode}
281 \listxadd{\AdigraphEdgeList}{\AdigraphFirstNode,\AdigraphSecondNode}
282
283 \newbool{adigraphEdge\AdigraphEdgeName HasLabel}
284 \newbool{adigraphEdge\AdigraphEdgeName HasEmptyLabel}
285 \newbool{adigraphEdge\AdigraphEdgeName Drawn}
286 \ifblank{#3}{%
287 \boolfalse{adigraphEdge\AdigraphEdgeName HasLabel}
288 }{%
289 \booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
290 }%
291 \ifblank{#2}{%
292 \booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
293 \booltrue{adigraphEdge\AdigraphEdgeName HasEmptyLabel}
294 \csdef{adigraphEdge\AdigraphEdgeName Weight}{1}%
295 }{%
296 \csdef{adigraphEdge\AdigraphEdgeName Weight}{#2}%
297 }%
298 \csdef{adigraphEdge\AdigraphEdgeName Label}{#3}%
299 \ifblank{##3}{
300 \csdef{adigraphEdge\AdigraphEdgeName Color}{\AdigraphDefaultColor}%
301 }{
302 \csdef{adigraphEdge\AdigraphEdgeName Color}{##3}%
303 }
304 \ifblank{##4}{
305 \csdef{adigraphEdge\AdigraphEdgeName Width}{\AdigraphDefaultWidth}%
306 }{
307 \csdef{adigraphEdge\AdigraphEdgeName Width}{##4}%
308 }
309 \csdef{adigraphEdge\AdigraphEdgeName LabelPosition}{#4}%
310 }{
311 \PackageError{adigraph}{%
312 The given edge ends in the node ##2, that does not exist.
313 }{%
314 You have to create the node ##2.
315 }
316 }
317 }{
318 \PackageError{adigraph}{%
319 The given edge starts from the node ##1, that does not exist.
320 }{%
321 You have to create the node ##1.
322 }
323 }
324 }
325 }
326 \AdigraphMemorizeEdge#1
327 }
328
329 \NewDocumentCommand{\AdigraphBuildEdgeWrapper}{m G{} G{} G{midway}}{%
330 %
331 % #1 -> List of edge nodes and its color
332 % #2 -> Weight
333 % #3 -> Label
334 % #4 -> Label position
335 %
336 \ifblank{#1}{%
337 %Do nothing, this is the tail of the list
338 }{%
339 \AdigraphBuildEdge{#1}{#2}{#3}{#4}
340 }
341 }
342
343 \NewDocumentCommand{\AdigraphElaboratePath}{m m G{\AdigraphDefaultColor}}{}
344
345 \NewDocumentCommand{\AdigraphSimpleSum}{m m m}{%
346 %
347 % #1 -> First addendum
348 % #2 -> Second addendum
349 % #3 -> Variable to store sum
350 %
351 \pgfmathsetmacro{\sat}{#1+#2}
352 \pgfmathfrac{\sat}
353 \IfEq{\pgfmathresult}{0.0}{
354 \pgfmathint{\sat}
355 \FPset{#3}{\pgfmathresult}
356 }{
357 \FPset{#3}{\sat}
358 }
359 }
360
361 \NewDocumentCommand{\AdigraphPathBuilder}{m m m m m m m}{%
362 %
363 % #1 -> first node
364 % #2 -> second node
365 % #3 -> units
366 % #4 -> forward color
367 % #5 -> backward color
368 % #6 -> forward width
369 % #7 -> backward width
370 %
371 \ifcsdef{adigraphEdge#1v#2Weight}{%
372 \letcs{\AdigraphWeightA}{adigraphEdge#1v#2Weight}
373 }{%
374 \def\AdigraphWeightA{0}
375 \newbool{adigraphEdge#1v#2HasLabel}
376 \newbool{adigraphEdge#1v#2HasEmptyLabel}
377 \ifcsdef{adigraphEdge#2v#1LabelPosition}{%
378 \csdef{adigraphEdge#1v#2LabelPosition}{\csuse{adigraphEdge#2v#1LabelPosition}}
379 }{%
380 \csdef{adigraphEdge#1v#2LabelPosition}{midway}
381 }
382 \csdef{adigraphEdge#1v#2LabelPosition}{midway}
383 \newbool{adigraphEdge#1v#2Drawn}
384 \listgadd{\AdigraphEdgeList}{#1,#2}
385 }
386 \ifcsdef{adigraphEdge#2v#1Weight}{%
387 \letcs{\AdigraphWeightB}{adigraphEdge#2v#1Weight}
388 }{%
389 \def\AdigraphWeightB{0}
390 \newbool{adigraphEdge#2v#1HasLabel}
391 \newbool{adigraphEdge#2v#1HasEmptyLabel}
392 \ifcsdef{adigraphEdge#1v#2LabelPosition}{%
393 \csdef{adigraphEdge#2v#1LabelPosition}{\csuse{adigraphEdge#1v#2LabelPosition}}
394 }{%
395 \csdef{adigraphEdge#2v#1LabelPosition}{midway}
396 }
397 \newbool{adigraphEdge#2v#1Drawn}
398 \listgadd{\AdigraphEdgeList}{#2,#1}
399 }
400
401 \ifblank{#4}{
402 \cslet{adigraphEdge#1v#2Color}{\AdigraphDefaultColor}%
403 }{
404 \cslet{adigraphEdge#1v#2Color}{#4}%
405 }
406 \ifblank{#5}{
407 \cslet{adigraphEdge#2v#1Color}{\AdigraphDefaultColor}%
408 }{
409 \cslet{adigraphEdge#2v#1Color}{#5}%
410 }
411 \ifblank{#6}{
412 \cslet{adigraphEdge#1v#2Width}{\AdigraphDefaultWidth}%
413 }{
414 \cslet{adigraphEdge#1v#2Width}{#6}%
415 }
416 \ifblank{#7}{
417 \cslet{adigraphEdge#2v#1Width}{\AdigraphDefaultWidth}%
418 }{
419 \cslet{adigraphEdge#2v#1Width}{#7}%
420 }
421
422 \expandafter\AdigraphSimpleSum\expandafter{\AdigraphWeightA}{-#3}{\resultA}
423 \expandafter\AdigraphSimpleSum\expandafter{\AdigraphWeightB}{#3}{\resultB}
424 \cslet{adigraphEdge#1v#2Weight}{\resultA}%
425 \cslet{adigraphEdge#2v#1Weight}{\resultB}%
426 }
427
428 \NewDocumentCommand{\AdigraphElaboratePathColors}{G{} G{\AdigraphDefaultColor}}{}
429 \NewDocumentCommand{\AdigraphElaboratePathWidth}{G{\AdigraphDefaultWidth} G{}}{}
430
431 \NewDocumentCommand{\AdigraphBuildPath}{> { \SplitList{,} } m G{} > { \SplitArgument{ 1 } {,} } G{} G{}}{%
432 %
433 % #1 -> List of nodes
434 % #2 -> Units to send over path
435 % #3 -> Colors list
436 % #4 -> Width list
437 %
438 \ifblank{#2}{%
439 \FPset{\units}{0}
440 }{
441 \FPset{\units}{#2}
442 }
443
444 \RenewDocumentCommand{\AdigraphElaboratePathColors}{G{} G{\AdigraphDefaultColor}}{%
445 \ifblank{##1}{
446 \ifnum\value{AdigraphNumberOfPaths}=\value{AdigraphCurrentPathNumber}
447 \def\AdigraphForwardPathColor{\AdigraphRed}
448 \def\AdigraphBackwardPathColor{\AdigraphCyan}
449 \else
450 \def\AdigraphForwardPathColor{\AdigraphDefaultColor}
451 \def\AdigraphBackwardPathColor{\AdigraphDefaultColor}
452 \fi
453 }{
454 \def\AdigraphForwardPathColor{##1}
455 \ifblank{##2}{
456 \def\AdigraphBackwardPathColor{\AdigraphDefaultColor}
457 }{
458 \def\AdigraphBackwardPathColor{##2}
459 }
460 }
461 }
462
463 \AdigraphElaboratePathColors#3
464
465 \RenewDocumentCommand{\AdigraphElaboratePathWidth}{G{\AdigraphDefaultWidth} G{##1}}{%
466 \ifblank{##1}{
467 \def\AdigraphForwardPathWidth{\AdigraphDefaultWidth}
468 }{
469 \def\AdigraphForwardPathWidth{##1}
470 }
471 \ifblank{##2}{
472 \def\AdigraphBackwardPathWidth{\AdigraphDefaultWidth}
473 }{
474 \def\AdigraphBackwardPathWidth{##2}
475 }
476 }
477
478 \AdigraphElaboratePathWidth#4
479
480 \RenewDocumentCommand{\AdigraphElaboratePath}{m}{%
481 \IfInteger{##1}{%
482 \IfEq{##1}{\AdigraphZero}{
483 \def\AdigraphCurrentNode{\AdigraphTextualZero}%
484 }{
485 \def\AdigraphCurrentNode{\AdigraphRom{##1}}%
486 }
487 }{%
488 \def\AdigraphCurrentNode{##1}%
489 }%
490 \ifdef{\AdigraphLastParsedNode}{%
491 \expandafter\expandafter\expandafter\AdigraphPathBuilder\expandafter\expandafter\expandafter{\expandafter\AdigraphLastParsedNode\expandafter}\expandafter{\AdigraphCurrentNode}{\units}{\AdigraphForwardPathColor}{\AdigraphBackwardPathColor}{\AdigraphBackwardPathWidth}{\AdigraphBackwardPathWidth}
492 }{%
493 % This is the first iteration
494 }
495 \IfInteger{##1}{%
496 \IfEq{##1}{\AdigraphZero}{
497 \def\AdigraphLastParsedNode{\AdigraphTextualZero}%
498 }{
499 \def\AdigraphLastParsedNode{\AdigraphRom{##1}}%
500 }
501 }{%
502 \def\AdigraphLastParsedNode{##1}%
503 }%
504 }
505 \ProcessList{#1}{\AdigraphElaboratePath}
506 \undef{\AdigraphLastParsedNode}
507 }
508
509 \NewDocumentCommand{\AdigraphBuildNodeWrapper}{m m G{}}{
510 % #1 -> given node name, not normalized, color and width
511 % #2 -> list of coordinates
512 % #3 -> optional label
513 \ifblank{#1}{
514 %end of list
515 }{
516 \AdigraphBuildNode{#1}{#2}{#3}
517 }
518 }
519
520 \NewDocumentCommand{\AdigraphNodeBuilder}{ > { \SplitArgument{ 2 } {:} } m }{%
521 \AdigraphBuildNodeWrapper#1
522 }
523
524 \NewDocumentCommand{\AdigraphEdgeBuilder}{ > { \SplitArgument{ 3 } {:} } m }{%
525 \AdigraphBuildEdgeWrapper#1
526 }
527
528
529
530 \NewDocumentCommand{\AdigraphApplyKleenePlusEdgeBuilder}{m}{%
531 %
532 % #1 -> element
533 %
534 \ifblank{#1}{
535 %end of list
536 }{
537 \IfSubStr{\AdigraphCurrentElaboratingEdge}{,#1,}{
538 %The node is already contained
539 }{
540 \IfSubStr{\AdigraphCurrentElaboratingEdge}{,#1}{
541 %The node is already contained
542 }{
543 \IfSubStr{\AdigraphCurrentElaboratingEdge}{#1,}{
544 \StrPosition{\AdigraphCurrentElaboratingEdge}{#1,}[\stringPosition]
545 \ifnumequal{\stringPosition}{1}{
546 %This is at the beginning of the list
547 }{
548 \StrSubstitute[1]{\AdigraphCurrentElaboratingEdge}{+}{#1}[\temp]%
549 \expandafter\AdigraphFirstEdgeRenormalizer\expandafter{\temp}
550 }
551 }{
552 \StrSubstitute[1]{\AdigraphCurrentElaboratingEdge}{+}{#1}[\temp]%
553 \expandafter\AdigraphFirstEdgeRenormalizer\expandafter{\temp}
554 }
555 }
556 }
557 }
558 }
559
560 \NewDocumentCommand{\AdigraphKleenePlusEdgeBuilder}{m}{%
561 \ifrmnum{#1}{
562 \def\AdigraphTempList{}
563 \listeadd{\AdigraphTempList}{\rmntonum{#1}}
564 \forlistloop{\AdigraphApplyKleenePlusEdgeBuilder}{\AdigraphTempList}
565 }{
566 \AdigraphApplyKleenePlusEdgeBuilder{#1}
567 }
568 }
569
570 \NewDocumentCommand{\AdigraphSecondEdgeRenormalizer}{m}{%
571 \xifinlist{+}{\AdigraphNodeList}{
572 %The plus is not a kleene sign, but a symbol for a node
573 \AdigraphEdgeBuilder{#1}
574 }{
575 \IfSubStr{#1}{+}{
576 \def\AdigraphCurrentElaboratingEdge{#1}
577 \forlistloop{\AdigraphKleenePlusEdgeBuilder}{\AdigraphNodeList}
578 }{
579 \AdigraphEdgeBuilder{#1}
580 }
581 }
582 }
583
584 \NewDocumentCommand{\AdigraphKleeneStarEdgeBuilder}{m}{%
585 \StrSubstitute[1]{\AdigraphCurrentElaboratingEdge}{*}{#1}[\temp]%
586 \expandafter\AdigraphFirstEdgeRenormalizer\expandafter{\temp}
587 }
588
589 \NewDocumentCommand{\AdigraphFirstEdgeRenormalizer}{m}{%
590 \xifinlist{*}{\AdigraphNodeList}{
591 %The asterisk is not a kleene sign, but a symbol for a node
592 \AdigraphSecondEdgeRenormalizer{#1}
593 }{
594 \IfSubStr{#1}{*}{
595 \def\AdigraphCurrentElaboratingEdge{#1}
596 \forlistloop{\AdigraphKleeneStarEdgeBuilder}{\AdigraphNodeList}
597 }{
598 \AdigraphSecondEdgeRenormalizer{#1}
599 }
600 }
601 }
602
603 \NewDocumentCommand{\AdigraphEdgeDrawer}{ > { \SplitArgument{ 1 } {,} } m }{%
604 \AdigraphDrawEdge#1
605 }
606
607 \NewDocumentCommand{\AdigraphNodeCounter}{m G{}}{%
608 \ifblank{#2}{
609 \stepcounter{AdigraphTotalNodeCounter}
610 }{}
611 }
612
613 \NewDocumentCommand{\AdigraphNodeCounterWrapper}{m > { \SplitArgument{ 1 } {,} } m m m }{%
614 \ifblank{#1}{}{
615 \AdigraphNodeCounter#2
616 }
617 }
618
619 \NewDocumentCommand{\AdigraphNodeCounterSecondWrapper}{ > { \SplitArgument{ 3 } {:} } m }{%
620 \AdigraphNodeCounterWrapper#1
621 }
622
623 \NewDocumentCommand{\AdigraphNodesCounter}{> { \SplitList{;} } m}{%
624 \ProcessList{#1}{\AdigraphNodeCounterSecondWrapper}
625 }
626
627 \NewDocumentCommand{\AdigraphProcessNodes}{> { \SplitList{;} } m}{%
628 \ProcessList{#1}{\AdigraphNodeBuilder}
629 \forlistloop{\AdigraphDrawNode}{\AdigraphNodeList}
630 }
631
632 \NewDocumentCommand{\AdigraphProcessEdges}{> { \SplitList{;} } m}{%
633 \ProcessList{#1}{\AdigraphFirstEdgeRenormalizer}
634 }
635
636 \NewDocumentCommand{\AdigraphProcessPaths}{> { \SplitArgument{ 3 } {:} } m}{%
637 % #1 -> List of nodes
638 % #2 -> Units to send over path
639 % #3 -> Colors list
640 % #4 -> Width list
641 \stepcounter{AdigraphCurrentPathNumber}
642 \AdigraphBuildPath#1
643 }
644
645 \NewDocumentCommand{\AdigraphCountPaths}{m}{%
646 \stepcounter{AdigraphNumberOfPaths}
647 }
648
649 \NewDocumentCommand{\AdigraphCalculateInclination}{m m m}{}
650
651 \NewDocumentCommand{\AdigraphCalculateOrientation}{m m m}{%
652 \letcs{\firstX}{adigraphNode#2X}
653 \letcs{\firstY}{adigraphNode#2Y}
654 \letcs{\secondX}{adigraphNode#3X}
655 \letcs{\secondY}{adigraphNode#3Y}
656
657 \providebool{equalX}
658 \pgfmathsetmacro{\deltaX}{\secondX-\firstX}
659 \FPifeq{\deltaX}{0}
660 \booltrue{equalX}
661 \else
662 \boolfalse{equalX}
663 \fi
664
665
666 \ifbool{equalX}{%
667 \FPset{#1}{90}
668 }{%
669 \pgfmathsetmacro{#1}{57.2958*atan((\secondY-\firstY)/(\deltaX))}
670 }
671 }
672
673 \NewDocumentCommand{\AdigraphExecuteCutBuilder}{m m G{\AdigraphDefaultColor}}{%
674 \ifblank{#1}{%
675 % Reached end of list
676 }{%
677 \IfInteger{#1}{%
678 \IfEq{#1}{\AdigraphZero}{
679 \def\AdigraphFirstNode{\AdigraphTextualZero}%
680 }{
681 \def\AdigraphFirstNode{\AdigraphRom{#1}}%
682 }
683 }{%
684 \def\AdigraphFirstNode{#1}%
685 }%
686 \IfInteger{#2}{%
687 \IfEq{#2}{\AdigraphZero}{
688 \def\AdigraphSecondNode{\AdigraphTextualZero}%
689 }{
690 \def\AdigraphSecondNode{\AdigraphRom{#2}}%
691 }
692 }{%
693 \def\AdigraphSecondNode{#2}%
694 }%
695 \AdigraphCalculateOrientation{\orientation}{\AdigraphFirstNode}{\AdigraphSecondNode}
696 \draw[dashed,#3,rotate=\orientation] ([yshift=10pt]$(\AdigraphFirstNode)!0.7!(\AdigraphSecondNode)$) -- ([yshift=-10pt]$(\AdigraphFirstNode)!0.7!(\AdigraphSecondNode)$);
697 }
698 }
699
700 \NewDocumentCommand{\AdigraphCutBuilder}{> { \SplitArgument { 2 } { , } } m}{%
701 \AdigraphExecuteCutBuilder #1
702 }
703
704 \NewDocumentCommand{\AdigraphProcessCuts}{> { \SplitList{;} } m}{%
705 \ProcessList{#1}{\AdigraphCutBuilder}
706 }
707
708 \NewDocumentCommand{\Adigraph}{m m m m m m}{%
709 %
710 % #1 -> Vertices
711 % #2 -> Edges
712 % #3 -> Augmenting paths
713 % #4 -> Cuts
714 % #5 -> Current augmenting paths
715 % #6 -> Style
716 %
717 \ifbool{AdigraphDrawGraphs}{%
718 \def\AdigraphNodeList{}
719 \def\AdigraphEdgeList{}
720 \letcs{\AdigraphAugmentingPaths}{#3}
721 \begin{tikzpicture}
722 \tikzset{%
723 vertex/.style={circle,draw,minimum size=2em},
724 edge/.style={#6}
725 }
726
727 % vertices
728 \ifblank{#1}{%
729 % List is empty
730 }{%
731 \AdigraphNodesCounter{#1}
732 \AdigraphProcessNodes{#1}
733 }
734
735 \ifblank{#2}{%
736 % List is empty
737 }{%
738 \AdigraphProcessEdges{#2}
739 }
740
741 \ifblank{\AdigraphAugmentingPaths}{%
742 % List is empty
743 }{%
744 \ifblank{#5}{%
745 \defcounter{AdigraphNumberOfPaths}{100000}
746 }{%
747 }
748 \forlistloop{\AdigraphCountPaths}{\AdigraphAugmentingPaths}
749 \forlistloop{\AdigraphProcessPaths}{\AdigraphAugmentingPaths}
750 }
751
752 \ifblank{#2}{%
753 % List is empty
754 }{%
755 \forlistloop{\AdigraphEdgeDrawer}{\AdigraphEdgeList}
756 }
757
758 % %cuts
759 \ifblank{#4}{%
760 % List is empty
761 }{%
762 \AdigraphProcessCuts{#4}
763 }
764 \end{tikzpicture}
765 }{}
766 }
767
768 \NewDocumentCommand{\AdigraphProcessAugmentingPaths}{m}{}
769 \NewDocumentCommand{\AdigraphProcessAugmentingPathsList}{m}{}
770
771 \NewDocumentCommand{\NewAdigraph}{m m G{} O{->,> = latex}}{%
772 %
773 % #1 -> Variable to assign to as command
774 % #2 -> Nodes
775 % #3 -> Edges
776 % #4 -> Style
777 %
778 \ifcsdef{#1}{
779 \PackageError{adigraph}{%
780 You have already define an adigraph called #1.
781 }{%
782 Identify the duplicate and change its name, e.g. Second#1
783 }
784 }{
785 \csdef{Adigraph#1AugmentingPaths}{}%
786 \RenewDocumentCommand{\AdigraphProcessAugmentingPaths}{m}{%
787 \listcsgadd{Adigraph#1AugmentingPaths}{##1}
788 }
789 \RenewDocumentCommand{\AdigraphProcessAugmentingPathsList}{> { \SplitList{;} } m}{%
790 \ProcessList{##1}{\AdigraphProcessAugmentingPaths}
791 }
792 \expandafter\NewDocumentCommand\expandafter{\csname #1\endcsname}{m G{}}{%
793 %
794 % ##1 -> Augmenting path
795 %
796 \setcounter{AdigraphCurrentNodeCounter}{0}
797 \setcounter{AdigraphTotalNodeCounter}{0}
798 \setcounter{AdigraphNumberOfPaths}{0}
799 \setcounter{AdigraphCurrentPathNumber}{0}
800 \AdigraphProcessAugmentingPathsList{##1}
801 \Adigraph{#2}{#3}{Adigraph#1AugmentingPaths}{##2}{##1}{#4}
802 }%
803 }
804 }
805
806 \NewDocumentCommand{\RenewAdigraph}{m m G{} O{->,> = latex}}{%
807 %
808 % #1 -> Variable to assign to as command
809 % #2 -> Nodes
810 % #3 -> Edges
811 % #4 -> Style
812 %
813 \ifcsdef{#1}{
814 \csdef{Adigraph#1AugmentingPaths}{}%
815 \RenewDocumentCommand{\AdigraphProcessAugmentingPaths}{m}{%
816 \listcsgadd{Adigraph#1AugmentingPaths}{##1}
817 }
818 \RenewDocumentCommand{\AdigraphProcessAugmentingPathsList}{> { \SplitList{;} } m}{%
819 \AdigraphProcessAugmentingPaths##1
820 }
821 \expandafter\RenewDocumentCommand\expandafter{\csname #1\endcsname}{m G{}}{%
822 %
823 % ##1 -> Augmenting path
824 %
825 \setcounter{AdigraphCurrentNodeCounter}{0}
826 \setcounter{AdigraphTotalNodeCounter}{0}
827 \setcounter{AdigraphNumberOfPaths}{0}
828 \setcounter{AdigraphCurrentPathNumber}{0}
829 \AdigraphProcessAugmentingPathsList{##1}
830 \Adigraph{#2}{#3}{Adigraph#1AugmentingPaths}{##2}{##1}{#4}
831 }%
832 }{
833 \PackageError{adigraph}{%
834 You cannot renew a graph called #1 without having it defined first.
835 }{%
836 Create the first graph using \textbackslash NewAdigraph
837 }
838 }
839 }
840
841 \NewDocumentCommand{\DisableAdigraphs}{}{%
842 \boolfalse{AdigraphDrawGraphs}
843 }
844
845 \NewDocumentCommand{\EnableAdigraphs}{}{%
846 \booltrue{AdigraphDrawGraphs}
847 }
848
849
850 \endinput
851 %%
852 %% End of file `adigraph.sty'.