%
% Copyright 2018 Luca Cappelletti
%
% 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:
%
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%
% 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.
%
\def\AdigraphVersionNumber{v1.7.2}
\NewDocumentCommand{\AdigraphMemorizeEdge}{m m G{\AdigraphDefaultColor} G{\AdigraphDefaultWidth}}{}
\NewDocumentCommand{\AdigraphBuildEdge}{> {\SplitArgument{3}{,}} m m m m}{%
%
% #1 -> List of edge nodes, its color and width
% #2 -> Weight
% #3 -> Label, defaults to weight
% #4 -> Label position, defaults to midway
%
\RenewDocumentCommand{\AdigraphMemorizeEdge}{m m G{\AdigraphDefaultColor} G{\AdigraphDefaultWidth}}{%
%
% ##1 -> first edge
% ##2 -> second edge
% ##3 -> color
% ##4 -> width
%
\IfInteger{##1}{%
\IfEq{##1}{\AdigraphZero}{
\csedef{AdigraphFirstNode}{\AdigraphTextualZero}%
}{
\csedef{AdigraphFirstNode}{\AdigraphRom{##1}}%
}
}{%
\csedef{AdigraphFirstNode}{##1}%
}%
\IfInteger{##2}{%
\IfEq{##2}{\AdigraphZero}{
\csedef{AdigraphSecondNode}{\AdigraphTextualZero}%
}{
\csedef{AdigraphSecondNode}{\AdigraphRom{##2}}%
}
}{%
\csedef{AdigraphSecondNode}{##2}%
}%
\xifinlist{\AdigraphFirstNode,\AdigraphSecondNode}{\AdigraphEdgeList}{
%This edge was previously added
}{
\xifinlist{\AdigraphFirstNode}{\AdigraphNodeList}{
\xifinlist{\AdigraphSecondNode}{\AdigraphNodeList}{
%This edge is new
\csedef{AdigraphEdgeName}{\AdigraphFirstNode v\AdigraphSecondNode}
\listxadd{\AdigraphEdgeList}{\AdigraphFirstNode,\AdigraphSecondNode}
\newbool{adigraphEdge\AdigraphEdgeName HasLabel}
\newbool{adigraphEdge\AdigraphEdgeName HasEmptyLabel}
\newbool{adigraphEdge\AdigraphEdgeName Drawn}
\ifblank{#3}{%
\boolfalse{adigraphEdge\AdigraphEdgeName HasLabel}
}{%
\booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
}%
\ifblank{#2}{%
\booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
\booltrue{adigraphEdge\AdigraphEdgeName HasEmptyLabel}
\csdef{adigraphEdge\AdigraphEdgeName Weight}{1}%
}{%
\csdef{adigraphEdge\AdigraphEdgeName Weight}{#2}%
}%
\csdef{adigraphEdge\AdigraphEdgeName Label}{#3}%
\ifblank{##3}{
\csdef{adigraphEdge\AdigraphEdgeName Color}{\AdigraphDefaultColor}%
}{
\csdef{adigraphEdge\AdigraphEdgeName Color}{##3}%
}
\ifblank{##4}{
\csdef{adigraphEdge\AdigraphEdgeName Width}{\AdigraphDefaultWidth}%
}{
\csdef{adigraphEdge\AdigraphEdgeName Width}{##4}%
}
\csdef{adigraphEdge\AdigraphEdgeName LabelPosition}{#4}%
}{
\PackageError{adigraph}{%
The given edge ends in the node ##2, that does not exist.
}{%
You have to create the node ##2.
}
}
}{
\PackageError{adigraph}{%
The given edge starts from the node ##1, that does not exist.
}{%
You have to create the node ##1.
}
}
}
}
\AdigraphMemorizeEdge#1
}
\NewDocumentCommand{\AdigraphBuildEdgeWrapper}{m G{} G{} G{midway}}{%
%
% #1 -> List of edge nodes and its color
% #2 -> Weight
% #3 -> Label
% #4 -> Label position
%
\ifblank{#1}{%
%Do nothing, this is the tail of the list
}{%
\AdigraphBuildEdge{#1}{#2}{#3}{#4}
}
}
\NewDocumentCommand{\AdigraphElaboratePath}{m m G{\AdigraphDefaultColor}}{}
\NewDocumentCommand{\AdigraphSimpleSum}{m m m}{%
%
% #1 -> First addendum
% #2 -> Second addendum
% #3 -> Variable to store sum
%
\pgfmathsetmacro{\sat}{#1+#2}
\pgfmathfrac{\sat}
\IfEq{\pgfmathresult}{0.0}{
\pgfmathint{\sat}
\FPset{#3}{\pgfmathresult}
}{
\FPset{#3}{\sat}
}
}
\NewDocumentCommand{\AdigraphPathBuilder}{m m m m m m m}{%
%
% #1 -> first node
% #2 -> second node
% #3 -> units
% #4 -> forward color
% #5 -> backward color
% #6 -> forward width
% #7 -> backward width
%
\ifcsdef{adigraphEdge#1v#2Weight}{%
\letcs{\AdigraphWeightA}{adigraphEdge#1v#2Weight}
}{%
\def\AdigraphWeightA{0}
\newbool{adigraphEdge#1v#2HasLabel}
\newbool{adigraphEdge#1v#2HasEmptyLabel}
\ifcsdef{adigraphEdge#2v#1LabelPosition}{%
\csdef{adigraphEdge#1v#2LabelPosition}{\csuse{adigraphEdge#2v#1LabelPosition}}
}{%
\csdef{adigraphEdge#1v#2LabelPosition}{midway}
}
\csdef{adigraphEdge#1v#2LabelPosition}{midway}
\newbool{adigraphEdge#1v#2Drawn}
\listgadd{\AdigraphEdgeList}{#1,#2}
}
\ifcsdef{adigraphEdge#2v#1Weight}{%
\letcs{\AdigraphWeightB}{adigraphEdge#2v#1Weight}
}{%
\def\AdigraphWeightB{0}
\newbool{adigraphEdge#2v#1HasLabel}
\newbool{adigraphEdge#2v#1HasEmptyLabel}
\ifcsdef{adigraphEdge#1v#2LabelPosition}{%
\csdef{adigraphEdge#2v#1LabelPosition}{\csuse{adigraphEdge#1v#2LabelPosition}}
}{%
\csdef{adigraphEdge#2v#1LabelPosition}{midway}
}
\newbool{adigraphEdge#2v#1Drawn}
\listgadd{\AdigraphEdgeList}{#2,#1}
}
\RenewDocumentCommand{\AdigraphElaboratePath}{m}{%
\IfInteger{##1}{%
\IfEq{##1}{\AdigraphZero}{
\def\AdigraphCurrentNode{\AdigraphTextualZero}%
}{
\def\AdigraphCurrentNode{\AdigraphRom{##1}}%
}
}{%
\def\AdigraphCurrentNode{##1}%
}%
\ifdef{\AdigraphLastParsedNode}{%
\expandafter\expandafter\expandafter\AdigraphPathBuilder\expandafter\expandafter\expandafter{\expandafter\AdigraphLastParsedNode\expandafter}\expandafter{\AdigraphCurrentNode}{\units}{\AdigraphForwardPathColor}{\AdigraphBackwardPathColor}{\AdigraphBackwardPathWidth}{\AdigraphBackwardPathWidth}
}{%
% This is the first iteration
}
\IfInteger{##1}{%
\IfEq{##1}{\AdigraphZero}{
\def\AdigraphLastParsedNode{\AdigraphTextualZero}%
}{
\def\AdigraphLastParsedNode{\AdigraphRom{##1}}%
}
}{%
\def\AdigraphLastParsedNode{##1}%
}%
}
\ProcessList{#1}{\AdigraphElaboratePath}
\undef{\AdigraphLastParsedNode}
}
\NewDocumentCommand{\AdigraphBuildNodeWrapper}{m m G{}}{
% #1 -> given node name, not normalized, color and width
% #2 -> list of coordinates
% #3 -> optional label
\ifblank{#1}{
%end of list
}{
\AdigraphBuildNode{#1}{#2}{#3}
}
}
\NewDocumentCommand{\AdigraphApplyKleenePlusEdgeBuilder}{m}{%
%
% #1 -> element
%
\ifblank{#1}{
%end of list
}{
\IfSubStr{\AdigraphCurrentElaboratingEdge}{,#1,}{
%The node is already contained
}{
\IfSubStr{\AdigraphCurrentElaboratingEdge}{,#1}{
%The node is already contained
}{
\IfSubStr{\AdigraphCurrentElaboratingEdge}{#1,}{
\StrPosition{\AdigraphCurrentElaboratingEdge}{#1,}[\stringPosition]
\ifnumequal{\stringPosition}{1}{
%This is at the beginning of the list
}{
\StrSubstitute[1]{\AdigraphCurrentElaboratingEdge}{+}{#1}[\temp]%
\expandafter\AdigraphFirstEdgeRenormalizer\expandafter{\temp}
}
}{
\StrSubstitute[1]{\AdigraphCurrentElaboratingEdge}{+}{#1}[\temp]%
\expandafter\AdigraphFirstEdgeRenormalizer\expandafter{\temp}
}
}
}
}
}
\NewDocumentCommand{\AdigraphSecondEdgeRenormalizer}{m}{%
\xifinlist{+}{\AdigraphNodeList}{
%The plus is not a kleene sign, but a symbol for a node
\AdigraphEdgeBuilder{#1}
}{
\IfSubStr{#1}{+}{
\def\AdigraphCurrentElaboratingEdge{#1}
\forlistloop{\AdigraphKleenePlusEdgeBuilder}{\AdigraphNodeList}
}{
\AdigraphEdgeBuilder{#1}
}
}
}
\NewDocumentCommand{\AdigraphFirstEdgeRenormalizer}{m}{%
\xifinlist{*}{\AdigraphNodeList}{
%The asterisk is not a kleene sign, but a symbol for a node
\AdigraphSecondEdgeRenormalizer{#1}
}{
\IfSubStr{#1}{*}{
\def\AdigraphCurrentElaboratingEdge{#1}
\forlistloop{\AdigraphKleeneStarEdgeBuilder}{\AdigraphNodeList}
}{
\AdigraphSecondEdgeRenormalizer{#1}
}
}
}
\NewDocumentCommand{\AdigraphProcessPaths}{> { \SplitArgument{ 3 } {:} } m}{%
% #1 -> List of nodes
% #2 -> Units to send over path
% #3 -> Colors list
% #4 -> Width list
\stepcounter{AdigraphCurrentPathNumber}
\AdigraphBuildPath#1
}