%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%LINK PATTERN ROUTINE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% requires tikz (calc,decorations.markings,positioning,fit). uses pgfkeys for optional args
%
%
%TODO
%need to rationalize use of counters vs macros
%
\makeatletter
\newcommand{\gettikzxy}[3]{% returns coordinates
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
%  \edef#2{\the\pgf@x}%
%  \edef#3{\the\pgf@y}%
\pgfmathsetmacro{#2}{\the\pgf@x/\linkpatternunit}
\pgfmathsetmacro{#3}{\the\pgf@y/\linkpatternunit}
}
% fixes the anchoring issue with labels
% what it does is as follows:
% - suppresses the automatic label anchor placement (the anchor is the place of the border of the label which gets glued to the main node)
% - allows to manually specify an angle or other placement choice for label anchor
% - by default, makes the anchor the center of the label (which looks really ugly)
% - drawback: the auto placement is clever and in particular knows about global transformations. we don't...
\tikzset{label anchor/.code={%
    \let\tikz@auto@anchor=\pgfutil@empty
    \def\tikz@anchor{#1}
  },
  label anchor/.default=center
}
\makeatother
%
% a useful macro
\tikzset{arrow/.style={postaction={decorate,thick,decoration={markings,mark = at position #1 with {\arrow{>}}}}},arrow/.default=0.5}
\tikzset{invarrow/.style={postaction={decorate,thick,decoration={markings,mark = at position #1 with {\arrow{<}}}}},invarrow/.default=0.5}
%
\newdimen\linkpatternunit%
\newcount\linkpatternsize%
\newcount\lpsize%the global internal variable... ugly but \foreach forces us to use a global variable
%
%the optional arguments
\newif\iflinkpatterninverted%mirror symmetry
\newif\iflinkpatterntikzstarted%in principle, shouldn't be needed -- tests itself
\newif\iflinkpatternboxed%draw a box around link patterns/tangles
\newif\iflinkpatternaxis%draw a line at starting/endpoints
\newif\iflinkpatternstraightlines%draw pipedream style
\newif\iflinkpatternnumbered%write labels
\newif\iflinkpatternalias%the vertices can be accessed via their label rather than their normal indexation. turn off for complicated labels
\newif\iflinkpatternnode%creates a node surrounding the linkpattern
\newif\iflinkpatterncentered%automatically centers the picture if outside \tikz{}
%
\newcount\linkpatternfused%for fused link patterns, level of fusion
%
%change dir to /linkpattern, then list of optional arguments
%note the use of /.style vs /.code: /.style means execute a series of pgfkeys, whereas /.code means execute a series of latex commands
\pgfkeys{/linkpattern/.cd,centered/.is if=linkpatterncentered,inverted/.is if=linkpatterninverted,numbered/.is if=linkpatternnumbered,tikzstarted/.is if=linkpatterntikzstarted,straight lines/.is if=linkpatternstraightlines,boxed/.is if=linkpatternboxed,axis/.is if=linkpatternaxis,vertexcolor/.store in=\linkpatternvertexcolor,edgecolor/.store in=\linkpatternedgecolor,boxcolor/.code={\linkpatternboxedtrue\def\linkpatternboxcolor{#1}},tikzoptions/.style={every linkpattern/.append style={#1}},size/.code={\linkpatternsize=#1},numbering0/.code={\def\lpnumbering{#1}\def\linkpatternnumbering{#1}},numbering/.style={numbered,numbering0={#1}},unit/.code={\linkpatternunit=#1},height/.store in=\linkpatternheight,shape/.store in=\linkpatternshape,looseness/.store in=\linkpatternlooseness,squareness/.store in=\linkpatternsquareness,extra space/.store in=\linkpatternextraspace,width/.store in=\linkpatternwidth,alias/.is if=linkpatternalias,pos/.store in=\linkpatternpos,labeloptions/.style={labeloptionslist/.append style={#1}},labeloptionslist/.style={inner sep=2pt,font=\scriptsize,execute at begin node=$,execute at end node=$,label anchor={#1+180}},nodeon/.is if=linkpatternnode,node/.style={nodeon,nodeoptionslist/.append style={#1}},nodeoptionslist/.style={},
%a few predefined styles
pipedream/.style={shape=pipedream,looseness=0,straight lines,numbering0=tangle},
tangle/.style={shape=tangle,numbering0=tangle},
%general stuff
every linkpattern/.style={x=\linkpatternunit,y=\linkpatternunit},
fused/.code={\linkpatternfused=#1},%semi-implemented
%
%vertex/.style={circle,thin,solid,draw=black,fill=\linkpatternvertexcolor,inner sep=0.2pt+0.07\linkpatternunit,draw opacity=1,transform shape},%note the transform shape to avoid all kinds of pbls. but! it makes the size of vertices vary with "scale"
vertex/.style={circle,thin,solid,draw=black,fill=\linkpatternvertexcolor,inner sep=1.5pt,draw opacity=1,transform shape},%note the transform shape to avoid all kinds of pbls
edge/.style={very thick,solid,draw=\linkpatternedgecolor,draw opacity=1}}
%
%default values *if arguments are not mentioned*
%these are global variables that can in principle be modified in the program
\linkpatterncenteredfalse%
\linkpatterninvertedfalse%
\linkpatternnumberedfalse%
\linkpatterntikzstartedfalse%
\linkpatternboxedfalse%
\linkpatternaxistrue%
\linkpatternaliastrue%
\linkpatternunit=0.6cm%
\linkpatternsize=0%
\linkpatternfused=1%
\linkpatternstraightlinesfalse%
\def\linkpatternlooseness{0.2}% how much arches go straight to their partner
\def\linkpatternsquareness{0.35}% how much arches go orthogonally
\def\linkpatternvertexcolor{red}%
\def\linkpatternedgecolor{blue}%
\def\linkpatternboxcolor{none}%
\def\linkpatternheight{0}
\def\linkpatternwidth{0}
\def\linkpatternshape{default}
\def\linkpatternnumbering{default}
\def\linkpatternpos{(0,0)}
\def\linkpatternextraspace{0}
\linkpatternnodefalse
%same, these can be modified in principle -- though this is not very clean code... should be hidden in /linkpattern and command line options should modify them
%
%
\def\firstchar#1#2\empty{#1}%
% generic mechanism to modify output depending on \linkpattern#2
\def\linkpatterndo#1#2{
\edef\param{\csname linkpattern#2\endcsname}
\edef\firstcharparam{\expandafter\firstchar\param\empty}
\expandafter\ifcat\firstcharparam a
\expandafter\ifx\csname linkpattern#1\param\endcsname\relax
%we got something unknown
\csname linkpattern#1unknown\endcsname
\else
\csname linkpattern#1\csname linkpattern#2\endcsname\endcsname
\fi
\else
%we got something unknown
\csname linkpattern#1unknown\endcsname
\fi
}%
% geometry macros
% these take argument \x = vertex number and spit out coordinate and angle
\def\linkpatterncoorddefault{\xdef\lpcoordx{\x}\xdef\lpcoordy{0}\xdef\lpangle{90}}%
\def\linkpatterncoordtangle{\ifnum\x>\lphalfsize\pgfmathparse{\lpsize+1-\x}\xdef\lpcoordx{\pgfmathresult}\xdef\lpcoordy{\lpheight}\xdef\lpangle{270}\else\xdef\lpcoordx{\x}\xdef\lpcoordy{-\lpheight}\xdef\lpangle{90}\fi}
\def\linkpatterncoordcircle{%
 \pgfmathparse{180+(\x-1)*360/\lpsize}
 \xdef\lpangle{\pgfmathresult}
 \pgfmathparse{-cos(\lpangle)}
\xdef\lpcoordx{\pgfmathresult}
 \pgfmathparse{-sin(\lpangle)}
 \xdef\lpcoordy{\pgfmathresult}
}%
\def\linkpatterncoordpipedream{\ifnum\x>\lphalfsize\pgfmathparse{\lpsize+1-\x-0.5}\xdef\lpcoordx{\pgfmathresult}\xdef\lpcoordy{0}\xdef\lpangle{270}\else\pgfmathparse{0.5-\x}\xdef\lpcoordy{\pgfmathresult}\xdef\lpcoordx{0}\xdef\lpangle{0}\fi}
\def\linkpatterncoordrectangle{
\ifnum\x>\lptqsize
\pgfmathparse{\lpsize+1-\x-0.5}\xdef\lpcoordx{\pgfmathresult}\xdef\lpcoordy{0}\xdef\lpangle{270}
\else\ifnum\x>\lphalfsize
\pgfmathparse{\x-\lptqsize-0.5}\xdef\lpcoordy{\pgfmathresult}\xdef\lpcoordx{\linkpatternwidth}\xdef\lpangle{180}
\else\ifnum\x>\linkpatternheight
\pgfmathparse{\x-\linkpatternheight-0.5}\xdef\lpcoordx{\pgfmathresult}\xdef\lpcoordy{-\linkpatternheight}\xdef\lpangle{90}
\else
\pgfmathparse{0.5-\x}\xdef\lpcoordy{\pgfmathresult}\xdef\lpcoordx{0}\xdef\lpangle{0}
\fi\fi\fi
}%
\def\linkpatterncoordunknown{\message{link pattern: unknown shape}}%
%
% draw axis
\def\linkpatterndrawaxisdefault{\draw (1-\linkpatternextraspace,0) -- (\lpsize+\linkpatternextraspace,0);}%
\def\linkpatterndrawaxistangle{\draw (1-\linkpatternextraspace,-\lpheight) -- (\lphalfsize+\linkpatternextraspace,-\lpheight) (1-\linkpatternextraspace,\lpheight) -- (\lphalfsize+\linkpatternextraspace,\lpheight);}%
\def\linkpatterndrawaxiscircle{\draw (0,0) circle (1);}%
\def\linkpatterndrawaxispipedream{\draw (0,-\lphalfsize) -- (0,0) -- (\lphalfsize,0);}%
\def\linkpatterndrawaxisrectangle{\draw (0,-\linkpatternheight) rectangle (\linkpatternwidth,0);}%
\def\linkpatterndrawaxisunknown{\message{link pattern: unknown shape}}%
%
% draw box
\def\linkpatterndrawboxdefault{\draw[fill=\linkpatternboxcolor] (0.5-\linkpatternextraspace,0) rectangle (\lpsize+0.5+\linkpatternextraspace,\lpheight);}%
\def\linkpatterndrawboxtangle{\draw[fill=\linkpatternboxcolor] (0.5-\linkpatternextraspace,-\lpheight) rectangle (\lphalfsize+0.5+\linkpatternextraspace,\lpheight);}
\def\linkpatterndrawboxcircle{\draw[fill=\linkpatternboxcolor] (0,0) circle (1);}%
\def\linkpatterndrawboxunknown{\message{link pattern: unknown shape}}%
\def\linkpatterndrawboxpipedream{\draw[fill=\linkpatternboxcolor] (0,-\lphalfsize) rectangle (\lphalfsize,0);}%
\def\linkpatterndrawboxrectangle{\draw[fill=\linkpatternboxcolor] (0,-\linkpatternheight) rectangle (\linkpatternwidth,0);}%
%
% set size if determined by the geometry
\def\linkpatternsetsizeunknown{
\global\lpsize=\linkpatternsize%this is the size as given by option size
%}%
%\def\linkpatternsetsizetangle{
%\global\lpsize=\linkpatternsize%this is the size as given by option size
%but we also want to fix height if not set manually (useful only for tangles and boxed link patterns)
\if\linkpatternheight0
\xdef\maxsep{0}
\foreach \x/\xx in \mylist%
{%
\edef\tempx{\withoutprime{\x}}
\edef\tempxx{\withoutprime{\xx}}
\pgfmathparse{max(\maxsep,abs(\tempx-\tempxx))}
\xdef\maxsep{\pgfmathresult}
}%
\pgfmathparse{0.25+0.8*\linkpatternsquareness*\maxsep}
\xdef\lpheight{\pgfmathresult}
\else
\xdef\lpheight{\linkpatternheight}
\fi
}
%
\def\linkpatternsetsizerectangle{
\pgfmathtruncatemacro{\tempsize}{2*\linkpatternwidth+2*\linkpatternheight}%lame
\global\lpsize=\tempsize
\pgfmathtruncatemacro{\tempsize}{\linkpatternwidth+2*\linkpatternheight}
\xdef\lptqsize{\tempsize}%why one counter and one macro??
}%
%
% numbering macros
% try to guess size
\newcount\tempsize
\def\linkpatternrightmostunknown{
\global\lpsize=0
\global\tempsize=0
\foreach\x/\labx in \linkpatternnumbering
{
\edef\tempx{\withoutprime{\x}}%not great... (prime not treated)
\ifnum\lpsize<\tempx\global\lpsize=\tempx\fi
\global\advance\tempsize by 1
}
\ifnum\tempsize>\lpsize\global\lpsize=\tempsize\fi
}%
\def\linkpatternrightmostdefault{
\global\lpsize=0
\global\tempsize=0
\foreach \x/\y in \mylist
{
\edef\tempx{\withoutprime{\x}}%not great... (prime not treated)
\ifnum\lpsize<\tempx\global\lpsize=\tempx\fi
\ifx\x\y
\global\advance\tempsize by 1
\else
\edef\tempy{\withoutprime{\y}}
\ifnum\lpsize<\tempy\global\lpsize=\tempy\fi%
\global\advance\tempsize by 2
\fi
}
\ifnum\tempsize>\lpsize\global\lpsize=\tempsize\fi
}%
\def\linkpatternrightmosttangle{
\global\lpsize=0
\global\tempsize=0
\foreach \x/\y in \mylist
{
\edef\tempx{\withoutprime{\x}}
\ifnum\lpsize<\tempx\global\lpsize=\tempx\fi
\ifx\x\y
\global\advance\tempsize by 1
\else
\edef\tempy{\withoutprime{\y}}
\ifnum\lpsize<\tempy\global\lpsize=\tempy\fi%
\global\advance\tempsize by 2
\fi
}
\global\advance\lpsize by\lpsize% not so great either: guess doubling because of 's
\ifnum\tempsize>\lpsize\global\lpsize=\tempsize\fi
}%
\def\linkpatternrightmosthalftangle{\linkpatternrightmosttangle}
%
% numbering. 
\def\linkpatternnumberingdefault{\xdef\lpnumbering{1,...,\lpsize}}%
\def\linkpatternnumberingtangle{%
%have to do it manually...
\xdef\lpnumbering{1,...,\lphalfsize}
\foreach\x in {1,...,\lphalfsize}
{\xdef\lpnumbering{\lpnumbering,\x'}}
}%
\def\linkpatternnumberinghalftangle{%
%have to do it manually...
\xdef\lpnumbering{1,...,\lphalfsize}
\foreach\x in {1,...,\lphalfsize}
{\xdef\lpnumbering{\lpnumbering,\x'/}}
}%
\def\linkpatternnumberingunknown{%
%manual numbering. nothing to do
}%
%
\newcommand\linkpattern[2][]{%universal command
{%we want the macro modifications to be local only
\pgfkeys{/linkpattern/.cd,#1}%parse the list of optional arguments
%
\edef\mylist{#2}%to avoid some annoying bugs
%
\def\primetest##1'{}%
\def\hasaprime##1{\expandafter\primetest##1''}%aha!
\def\internalwithoutprime##1'{##1}%
\def\withoutprime##1{\if\hasaprime##1 %
\expandafter\internalwithoutprime##1\else ##1\fi}%
%
\iflinkpatternnumbered%
\iflinkpatterninverted%the label anchoring hack forces us to distinguish cases
\tikzset{/linkpattern/lbl/.style n args={3}{label={[/linkpattern/labeloptionslist=-##1,##3] ##1:##2}}}%
\else%
\tikzset{/linkpattern/lbl/.style n args={3}{label={[/linkpattern/labeloptionslist=##1,##3] ##1:##2}}}%
\fi%
\else%
\tikzset{/linkpattern/lbl/.style={}}%
\fi%
\tikzifinpicture{\linkpatterntikzstartedtrue%
\begin{scope}[shift=\linkpatternpos,/linkpattern/every linkpattern]%should it be there? can't decide. and order of shift and every...
}{%
\linkpatterntikzstartedfalse%
\iflinkpatterncentered
\begin{tikzpicture}[baseline=(current  bounding  box.center),/linkpattern/every linkpattern]%
\else
\begin{tikzpicture}[baseline=0,/linkpattern/every linkpattern]%
\fi
}%
\begin{scope}[local bounding box=link pattern box]
\iflinkpatterninverted%
\begin{scope}[yscale=-1]%
\fi%
\linkpatterndo{setsize}{shape}%in case geometry dictates size
\ifnum\lpsize=0
%otherwise try to guess size
%by finding rightmost vertex: we do it differently in case of renumbering
\linkpatterndo{rightmost}{numbering}
\fi
\pgfmathtruncatemacro{\lphalfsize}{\lpsize/2}%should probably be transferred somewhere above
%now do the automatic numbering if needed
\linkpatterndo{numbering}{numbering}
%draw line or box (depending on shape)
\iflinkpatternboxed
\linkpatterndo{drawbox}{shape}
\else
\iflinkpatternaxis
\linkpatterndo{drawaxis}{shape}
\fi
\fi
%now place vertices
\foreach\xx/\xlab/\opt in \lpnumbering
{
\ifx\xlab\opt\def\opt{}\fi%if no option avoid repeat of \xlab
%tangle type numbering also tolerated
\if\hasaprime\xx %
\pgfmathtruncatemacro{\xx}{\lpsize+1-\withoutprime{\xx}}
\fi
%
%
% primitive way of getting fused to work. should not be used for exotic geometries
\ifnum\linkpatternfused>1
\pgfmathsetmacro{\x}{0.4*(0.5+\linkpatternfused*(0.5+floor((\xx-1)/\linkpatternfused)))+0.6*\xx}
\else
\def\x{\xx}
\fi
%
\linkpatterndo{coord}{shape}
%
\iflinkpatternalias\def\xlabb{\xlab}\else\def\xlabb{\xx}\fi
\path (\lpcoordx,\lpcoordy) coordinate[/linkpattern/vertex,/linkpattern/lbl={\lpangle+180}{\xlab}{\opt},alias=v\xlabb] (v\xx) ++(\lpangle:\linkpatternunit) coordinate[alias=vv\xlabb] (vv\xx); 
%coordinate vv is there only for direction purposes (lame)
}
%finally, draw edges
\foreach \a/\b/\c in \mylist
{
\if\hasaprime\a %
\pgfmathtruncatemacro{\a}{\lpsize+1-\withoutprime{\a}}
\fi
\ifx\b\c\def\c{}\fi%set option to empty if they're not there
\draw[/linkpattern/edge]
\ifx\a\b
%singleton
(v\a)
\c
--
++(0,\lpheight);
\else
%pair
\pgfextra{
\if\hasaprime\b %
\pgfmathtruncatemacro{\b}{\lpsize+1-\withoutprime{\b}}
\fi
\gettikzxy{(v\a)}{\ax}{\ay}
\gettikzxy{(v\b)}{\bx}{\by}
\gettikzxy{(vv\a)}{\axx}{\ayy}
\gettikzxy{(vv\b)}{\bxx}{\byy}
\pgfmathsetmacro{\dist}{sqrt((\ax-\bx)*(\ax-\bx)+(\ay-\by)*(\ay-\by))}
\pgfmathsetmacro{\abx}{(\axx-\ax)*\dist*\linkpatternsquareness+(\bx-\ax)*\linkpatternlooseness)}
\pgfmathsetmacro{\aby}{(\ayy-\ay)*\dist*\linkpatternsquareness+(\by-\ay)*\linkpatternlooseness)}
\pgfmathsetmacro{\bax}{(\bxx-\bx)*\dist*\linkpatternsquareness+(\ax-\bx)*\linkpatternlooseness)}
\pgfmathsetmacro{\bay}{(\byy-\by)*\dist*\linkpatternsquareness+(\ay-\by)*\linkpatternlooseness)}
}
(v\a)
\c%option
\iflinkpatternstraightlines
\pgfextra{
%straight lines: need to compute an intersection a,a+ab inter b,b+ba
\pgfmathsetmacro{\t}{((\ax-\bx)*\bay-(\ay-\by)*\bax)/(\aby*\bax-\abx*\bay)}
\pgfmathsetmacro{\abx}{\t*\abx}
\pgfmathsetmacro{\aby}{\t*\aby}
}
[rounded corners=0.2\linkpatternunit] -- ++(\abx,\aby) -- (v\b);
\else
%round lines
.. controls ++(\abx,\aby) and ++(\bax,\bay) .. 
\fi
(v\b);
\fi
}
\end{scope}
\iflinkpatternnode
\node[fit=(link pattern box),/linkpattern/nodeoptionslist] {};
\fi
\iflinkpatterninverted
\end{scope}
\fi
\iflinkpatterntikzstarted
\end{scope}
\else%
\end{tikzpicture}%
\fi%
}}%
%
% end main command
%
% convenient for putting a tangle and a link pattern on top of each other
\newcommand\tanglelinkpattern[3][]{%
{%we want the macro modifications to be local only
\pgfkeys{/linkpattern/.cd,#1}
\iflinkpatterninverted
\begin{tikzpicture}[/linkpattern/every linkpattern,baseline=\linkpatternunit]%
\else
\begin{tikzpicture}[/linkpattern/every linkpattern,baseline=-\linkpatternunit]%
\fi
\linkpattern[#1,tikzstarted,numbered=false]{#3}
\pgfmathtruncatemacro{\lptempsize}{2*\linkpatternsize}
%
\iflinkpatterninverted
\begin{scope}[yshift=0.5*\linkpatternunit]
\else
\begin{scope}[yshift=-0.5*\linkpatternunit]
\fi
\linkpattern[tangle,#1,tikzstarted,size=\lptempsize,
numbering=halftangle,
height=0.5]{#2}
\end{scope}
\end{tikzpicture}%
}}
%
%
% schubert variety equations diagrams
\newcommand\diag[4][]{%
\pgfkeys{/linkpattern/.cd,#1}%parse the list of optional arguments
\iflinkpatterntikzstarted\else%
\begin{tikzpicture}[scale=0.5]%TEMP
\fi%
\iflinkpatterninverted%
\begin{scope}[yscale=-1]%
\fi%
\draw (0,0) grid (#2,#3);
\edef\mylist{#4}%to avoid some annoying bugs
\foreach\y/\x/\z in \mylist
{
\ifx\x\z
\draw[decorate,decoration={zigzag,
amplitude=1pt,segment length=5pt}]
(\x-0.5,#3) -- (\x-0.5,\y-0.5) node[circle,fill=black,inner sep=2pt] {} -- (#2,\y-0.5);
\else
\node at (\x-0.5,\y-0.5) {$\z$};
\fi
}
\iflinkpatterninverted
\end{scope}
\fi
\iflinkpatterntikzstarted\else%
\end{tikzpicture}%
\fi%
}
%
%boundary link pattern stuff
\makeatletter
\tikzset{circle split part fill/.style  args={#1,#2}{%
 alias=tmp@name,
  postaction={%
    insert path={
     \pgfextra{% 
     \pgfpointdiff{\pgfpointanchor{\pgf@node@name}{center}}%
                  {\pgfpointanchor{\pgf@node@name}{east}}%            
     \pgfmathsetmacro\insiderad{\pgf@x}
      \fill[#1] (\pgf@node@name.base) ([xshift=-\pgflinewidth]\pgf@node@name.east) arc
                          (0:180:\insiderad-\pgflinewidth)--cycle;
      \fill[#2] (\pgf@node@name.base) ([xshift=\pgflinewidth]\pgf@node@name.west)  arc
                           (180:360:\insiderad-\pgflinewidth)--cycle;                    }}}}}  
 \makeatother
\tikzset{bdot/.style={circle,circle split,draw,circle split part fill={black,white},thin,inner sep=1pt}}%
\tikzset{wdot/.style={circle,circle split,draw,circle split part fill={white,black},thin,inner sep=1pt}}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% OUTDATED: circlelinkpattern kept because fused hasn't been reimplemented
%
\newcommand\circlelinkpattern[2][]{%in principle, could be merged
{%we want the macro modifications to be local only
\pgfkeys{/linkpattern/.cd,#1}%parse the list of optional arguments
\iflinkpatterntikzstarted\else%
\begin{tikzpicture}[/linkpattern/every linkpattern]%
\fi%
\iflinkpatterninverted%
\begin{scope}[yscale=-1]%
\fi%
%find rightmost vertex
\global\lpsize=\linkpatternsize
\edef\mylist{#2}%to avoid some annoying bugs
%find rightmost vertex
\foreach \x/\y in \mylist
{
\ifnum\x>\lpsize\global\lpsize=\x\fi
\ifnum\y>\lpsize\global\lpsize=\y\fi
}
%
%\draw[fill=white] (0,0) circle (1);
\iflinkpatternaxis
\draw (0,0) circle (1);
\fi
\foreach\x in {1,...,\lpsize}
{
\pgfmathparse{(0.3*floor((\x-1)/\linkpatternfused)+0.7*((\x-0.5)/\linkpatternfused-0.5))*\linkpatternfused*360/\lpsize}
\coordinate[/linkpattern/vertex] (v\x) at (\pgfmathresult:1);
}
%finally, draw edges
\foreach \x/\y/\z in \mylist
{
\ifx\y\z%
\draw[/linkpattern/edge] (v\x) .. controls ($0.5*(v\x)$) and  ($0.5*(v\y)$) .. (v\y);
\else
%\draw[/linkpattern/edge,decoration={markings,mark = at position 0.5 with { \arrow[semithick]{\z} }},postaction={decorate}] (v\x) .. controls ($0.5*(v\x)$) and  ($0.5*(v\y)$) .. (v\y);
\draw[/linkpattern/edge] \z (v\x) .. controls ($0.5*(v\x)$) and  ($0.5*(v\y)$) .. (v\y);
\fi
}
\iflinkpatternnumbered%
\pgfmathparse{\lpsize/\linkpatternfused}
\global\lpsize=\pgfmathresult
\def\linkpatternnumbering{1,...,\lpsize}
\newdimen\angle
\foreach\x/\xx/\opt in \linkpatternnumbering
{
  \pgfmathsetmacro{\angle}{360/\lpsize*(\x-1)}
\ifx\xx\opt%
  \node[outer sep=1pt,anchor=180+\angle] at (\angle:1) {$\scriptstyle\xx$}; % note the subtle anchoring: if we used for example "label", the anchoring would be restricted to the cardinal points i.e. north, north east, etc. instead we're using angle as anchor (documented in sec 39)
\else
  \node[outer sep=1pt,anchor=180+\angle,\opt] at (\angle:1) {$\scriptstyle\xx$}; % note the subtle anchoring: if we used for example "label", the anchoring would be restricted to the cardinal points i.e. north, north east, etc. instead we're using angle as anchor (documented in sec 39)
\fi
}
\fi%
\iflinkpatterninverted%
\end{scope}
\fi%
\iflinkpatterntikzstarted\else%
\end{tikzpicture}%
\fi%
}}%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% loop configuration macros
\newdimen{\loopcellsize}\setlength{\loopcellsize}{0.75cm}
\tikzset{bgplaq/.style={draw=black,fill=\linkpatternboxcolor}}
%
\def\plaqwest{}
\def\plaqeast{}
\def\plaqnorth{}
\def\plaqsouth{}
%
%pieces first 
\def\plaqa{
\draw[/linkpattern/edge,\plaqwest,\plaqnorth] (0,0.5) .. controls (0,0.2) and (-0.2,0) .. (-0.5,0);
\draw[/linkpattern/edge,\plaqeast,\plaqsouth] (0,-0.5) .. controls (0,-0.2) and (0.2,0) .. (0.5,0);
}
\def\plaqb{
\draw[/linkpattern/edge,\plaqwest,\plaqsouth] (0,-0.5) .. controls (0,-0.2) and (-0.2,0) .. (-0.5,0);
\draw[/linkpattern/edge,\plaqeast,\plaqnorth] (0,0.5) .. controls (0,0.2) and (0.2,0) .. (0.5,0);
}
\def\plaqc{
\draw[/linkpattern/edge,\plaqsouth,\plaqnorth] (0,0.5) -- (0,-0.5);
\draw[/linkpattern/edge,\plaqwest,\plaqeast] (0.5,0) -- (-0.5,0);
}
\def\plaqj{
\draw[/linkpattern/edge,\plaqwest,\plaqnorth] (0,0.5) .. controls (0,0.2) and (-0.2,0) .. (-0.5,0);
}
\def\plaqr{
\draw[/linkpattern/edge,\plaqeast,\plaqsouth] (0,-0.5) .. controls (0,-0.2) and (0.2,0) .. (0.5,0);
}
\def\plaqh{
\draw[/linkpattern/edge,\plaqwest,\plaqnorth] (-0.5,0) -- (0.5,0);
}
\def\plaqv{
\draw[/linkpattern/edge,\plaqwest,\plaqnorth] (0,-0.5) -- (0,0.5);
}
%
\pgfkeys{/linkpattern/.cd,west/.store in=\plaqwest,east/.store in=\plaqeast,north/.store in=\plaqnorth,south/.store in=\plaqsouth}
\def\plaqname{plaq}%default name
\newcommand\plaq[2][]{
\node[bgplaq,rectangle,draw,minimum size=\loopcellsize,transform shape] (\plaqname) {};
\useasboundingbox;
\pgfkeys{/linkpattern/.cd,#1}%parse the list of optional arguments
\ifx#2\empty\else
\begin{scope}[x=\loopcellsize,y=\loopcellsize]
\csname plaq#2\endcsname
\end{scope}\fi
}
%
\newcommand\halfplaq[1][]{
\node[draw=none,fill=none,rectangle,minimum size=\loopcellsize,transform shape] (\plaqname) {};
\useasboundingbox;
\pgfkeys{/linkpattern/.cd,#1}%parse the list of optional arguments
\begin{scope}[x=\loopcellsize,y=\loopcellsize]
\draw[bgplaq] (-0.5,-0.5) -- ++(1,1) -- ++(-1,0) -- cycle;
\draw[/linkpattern/edge,\plaqwest,\plaqnorth] (0,0.5) .. controls (0,0.2) and (-0.2,0) .. (-0.5,0);
\end{scope}
}
%
\tikzset{loop/.code={\def\plaqname{loop-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}},loop/.append style={matrix,row sep={\loopcellsize,between origins},column sep={\loopcellsize,between origins}}}%note: define loop-row-col as name for each elementary plaquette