%% symbol library for TikZ track schematics
%
% Copyright (c) 2018 - 2022, Martin Scheidt (ISC license)
%
% Permission to use, copy, modify, and/or distribute this file for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
%
\ProvidesFileRCS{tikzlibrarytrackschematic.topology.code.tex}%
%
%%%%%%%%%%%%%%%
% Requirements
%%%%%%%%%%%%%%%
\RequirePackage{tikz,etoolbox}%
\usetikzlibrary{calc,patterns}%
%
%%%%%%%%%%%%%%%
% tikz keys for multiple use
%%%%%%%%%%%%%%%
\pgfkeys{%
  /tikz/trackschematic/.is family,%
  /tikz/trackschematic/.cd,%
  %% face
  face/.value required,% forward OR backward
  face/.store in=\face,%
  /tikz/face/.forward to=/tikz/trackschematic/face,%
  /tikz/forward/.code={\pgfkeys{/tikz/trackschematic/face=forward}},%
  /tikz/backward/.code={\pgfkeys{/tikz/trackschematic/face=backward}},%
  /tikz/bidirectional/.code={\pgfkeys{/tikz/trackschematic/face=bidirectional}},%
  %% operation
  operation mode/.store in=\operationmode,% manual, remote OR none
  operation mode=none,% DEFAULT
  /tikz/operation/.forward to=/tikz/trackschematic/operation mode,%
  %% label
  shift label/.store in=\labelcoord,% (coord)
  shift label=(none),% DEFAULT
  /tikz/shift label/.forward to=/tikz/trackschematic/shift label,%
}%
\pgfkeys{%
  /tikz/trackschematic/topology/.is family,%
  /tikz/trackschematic/topology/.cd,%
  %% branch
  branch/.value required,% left OR right
  branch/.store in=\branch,% left OR right
  /tikz/branch/.forward to=/tikz/trackschematic/topology/branch,%
  %% fouling point
  fouling point/.value forbidden,%
  fouling point/.code={\settoggle{fouling_point}{true}},%
  /tikz/fouling point/.forward to=/tikz/trackschematic/topology/fouling point,%
  %% points
  points/.store in=\points,% moving, left, right OR none
  points=none,% DEFAULT
  /tikz/points/.forward to=/tikz/trackschematic/topology/points,%
}%
% options for turnout and slip turnout
\newtoggle{fouling_point}\settoggle{fouling_point}{false}%
%
%%%%%%%%%%%%%%%
% symbol track
%%%%%%%%%%%%%%%
%
% commands
\newcommand\maintrack{}% just for safety
\def\maintrack{\path[draw=foreground,MainTrack]}% \maintrack (coord1) -- (coord2);
\newcommand\secondarytrack{}% just for safety
\def\secondarytrack{\path[draw=foreground,SecondaryTrack]}% \secondarytrack (coord1) -- (coord2);
\newcommand\sidetrack{}% just for safety
\def\sidetrack{\path[draw=foreground,SecondaryTrack]}% alias for \secondarytrack
%
%% symbol definition
\tikzset{%
  MainTrack/.style={line width=2pt},%
  % symbology entry
  symbology_main_track/.pic = {\maintrack (0,0) -- (6,0);}%
}%
\tikzset{%
  SecondaryTrack/.style={line width=0.7pt},%
  % symbology entry
  symbology_secondary_track/.pic = {\secondarytrack (0,0) -- (6,0);}%
}%
%
%%%%%%%%%%%%%%%
% symbol track number
%%%%%%%%%%%%%%%
%
%% command
\newcommand\tracklabel{}% just for safety
\def\tracklabel#1(#2)#3(#4){% \tracklabel at (coord) label (name);
  \pic at (#2) {track_label={#1/#3/#4}}% symbol
}%
%% tikz keys
% \pgfkeys{%
%   /tikz/trackschematic/.is family,%
%   /tikz/trackschematic/.cd,%
% }%
%% symbol definition
\tikzset{%
  pics/track_label/.default=,%
  pics/track_label/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1} % beware of leading and tailing spaces!
    \def\labelcommand{#2} % beware of leading and tailing spaces!
    \def\labelcontent{#3}
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={fill=background,text=foreground}};%
      \coordinate (ts-tl-l) at (0,0);%
      \ifdefstring{\labelcoord}{(none)}{% default coord
      }{% initialize if NOT default
        \path let \p1=\labelcoord in coordinate (ts-tl-l) at ($(ts-tl-l)+(\x1,\y1)$);%
      }%
      \node at (ts-tl-l) {\labelcontent};%
    }%
  }},% END pics/track_label/.style args={#1/#2/#3}
  %
  % symbology entry
  symbology_track_label/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \tracklabel at (3,0) label (label);%
  },%
}%
%
%%%%%%%%%%%%%%%
% symbol buffer stop
%%%%%%%%%%%%%%%
%
% command
\newcommand\bufferstop{}% just for safety
\def\bufferstop[#1]#2(#3){% \bufferstop[options] at (coord);
  \pic[#1] at (#3) {bufferstop={#2}}% symbol
}%
\newlength{\friction}\setlength{\friction}{0.5cm}
% tikz keys
\pgfkeys{%
  /tikz/trackschematic/topology/bufferstop/.is family,%
  /tikz/trackschematic/topology/bufferstop/.cd,%
  %% friction
  friction/.store in=\friction,% length OR none
  friction=none,% length OR none
  /tikz/friction/.forward to=/tikz/trackschematic/topology/bufferstop/friction,%
}%
% symbol definition
\tikzset{%
  pics/bufferstop/.default=,%
  pics/bufferstop/.style args={#1}{code={%
    %% settings
    \def\coordcommand{#1} % beware of leading and tailing spaces!
    %% face setup
    \ifdefstring{\face}{forward}{% face
      \pgfmathsetmacro{\facefactor}{1}%
    }{%
      \ifdefstring{\face}{backward}{% face
        \pgfmathsetmacro{\facefactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/face}{“forward“ OR “backward“ as key required}}%
      }%
    }% end \ifdefstring{\face}
    %% symbol
    \ifdefstring{\friction}{none}{% none friction
      \path[draw=foreground, line width=1pt]%
        ($\facefactor*(-0.1,0) + (0,0.2)$) --  ++($\facefactor*( 0.1,0)$) -- ++(0,-0.4) -- ++ ($\facefactor*(-0.1,0)$);% bufferstop marker
    }{% friction
      \path[draw=foreground, line width=1pt]%
        ($\facefactor*(-\friction,0) + \facefactor*(-0.05,0) + (0,0.2)$) --  ++($\facefactor*(0.1,0)$) -- ++(0,-0.4) -- ++ ($\facefactor*(-0.1,0)$);% bufferstop marker
      \fill[foreground] (0,0) circle (0.06);% track closure indicator
    }%
  }},% END pics/bufferstop/.style args={#1}
  %
  % symbology entry
  symbology_bufferstop/.pic = {%
    \secondarytrack (0,0) -- (3,0);%
    \bufferstop[forward] at (3,0);%
  },%
  % symbology entry
  symbology_friction_bufferstop/.pic = {%
    \maintrack (0,0) -- (3,0);%
    \bufferstop[forward,friction=.5] at (3,0);%
  },%
}%
%
%%%%%%%%%%%%%%%
% symbol track closure
%%%%%%%%%%%%%%%
%
%% command
\newcommand\trackclosure{}% just for safety
\def\trackclosure#1(#2){% \trackclosure at (coord);
  \pic at (#2) {track_closure={#1}}% symbol
}%
% symbol definition
\tikzset{%
  pics/track_closure/.default=,%
  pics/track_closure/.style args={#1}{code={%
    %% settings
    \def\coordcommand{#1} % beware of leading and tailing spaces!
    %%
    \fill[foreground] (0,0) circle (0.06);% track closure indicator
    % \path[fill=foreground] (0.04,0.02) -- (0.04,-0.02) -- (0.02,-0.04) -- (-0.02,-0.04) -- (-0.04,-0.02) -- (-0.04,0.02) -- (-0.02,0.04) -- (0.02,0.04) -- cycle;% label
  }},% END pics/track_closure/.style args={#1}
  %
  % symbology entry
  symbology_track_closure/.pic = {%
    \maintrack (0,0) -- (3,0);%
    \trackclosure at (3,0);%
  },%
}%
%
%%%%%%%%%%%%%%%
% symbol turnout
%%%%%%%%%%%%%%%
%
% command
\newcommand\turnout{}% just for safety
\def\turnout[#1]#2(#3)#4(#5){% \turnout[options] at (coord) label (name);
  \pic[operation=remote,#1] at (#3) {turnout={#2/#4/#5}}% symbol
}%
% tikz keys
\pgfkeys{%
  /tikz/trackschematic/topology/turnout/.is family,%
  /tikz/trackschematic/topology/turnout/.cd,%
  %% points
  points/.store in=\points,% moving, left, right OR none
  points=none,% moving, left, right OR none
  /tikz/points/.forward to=/tikz/trackschematic/topology/turnout/points,%
}%
% symbol definition
\tikzset{%
  pics/turnout/.default=,%
  pics/turnout/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontent{#3}%
    %% face setup
    \ifdefstring{\face}{forward}{% face
      \pgfmathsetmacro{\facefactor}{1}%
    }{%
      \ifdefstring{\face}{backward}{% face
        \pgfmathsetmacro{\facefactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/face}{“forward“ OR “backward“ as key required}}%
      }%
    }% end \ifdefstring{\face}
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \pgfmathsetmacro{\branchfactor}{1}%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \pgfmathsetmacro{\branchfactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }% end \ifdefstring{\branch}
    %% turnout marker
    \ifdefstring{\operationmode}{manual}{% operation=manual
      \ifdefstring{\face}{forward}{%
        \ifdefstring{\branch}{left}{% branch
          \def\patterntype{north west lines}%
        }{%
          \def\patterntype{north east lines}%
        }%
      }{%
        \ifdefstring{\branch}{right}{% branch
          \def\patterntype{north west lines}%
        }{%
          \def\patterntype{north east lines}%
        }%
      }%
      \path[draw=foreground,pattern=\patterntype, pattern color=foreground] (0,0) -- ++($\facefactor*(0.4,0)$) -- ++($\branchfactor*(0,0.4)$) -- cycle;% turnout marker
    }{%
      \ifdefstring{\operationmode}{remote}{% operation=remote
        \path[fill=foreground] (0,0) -- ++($\facefactor*(0.4,0)$) -- ++($\branchfactor*(0,0.4)$) -- cycle;% turnout marker
      }{%
        \ifdefstring{\operationmode}{none}{
          \path[draw=foreground] (0,0) -- ++($\facefactor*(0.4,0)$) -- ++($\branchfactor*(0,0.4)$) -- cycle;% turnout marker
        }{%
          \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/operation}{“manual“, “remote“ OR “none“ as key required}}%
        }%
      }
    }%
    %
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={text=foreground}};%
      \coordinate (ts-y-l) at ($\branchfactor*(0,-8pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \path let \p1=\labelcoord in coordinate (ts-y-l) at ($(ts-y-l)+(\x1,\y1)$);%
      }%
      \node at (ts-y-l) {\footnotesize \labelcontent};%
    }%
    %
    %% fouling point indicator
    \iftoggle{fouling_point}{%
      \path[draw=foreground] ($\facefactor*(0.7,0)$) -- ++($\branchfactor*(0,0.7)$);% fouling point indicator
    }{}%
    %
    %% points
    \ifdefstring{\points}{left}{% points left
      \ifdefstring{\branch}{left}{%
        \path[draw=foreground,line width=1.5pt] ($\facefactor*(-0.035,0)+(0,0.1)$) -- ++($\facefactor*(0.2,0)+(0,0.2)$);%
      }{%
        \path[draw=foreground,line width=1.5pt] ($\facefactor*( 0.035,0)+(0,0.1)$) -- ++($\facefactor*( 0.265,0  )$);%
      }%
    }{%
      \ifdefstring{\points}{right}{% points right
        \ifdefstring{\branch}{left}{%
          \path[draw=foreground,line width=1.5pt] ($\facefactor*( 0.035,0)+(0,-0.1)$) -- ++($\facefactor*( 0.265,0  )$);%
        }{%
          \path[draw=foreground,line width=1.5pt] ($\facefactor*(-0.035,0)+(0,-0.1)$) -- ++($\facefactor*(0.2,0)+(0,-0.2)$);%
        }%
      }{%
        \ifdefstring{\points}{moving}{% moving points
          \fill[foreground] ($\facefactor*(0.075,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[foreground] ($\facefactor*(0.225,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[foreground] ($\facefactor*(0.015,0) + \branchfactor*(0, 0.15)$) circle (0.05);% points indicator right
          \fill[foreground] ($\facefactor*(0.115,0) + \branchfactor*(0, 0.25)$) circle (0.05);% points indicator right
        }{% error message
          \ifdefstring{\points}{none}{%
          }{%
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/turnout/points}{“left“, “right“ OR “moving“ as key required}}%
          }%
        }%
      }%
    }% end \ifdefstring{\points}
    %
  }},% END pics/turnout/.style args={#1/#2/#3}
  % symbology entry
  symbology_turnout/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \maintrack (3,0) -- ++(0.8,0.8);%
    \turnout[forward,branch=left] at (3,0) label ();%
  },%
  symbology_turnout_fouling/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \maintrack (3,0) -- ++(0.8,0.8);%
    \turnout[forward,branch=left,fouling point] at (3,0) label ();%
  },%
  symbology_turnout_manually/.pic = {%
    \secondarytrack (0,0) -- (6,0);%
    \secondarytrack (3,0) -- ++(0.8,0.8);%
    \turnout[forward,branch=left,operation=manual] at (3,0) label ();%
  },%
  symbology_turnout_points_right/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \maintrack (3,0) -- ++(0.8,0.8);%
    \turnout[forward,branch=left,points=right] at (3,0) label ();%
  },%
  symbology_turnout_points_left/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \maintrack (3,0) -- ++(0.8,0.8);%
    \turnout[forward,branch=left,points=left] at (3,0) label ();%
  },%
  symbology_turnout_points_moving/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \maintrack (3,0) -- ++(0.8,0.8);%
    \turnout[forward,branch=left,points=moving] at (3,0) label ();%
  },%
}%
%
%%%%%%%%%%%%%%%
% symbol slip switch turnout - using parts of turnout
%%%%%%%%%%%%%%%
%
\newcommand\slipturnout{}% just for safety
\def\slipturnout[#1]#2(#3)#4(#5)(#6){% \slipturnout[options] at (coord) label (name1)(name2);
  \pic[operation=remote,slip=double,#1] at (#3) {slipturnout={#2/#4/#5/#6}}% symbol
}%
% tikz keys
\pgfkeys{%
  /tikz/trackschematic/topology/slipturnout/.is family,%
  /tikz/trackschematic/topology/slipturnout/.cd,%
  %% points
  forward points/.store in=\forwardpoints,% moving, left, right OR none
  forward points=none,% moving, left, right OR none
  /tikz/forward points/.forward to=/tikz/trackschematic/topology/slipturnout/forward points,%
  backward points/.store in=\backwardpoints,% moving, left, right OR none
  backward points=none,% moving, left, right OR none
  /tikz/backward points/.forward to=/tikz/trackschematic/topology/slipturnout/backward points,%
  %% slips
  slip/.store in=\slip,% double, left, right OR none
  slip=none,% double, left, right OR none
  /tikz/slip/.forward to=/tikz/trackschematic/topology/slipturnout/slip,%
}%
% symbol definition
\tikzset{%
  pics/slipturnout/.default=,%
  pics/slipturnout/.style args={#1/#2/#3/#4}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontentleft{#3}%
    \def\labelcontentright{#4}%
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \pgfmathsetmacro{\branchfactor}{1}%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \pgfmathsetmacro{\branchfactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }% end \ifdefstring{\branch}
    %% turnout marker
    \ifdefstring{\operationmode}{manual}{% operation=manual
      \ifdefstring{\branch}{left}{% branch
        \def\patterntype{north west lines}%
      }{%
        \def\patterntype{north east lines}%
      }%
      \path[draw,pattern=\patterntype, pattern color=foreground] (0,0) -- ++($( 0.4,0)$) -- ++($\branchfactor*(0, 0.4)$) -- cycle;% turnout marker
      \path[draw,pattern=\patterntype, pattern color=foreground] (0,0) -- ++($(-0.4,0)$) -- ++($\branchfactor*(0,-0.4)$) -- cycle;% turnout marker
    }{% operated automaticly
      \ifdefstring{\operationmode}{remote}{% operation=remote
        \path[fill=foreground] (0,0) -- ++($( 0.4,0)$) -- ++($\branchfactor*(0, 0.4)$) -- cycle;% turnout marker
        \path[fill=foreground] (0,0) -- ++($(-0.4,0)$) -- ++($\branchfactor*(0,-0.4)$) -- cycle;% turnout marker
      }{%
        \ifdefstring{\operationmode}{none}{}{%
          \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/operation}{“manual“, “remote“ OR “none“ as key required}}%
        }%
      }%
    }%
    %% fouling point indicator
    \iftoggle{fouling_point}{%
      \path[draw=foreground] ( 0.7,0) -- ++($\branchfactor*(0, 0.7)$);% fouling point indicator
      \path[draw=foreground] (-0.7,0) -- ++($\branchfactor*(0,-0.7)$);% fouling point indicator
    }{}%
    %
    %% points
    \ifdefstring{\forwardpoints}{left}{% points left
      \ifdefstring{\branch}{left}{%
        \path[draw=foreground,line width=1.5pt] (-0.035,0.1) -- ++(0.2,0.2);%
      }{%
        \path[draw=foreground,line width=1.5pt] ( 0.035,0.1) -- ++(0.265,0);%
      }%
    }{%
      \ifdefstring{\forwardpoints}{right}{% points right
        \ifdefstring{\branch}{left}{%
          \path[draw=foreground,line width=1.5pt] ( 0.035,-0.1) -- ++(0.265,0);
        }{%
          \path[draw=foreground,line width=1.5pt] (-0.035,-0.1) -- ++(0.2,-0.2);%
        }%
      }{%
        \ifdefstring{\forwardpoints}{moving}{% moving points
          \fill[foreground] ($(0.075,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[foreground] ($(0.225,0) + \branchfactor*(0,-0.1 )$) circle (0.05);% points indicator left
          \fill[foreground] ($(0.015,0) + \branchfactor*(0, 0.15)$) circle (0.05);% points indicator right
          \fill[foreground] ($(0.115,0) + \branchfactor*(0, 0.25)$) circle (0.05);% points indicator right
        }{% error message
          \ifdefstring{\forwardpoints}{none}{%
          }{%
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/slipturnout/forward points}{“left“, “right“ OR “moving“ as key required}}%
          }%
        }%
      }%
    }% end of  \ifdefstring{\forwardpoints}
    \ifdefstring{\backwardpoints}{left}{% points left
      \ifdefstring{\branch}{left}{%
        \path[draw=foreground,line width=1.5pt] (0.035,-0.1) -- ++(-0.2,-0.2);%
      }{%
        \path[draw=foreground,line width=1.5pt] (-0.035,-0.1) -- ++(-0.265,0);%
      }%
    }{%
      \ifdefstring{\backwardpoints}{right}{% points right
        \ifdefstring{\branch}{left}{%
          \path[draw=foreground,line width=1.5pt] (-0.035,0.1) -- ++(-0.265,0);
        }{%
          \path[draw=foreground,line width=1.5pt] (0.035,0.1) -- ++(-0.2,0.2);%
        }%
      }{%
        \ifdefstring{\backwardpoints}{moving}{% moving points
          \fill[foreground] ($(-0.075,0) + \branchfactor*(0, 0.1 )$) circle (0.05);% points indicator left
          \fill[foreground] ($(-0.225,0) + \branchfactor*(0, 0.1 )$) circle (0.05);% points indicator left
          \fill[foreground] ($(-0.015,0) + \branchfactor*(0,-0.15)$) circle (0.05);% points indicator right
          \fill[foreground] ($(-0.115,0) + \branchfactor*(0,-0.25)$) circle (0.05);% points indicator right
        }{% error message
          \ifdefstring{\backwardpoints}{none}{%
          }{%
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/slipturnout/forward points}{“left“, “right“ OR “moving“ as key required}}%
          }%
        }%
      }%
    }% end of  \ifdefstring{\backwardpoints}
    %
    %% slips
    \ifdefstring{\slip}{double}{% slip
      \path[draw=foreground,line width=0.75pt] ($\branchfactor*(-0.4,0) + (0, 0.1)$) --  ($\branchfactor*( 0.3,0) + (0, 0.4)$);% slip
      \path[draw=foreground,line width=0.75pt] ($\branchfactor*( 0.4,0) + (0,-0.1)$) --  ($\branchfactor*(-0.3,0) + (0,-0.4)$);% slip
    }{%
      \ifdefstring{\slip}{left}{%
        \path[draw=foreground,line width=0.75pt] ($\branchfactor*(-0.4,0) + (0,0.1)$) --  ($\branchfactor*(0.3,0) + (0,0.4)$);% slip
      }{%
        \ifdefstring{\slip}{right}{%
          \path[draw=foreground,line width=0.75pt] ($\branchfactor*(0.4,0) + (0,-0.1)$) --  ($\branchfactor*(-0.3,0) + (0,-0.4)$);% slip
        }{%
          \ifdefstring{\slip}{none}{%
            %
          }{%error message
            \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/slipturnout/slip}{“double“, “left“, “right“ OR “none“ as key required}}%
          }%
        }%
      }%
    }%
    %
    %% label
    \tikzset{every node/.style={text=foreground}};%
    \ifdefstring{\labelcontentleft}{}{}{% label NOT empty
      \coordinate (ts-sy-l1) at ($(2pt,0) + \branchfactor*(0, 10pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \path let \p1=\labelcoord in coordinate (ts-sy-l1) at ($(ts-sy-l1)+(\x1,\y1)$);%
      }%
      \node[left]  at (ts-sy-l1) {\footnotesize \labelcontentleft};%
    }%
    \ifdefstring{\labelcontentright}{}{}{% label NOT empty
      \coordinate (ts-sy-l2) at ($(2pt,0) + \branchfactor*(0,-10pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \path let \p1=\labelcoord in coordinate (ts-sy-l2) at ($(ts-sy-l2)+(\x1,\y1)$);%
      }%
      \node[right] at (ts-sy-l2) {\footnotesize \labelcontentright};%
    }%
    %
  }},% END of  pics/slipturnout/.style args={#1/#2/#3/#4}
  % symbology entry
  symbology_slip_turnout/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \maintrack (3,0) -- ++(0.8,0.8);%
    \maintrack (3,0) -- ++(-0.8,-0.8);%
    \slipturnout[branch=left] at (3,0) label ()();%
  },%
}%
%
%%%%%%%%%%%%%%%
% symbol crossing - using parts of turnout
%%%%%%%%%%%%%%%
% command
\newcommand\crossing{}% just for safety
\def\crossing[#1]#2(#3)#4(#5){% \crossing[options] at (coord) label (name);
  \pic[#1] at (#3) {crossing={#2/#4/#5}}% symbol
}%
% symbol definition
\tikzset{%
  pics/crossing/.default=,%
  pics/crossing/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontent{#3}%
    %% face setup
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \pgfmathsetmacro{\branchfactor}{1}%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \pgfmathsetmacro{\branchfactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }%
    %
    %% crossing marker
    \path[draw=foreground] (0,0) -- ++( 0.4,0) -- ++($\branchfactor*(0, 0.4)$) -- cycle;% turnout marker
    \path[draw=foreground] (0,0) -- ++(-0.4,0) -- ++($\branchfactor*(0,-0.4)$) -- cycle;% turnout marker
    %
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={text=foreground}};%
      \coordinate (ts-x-l) at ($(2pt,0) + \branchfactor*(0,-9pt)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \path let \p1=\labelcoord in coordinate (ts-x-l) at ($(ts-x-l)+(\x1,\y1)$);%
      }%
      \node[right] at (ts-x-l) {\footnotesize \labelcontent};%
    }%
    %
    %% fouling point indicator
    \iftoggle{fouling_point}{%
      \path[draw=foreground] ( 0.7,0) -- ++($\branchfactor*(0, 0.7)$);% fouling point indicator
      \path[draw=foreground] (-0.7,0) -- ++($\branchfactor*(0,-0.7)$);% fouling point indicator
    }{}%
    %
  }},% end of  pics/crossing/.style args={#1/#2/#3}
  % symbology entry
  symbology_diamond_crossing/.pic = {%
    \maintrack (0,0) -- (6,0);%
    \maintrack (3,0) -- ++(0.8,0.8);%
    \maintrack (3,0) -- ++(-0.8,-0.8);%
    \crossing[branch=left] at (3,0) label ();%
  },%
}%
%
%%%%%%%%%%%%%%%
% symbol derailer
%%%%%%%%%%%%%%%
%
% command
\newcommand\derailer{}% just for safety
\def\derailer[#1]#2(#3)#4(#5){% \derailer[options] at (coord) label (name);
  \pic[#1] at (#3) {derailer={#2/#4/#5}}% symbol
}%
% symbol definition
\tikzset{%
  pics/derailer/.default=,%
  pics/derailer/.style args={#1/#2/#3}{code={%
    %% settings
    \def\coordcommand{#1}% beware of leading and tailing spaces!
    \def\labelcommand{#2}% beware of leading and tailing spaces!
    \def\labelcontent{#3}%
    %% face setup
    \ifdefstring{\face}{forward}{% face
      \pgfmathsetmacro{\facefactor}{1}%
    }{%
      \ifdefstring{\face}{backward}{% face
        \pgfmathsetmacro{\facefactor}{-1}%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/face}{“forward“ OR “backward“ as key required}}%
      }%
    }% end \ifdefstring{\face}
    %% branch setup
    \ifdefstring{\branch}{left}{% branch
      \ifdefstring{\face}{forward}{%
        \pgfmathsetmacro{\branchfactor}{1}%
      }{%
        \pgfmathsetmacro{\branchfactor}{-1}%
      }%
    }{%
      \ifdefstring{\branch}{right}{% branch
        \ifdefstring{\face}{forward}{%
          \pgfmathsetmacro{\branchfactor}{-1}%
        }{%
          \pgfmathsetmacro{\branchfactor}{1}%
        }%
      }{% error message
        \pgfkeys{/errors/unknown choice value={/tikz/trackschematic/topology/branch}{“left“ OR “right“ as key required}}%
      }%
    }% end \ifdefstring{\branch}
    %
    %% symbol
    \path[draw=foreground, line width=1pt] (0,0.1) -- ++(0,-0.2);% derailer marker
    \path[draw=foreground,->,>=latex,line width=1pt,dashed] (0,0) -- ++($\facefactor*(0.4,0) + \branchfactor*(0,0.4)$);% derailer arrow
    %
    %% label
    \ifdefstring{\labelcontent}{}{}{% label NOT empty
      \tikzset{every node/.style={text=foreground}};%
      \coordinate (ts-dr-l) at ($\branchfactor*(0,-0.3)$);%
      \ifdefstring{\labelcoord}{(none)}{}{% initialize if NOT default
        \path let \p1=\labelcoord in coordinate (ts-dr-l) at ($(ts-dr-l)+(\x1,\y1)$);%
      }%
      \node at (ts-dr-l) {\footnotesize \labelcontent};%
    }%
    %
  }},% END pics/derailer/.style args={#1/#2/#3}
  % symbology entry
  symbology_derailer/.pic = {%
    \secondarytrack (0,0) -- (6,0);%
    \derailer[forward,branch=left] at (3,0) label ();%
  },%
}%
%
%%%%%%%%%%%%%%%
\endinput%
%
