\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{causets}[2025/02/24 v1.5 Package to draw causal set diagrams]
%% Copyright 2020-2025 by C. Minz
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this licence or (at your option) any later version.
% The latest version of this licence is in
% http://www.latex-project.org/lppl.txt
%
% This work has the LPPL maintenance status "maintained".
% 
% The current version of this package is 1.5.
%
% The current maintainer of this work is C. Minz.
% https://github.com/c-minz
%
% This work consists of the files causets.sty, causets.tex, causets.pdf,
% causets_example1.tex, causets_example1.pdf, 
% causets_example2.tex, causets_example2.pdf

\RequirePackage{tikz}
\usepgflibrary{arrows.meta}

%% PGF keys and style macros:
\def\tikzcausetsset#1{\pgfqkeys{/tikz/causets}{#1}}
\pgfkeys{/tikz/causets/.search also={/tikz}}
% Size options:
\tikzcausetsset{%
  size unit/.store in=\causetTileSize,
  size unit/.value required,
  tile size/.store in=\causetTileSize,
  tile size/.value required,
  text font/.store in=\causetTextFont,
  text font/.value required,
  text scale/.store in=\causetTextScale,
  text scale/.value required,
  region line width/.initial=0.07*\causetTileSize,
  grid line width/.initial=0.04*\causetTileSize,
  event size/.initial=0.56*\causetTileSize,
  event outline/.initial=0.04*\causetTileSize,
  link width/.initial=0.14*\causetTileSize,
  broken link gap/.initial=0.42*\causetTileSize,
  tiny/.value forbidden,
  tiny/.style={%
    size unit=0.30ex,
    text font=\tiny,
    text scale=0.30},
  very small/.value forbidden,
  very small/.style={%
    size unit=0.40ex,
    text font=\tiny,
    text scale=0.40},
  small/.value forbidden,
  small/.style={%
    size unit=0.60ex,
    text font=\scriptsize,
    text scale=0.43},
  normal/.value forbidden,
  normal/.style={%
    size unit=0.90ex,
    text font=\footnotesize,
    text scale=0.56},
  large/.value forbidden,
  large/.style={%
    size unit=1.35ex,
    text font=\small,
    text scale=0.75},
  very large/.value forbidden,
  very large/.style={%
    size unit=2.025ex,
    text font=\normalsize,
    text scale=1.02},
  huge/.value forbidden,
  huge/.style={%
    size unit=2.70ex,
    text font=\Large,
    text scale=0.97}
}
% Colour options:
\tikzcausetsset{%
  region color/.initial=black!75,
  grid color/.initial=black!25,
  tile color/.initial=black!10!white,
  event color/.initial=black,
  label color/.initial=black,
  link color/.initial=black!65!white,
  spatial link color/.initial=black!50!white,
  ulabel color/.initial=black!65!white,
  vlabel color/.initial=black!65!white,
  black colors/.style={%
    region color=black,
    grid color=black,
    tile color=white,
    event color=black,
    label color=black,
    link color=black,
    spatial link color=black,
    ulabel color=black,
    vlabel color=black},
  gray colors/.style={%
    region color=black!75,
    grid color=black!25,
    tile color=black!10!white,
    event color=black,
    label color=black,
    link color=black!65!white,
    spatial link color=black!50!white,
    ulabel color=black!65!white,
    vlabel color=black!65!white},
  blue colors/.style={%
    region color=black!75,
    grid color=black!25,
    tile color=cyan!25!black!25!white,
    event color=blue!50!black,
    label color=blue!50!black,
    link color=cyan!75!black,
    spatial link color=blue!75!white,
    ulabel color=black!50,
    vlabel color=black!50},
  neon colors/.style={%
    region color=green,
    grid color=green,
    tile color=yellow,
    event color=blue,
    label color=blue,
    link color=cyan,
    spatial link color=red,
    ulabel color=magenta,
    vlabel color=magenta}
}
% Causet modification options:
\tikzcausetsset{%
  padding/.store in=\causets@Padding,
  offset/.store in=\causet@Offset,
  alt/.estore in=\causetAltText,
  every causet/.value forbidden,
  every causet/.style={baseline=(baseline point), alt={causet}},
  local style/.style={#1},
  set local style/.style={local style/.expand once={#1}}
}
% Graphic component switches:
\newif\ifcausetsDrawPermutation
\newif\ifcausetsDrawLinks
\newif\ifcausetsBreakLinks
\newif\ifcausetsDrawSpatialLinks
\newif\ifcausetsDrawLabels
\newif\ifcausetsDrawULabels
\newif\ifcausetsDrawVLabels
\tikzcausetsset{%
  show permutation/.is if=causetsDrawPermutation,
  show links/.is if=causetsDrawLinks,
  break links/.is if=causetsBreakLinks,
  show spatial links/.is if=causetsDrawSpatialLinks,
  show labels/.is if=causetsDrawLabels,
  show ulabels/.is if=causetsDrawULabels,
  show vlabels/.is if=causetsDrawVLabels,
  permutation/.code={\causetsDrawPermutationtrue},
  linked/.code={\causetsDrawLinkstrue},
  unlinked/.code={\causetsDrawLinksfalse},
  brokenly linked/.code={\causetsBreakLinkstrue},
  continuously linked/.code={\causetsBreakLinksfalse},
  spatially linked/.code={\causetsDrawSpatialLinkstrue},
  spatially unlinked/.code={\causetsDrawSpatialLinksfalse},
  labeled/.code={\causetsDrawLabelstrue},
  unlabeled/.code={\causetsDrawLabelsfalse},
  labeled u/.code={\causetsDrawULabelstrue},
  unlabeled u/.code={\causetsDrawULabelsfalse},
  labeled v/.code={\causetsDrawVLabelstrue},
  unlabeled v/.code={\causetsDrawVLabelsfalse}
}
% Permutation grid styles:
\tikzcausetsset{%
  every region/.value forbidden,
  every region/.style={color=\pgfkeysvalueof{/tikz/causets/region color}, line width=\pgfkeysvalueof{/tikz/causets/region line width}},
  region/.style={show permutation, every region/.append style={#1}},
  every grid/.value forbidden,
  every grid/.style={color=\pgfkeysvalueof{/tikz/causets/grid color}, line width=\pgfkeysvalueof{/tikz/causets/grid line width}, step=\causetTileSize},
  grid/.style={show permutation, every grid/.append style={#1}},
  every tile/.value forbidden,
  every tile/.style={color=\pgfkeysvalueof{/tikz/causets/tile color}, line width=\pgfkeysvalueof{/tikz/causets/grid line width}, fill},
  tiles/.style={show permutation, every tile/.append style={#1}}
}
% Label styles:
\tikzcausetsset{%
  every label base/.value forbidden,
  every label base/.style={text opacity=1, scale=\causetTextScale, font=\causetTextFont, inner sep=0pt},
  all labels/.style={every label base/.append style={#1}},
  every label/.value forbidden,
  every label/.style={every label base, color=\pgfkeysvalueof{/tikz/causets/label color}, below left, at=south west},
  labels/.style={show labels, every label/.append style={#1}},
  every ulabel/.value forbidden,
  every ulabel/.style={every label base, color=\pgfkeysvalueof{/tikz/causets/ulabel color}, right, rotate=-45, inner sep=1pt, align=left},
  ulabels/.style={show ulabels, every ulabel/.append style={#1}},
  every vlabel/.value forbidden,
  every vlabel/.style={every label base, color=\pgfkeysvalueof{/tikz/causets/vlabel color}, left, rotate=45, inner sep=1pt, align=right}, 
  vlabels/.style={show vlabels, every vlabel/.append style={#1}},
  every replaced label/.value forbidden,
  every replaced label/.style={every label},
  replaced labels/.style={every replaced label/.append style={#1}},
  at/.initial=center,
  label/.store in=\causet@EventLabel,
  label/.value required
}
% Event styles:
\tikzcausetsset{%
  every event/.value forbidden,
  every event/.style={color=\pgfkeysvalueof{/tikz/causets/event color}, circle, fill, inner sep=0pt, minimum size=\pgfkeysvalueof{/tikz/causets/event size}, line width=\pgfkeysvalueof{/tikz/causets/event outline}},
  events/.value required,
  events/.style={every event/.append style={#1}},
  every replaced event/.value forbidden,
  every replaced event/.style={every label base, circle, minimum size=\pgfkeysvalueof{/tikz/causets/event size}},
  replaced events/.value required,
  replaced events/.style={every replaced event/.append style={#1}},
  event/.store in=\causet@EventText,
  event/.value required
}
% Link styles:
\tikzcausetsset{%
  Link Start/.tip={},
  Link Pause/.tip={Triangle Cap[] . Fast Triangle[] . Fast Triangle[]},
  Link Resume/.tip={Triangle Cap[reversed] . Fast Triangle[reversed] . Fast Triangle[reversed]},
  Link End/.tip={},
  every link/.value forbidden,
  every link/.style={color=\pgfkeysvalueof{/tikz/causets/link color}, line width=\pgfkeysvalueof{/tikz/causets/link width}, {Link Start[]}-{Link End[]}},
  links/.style={show links, every link/.append style={#1}},
  link starts/.value required,
  link starts/.style={Link Start/.tip={#1}},
  link pauses/.value required,
  link pauses/.style={Link Pause/.tip={#1}},
  link resumes/.value required,
  link resumes/.style={Link Resume/.tip={#1}},
  link ends/.value required,
  link ends/.style={Link End/.tip={#1}},
  cap links/.value forbidden,
  cap links/.style={link starts={Triangle Cap[reversed]}, link ends={Triangle Cap[]}},
  arrow links/.value forbidden,
  arrow links/.style={link ends={Stealth[round, length=4*\pgfkeysvalueof{/tikz/causets/link width}, line width=\pgfkeysvalueof{/tikz/causets/link width}/3]}},
  every spatial link/.value forbidden,
  every spatial link/.style={every link, color=\pgfkeysvalueof{/tikz/causets/spatial link color}, dashed},
  spatial links/.style={show spatial links, every spatial link/.append style={#1}}
}
% Causet externalisation:
\newif\ifcausets@External
\newif\ifcausetsNameExternal
\tikzcausetsset{%
  name/.store in=\causet@Name,
  name prefix/.store in=\causets@NamePrefix,
  set prefix/.style={name prefix=#1, /tikz/external/prefix=#1},
  name external/.is if=causetsNameExternal
}

%% Package options:
\tikzcausetsset{normal, offset=0, padding=\thinspace, name prefix={./}}
\causetsDrawLinkstrue
\causetsBreakLinkstrue
\causetsDrawULabelstrue
\causetsDrawVLabelstrue
\causets@Externalfalse
\DeclareOption{tiny}{\tikzcausetsset{tiny}}
\DeclareOption{smaller}{\tikzcausetsset{very small}}
\DeclareOption{small}{\tikzcausetsset{small}}
\DeclareOption{large}{\tikzcausetsset{large}}
\DeclareOption{larger}{\tikzcausetsset{very large}}
\DeclareOption{huge}{\tikzcausetsset{huge}}
\DeclareOption{gray}{\tikzcausetsset{gray colors}}
\DeclareOption{black}{\tikzcausetsset{black colors}}
\DeclareOption{blue}{\tikzcausetsset{blue colors}}
\DeclareOption{neon}{\tikzcausetsset{neon colors}}
\DeclareOption{linked}{\causetsDrawLinkstrue}
\DeclareOption{unlinked}{\causetsDrawLinksfalse}
\DeclareOption{caps}{\tikzcausetsset{cap links}}
\DeclareOption{arrows}{\tikzcausetsset{arrow links}}
\DeclareOption{continuous}{\causetsBreakLinksfalse}
\DeclareOption{spatial}{\causetsDrawSpatialLinkstrue}
\DeclareOption{permutation}{\causetsDrawPermutationtrue}
\DeclareOption{labeled}{\causetsDrawLabelstrue}
\DeclareOption{unlabeled}{\causetsDrawLabelsfalse}
\DeclareOption{ulabeled}{\causetsDrawULabelstrue}
\DeclareOption{uunlabeled}{\causetsDrawULabelsfalse}
\DeclareOption{vlabeled}{\causetsDrawVLabelstrue}
\DeclareOption{vunlabeled}{\causetsDrawVLabelsfalse}
\DeclareOption{external}{\causets@Externaltrue}
\DeclareOption*{\PackageWarning{causets}{The package causets does not support the option: \CurrentOption}}
\ProcessOptions*
\ifcausets@External
  \usetikzlibrary{external}%
  \tikzcausetsset{name prefix=causets/, name external=true}%
  \tikzexternalize[prefix=causets/]
\fi

%% Package functions:
\newif\ifcauset@ItemFoundInList

\newcommand*{\causets@setCausetName}[1]{%
{%
\def\causet@Name{}%
\tikzcausetsset{#1}%
\ifx\causet@Name\empty%
  \relax%
\else%
  \expandafter\tikzsetnextfilename\expandafter{\causet@Name}%
\fi%
}%
}

\newcommand*{\causets@prepareCauset}[1]{%
% Count the number of events (\causet@N), find the minimal value in the list 
% argument (\causet@Start), and define baseline point.
\foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#1}{%
  \ifnum1=\causet@i
    \pgfmathparse{int(\causet@E@i)}
  \else
    \pgfmathparse{int(min(\causet@Start, \causet@E@i))}
  \fi
  \xdef\causet@Start{\pgfmathresult}
  \xdef\causet@N{\causet@i}
}
\pgfmathsetmacro\causet@Start{int(\causet@Start + \causet@Offset)}
\coordinate (baseline point) at (0, -0.707*\causetTileSize);
}

\newcommand*{\causets@drawEvent}[2][every event]{%
% Draws an event tile and the event on top.
\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
\pgfmathsetmacro\causet@u{\causet@E@i - \causet@Start}
\pgfmathsetmacro\causet@v{\causet@i - 1.0}
\ifcausetsDrawPermutation
  \path[causets/.cd, every tile] 
    ( \causet@u * \causetTileSize, \causet@v * \causetTileSize ) 
    rectangle +( \causetTileSize, \causetTileSize );
\fi
\pgfmathsetmacro\causet@u{\causet@E@i - \causet@Start + 0.5}
\pgfmathsetmacro\causet@v{\causet@i - 0.5}
\node[causets/.cd, #1] (E\causet@E@i) 
  at ( \causet@u * \causetTileSize, \causet@v * \causetTileSize ) {#2};
}

\newcommand*{\causets@drawEvents}[1]{%
% Draws a permutation tile and defines and draws an event node (E#) on top of 
% the tile, for each event number # in the list argument.
\foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#1}{%
  \ifx\causet@E@i\causet@LocalStyle
    \causets@drawEvent{}
  \else
    \def\causet@EventText{}
    \tikzcausetsset{set local style=\causet@LocalStyle}
    \ifx\causet@EventText\empty
      \causets@drawEvent[every event, set local style=\causet@LocalStyle]{}
    \else
      \causets@drawEvent[every replaced event, set local style=\causet@LocalStyle]{\causet@EventText}
    \fi
  \fi
}
}

\newcommand*{\causets@drawPermutation}[1]{%
% Draws the permutation grid and adds the u- and v-coordinate labels.
\ifcausetsDrawPermutation
  \draw[causets/.cd, every grid] ( 0, 0 ) 
    grid ( \causet@N * \causetTileSize, \causet@N * \causetTileSize );
  \draw[causets/.cd, every region] ( 0, 0 ) 
    rectangle ( \causet@N * \causetTileSize, \causet@N * \causetTileSize );
  \foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#1}{%
    \pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
    \ifcausetsDrawULabels
      \pgfmathsetmacro\causet@u{int(\causet@i + \causet@Start - 1)}
      \node[causets/.cd, every ulabel] (EUL\causet@E@i) 
        at ( \causet@i * \causetTileSize - 0.5 * \causetTileSize, 0 ) 
        {\causet@u};
    \fi
    \ifcausetsDrawVLabels
      \node[causets/.cd, every vlabel] (EVL\causet@E@i) 
        at ( 0, \causet@i * \causetTileSize - 0.5 * \causetTileSize ) 
        {\causet@E@i};
    \fi
  }
\fi
}

\newcommand*{\causets@drawSpatialLinks}[1]{%
% Draws the spatial links between the events (only for \drawpcauset and 
% \drawrcauset).
\ifcausetsDrawSpatialLinks
  \foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#1}{%
    \pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
    % \causet@Bound stores the lower bound to find spatially linked events.
    \edef\causet@Bound{\causet@Offset}
    \foreach \causet@E@j/\causet@LocalStyle [count=\causet@j] in {#1}{%
      \pgfmathgreater{\causet@j}{\causet@i}
      \ifnum1=\pgfmathresult
        \pgfmathsetmacro\causet@E@j{int(\causet@E@j + \causet@Offset)}
        \pgfmathparse{and(\causet@E@j > \causet@Bound, \causet@E@i > \causet@E@j)}
        \ifnum1=\pgfmathresult
          % Add spatial link and remember new lower bound:
          \draw[causets/.cd, every spatial link]
            (E\causet@E@j) -- (E\causet@E@i);
          \xdef\causet@Bound{\causet@E@j}
        \fi
      \fi
    }
  }
\fi
}

\newcommand*{\causets@drawEventLabel}[2][every label]{%
% Draws an event label.
\pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
\node[causets/.cd, #1] (EL\causet@E@i)
  at (E\causet@E@i.\pgfkeysvalueof{/tikz/causets/at}) {#2};
}

\newcommand*{\causets@drawEventLabels}[1]{%
% Adds event labels to the events.
\foreach \causet@E@i/\causet@LocalStyle in {#1}{%
  \ifx\causet@E@i\causet@LocalStyle
    \ifcausetsDrawLabels
      \causets@drawEventLabel{\causet@E@i}
    \fi
  \else
    \def\causet@EventLabel{}
    \tikzcausetsset{set local style=\causet@LocalStyle}
    \ifx\causet@EventLabel\empty
      \ifcausetsDrawLabels
        \causets@drawEventLabel{\causet@E@i}
      \fi
    \else
      \causets@drawEventLabel[every replaced label]{\causet@EventLabel}
    \fi
  \fi
}
}

\newcommand*{\causets@drawContinuousLinks}[2][]{%
% Draws the links in full even if they cross over events.
\foreach \causet@From/\causet@To/\causet@LocalLinkStyle in {#2}{%
  \ifx\causet@To\causet@LocalLinkStyle
    \gdef\causet@LocalLinkStyle{#1}
  \fi
  \pgfmathsetmacro\causet@From{int(\causet@From + \causet@Offset)}
  \pgfmathsetmacro\causet@To{int(\causet@To + \causet@Offset)}
  \draw[causets/.cd, every link, set local style=\causet@LocalLinkStyle]
    (E\causet@From) -- (E\causet@To);
}
}

\newcommand*{\causets@drawBrokenLinks}[3][]{%
% Tests every link if it crosses over an unlinked event and breaks the link.
\foreach \causet@From/\causet@To/\causet@LocalLinkStyle in {#3}{%
  \ifx\causet@To\causet@LocalLinkStyle
    \gdef\causet@LocalLinkStyle{#1}
  \fi
  % In this function, \causet@u and \causet@v store the link distance in 
  % the u- and in the v-direction, respectively. Since these are coordinate 
  % differences, they are computed without adding the offset.
  \pgfmathsetmacro\causet@u{int(\causet@To) - int(\causet@From)}
  \def\causet@v{0}
  \foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#2}{%
    \pgfmathequal{\causet@E@i}{\causet@From}
    \ifnum1=\pgfmathresult
      \xdef\causet@v{\causet@i}
    \fi
    \pgfmathequal{\causet@E@i}{\causet@To}
    \ifnum1=\pgfmathresult
      \pgfmathparse{int(\causet@i - \causet@v)}
      \xdef\causet@v{\pgfmathresult}
      \breakforeach
    \fi
  }
  % Now add offset to events:
  \pgfmathsetmacro\causet@From{int(\causet@From + \causet@Offset)}
  \pgfmathsetmacro\causet@To{int(\causet@To + \causet@Offset)}
  % In this function, \causet@E@j stores the last breaking point and 
  % \causet@j the index of \causet@From.
  \pgfmathsetmacro\causet@E@j{int(\causet@From - 1)}
  \edef\causet@j{0}
  \foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#2}{%
    \pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
    \pgfmathequal{\causet@E@i}{\causet@To}
    \ifnum1=\pgfmathresult
      \edef\causet@j{0}
      \breakforeach
    \fi
    \ifnum0<\causet@j
      % If the u-/v-coordinate ratio is the same, the event \causet@E@i falls 
      % along the link and the link needs to break. The ratios are compared 
      % via cross-multiplication, which is faster and does not cause 
      % rounding errors:
      \pgfmathequal{int((\causet@E@i - \causet@From) * \causet@v)}{int(\causet@u * (\causet@i - \causet@j)))}
      \ifnum1=\pgfmathresult
        \pgfmathless{\causet@E@j}{\causet@From}
        \ifnum1=\pgfmathresult
          % Link is crossing over another event, so draw the link up to the 
          % (first) breaking point:
          \draw[causets/.cd, every link, {Link Start}-{Link Pause}, shorten >=\pgfkeysvalueof{/tikz/causets/broken link gap}, set local style=\causet@LocalLinkStyle]
            (E\causet@From) -- (E\causet@E@i);
        \else
          % Link is crossing over yet another event, so continue from last 
          % breaking point to next breaking point:
          \draw[causets/.cd, every link, {Link Resume}-{Link Pause}, shorten <=\pgfkeysvalueof{/tikz/causets/broken link gap}, shorten >=\pgfkeysvalueof{/tikz/causets/broken link gap}, set local style=\causet@LocalLinkStyle]
            (E\causet@E@j) -- (E\causet@E@i);
        \fi
        \xdef\causet@E@j{\causet@E@i}
      \fi
    \fi
    \pgfmathequal{\causet@E@i}{\causet@From}
    \ifnum1=\pgfmathresult
      \xdef\causet@j{\causet@i}
    \fi
  }
  \pgfmathgreater{\causet@E@j}{\causet@From}
  \ifnum1=\pgfmathresult
    % Link did cross over another event, so finish the link from last 
    % breaking point to final event:
    \draw[causets/.cd, every link, {Link Resume}-{Link End}, shorten <=\pgfkeysvalueof{/tikz/causets/broken link gap}, set local style=\causet@LocalLinkStyle]
      (E\causet@E@j) -- (E\causet@To);
  \else
    % Link does not cross over another event, so just draw it:
    \draw[causets/.cd, every link, set local style=\causet@LocalLinkStyle]
      (E\causet@From) -- (E\causet@To);
  \fi
}
}

\newcommand*{\drawpcauset}[1]{%
% Draws a causet from a permutation.
\causets@prepareCauset{#1}
\begin{scope}[rotate=45]
  \begin{scope}[xshift=-\causet@N * \causetTileSize / 2, 
                yshift=-\causet@N * \causetTileSize / 2]
    \causets@drawEvents{#1}
    \causets@drawPermutation{#1}
    \causets@drawSpatialLinks{#1}
    \ifcausetsDrawLinks
      \foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#1}{%
        \pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
        % \causet@Bound stores the upper bound to find linked events.
        \pgfmathsetmacro\causet@Bound{int(\causet@N + \causet@Start)}
        \foreach \causet@E@j/\causet@LocalStyle [count=\causet@j] in {#1}{%
          \pgfmathgreater{\causet@j}{\causet@i}
          \ifnum1=\pgfmathresult
            \pgfmathsetmacro\causet@E@j{int(\causet@E@j + \causet@Offset)}
            \pgfmathparse{and(\causet@E@j < \causet@Bound, \causet@E@i < \causet@E@j)}
            \ifnum1=\pgfmathresult
              % Add link and remember new upper bound:
              \draw[causets/.cd, every link]
                (E\causet@E@i) -- (E\causet@E@j);
              \xdef\causet@Bound{\causet@E@j}
            \fi
          \fi
        }
      }
    \fi
    \causets@drawEventLabels{#1}
  \end{scope}
\end{scope}
}

\newcommand*{\drawcauset}[2]{%
% Draws a causet from a permutation and a link-pair list.
\causets@prepareCauset{#1}
\begin{scope}[rotate=45]
  \begin{scope}[xshift=-\causet@N * \causetTileSize / 2, 
                yshift=-\causet@N * \causetTileSize / 2]
    \causets@drawEvents{#1}
    \causets@drawPermutation{#1}
    \ifcausetsDrawLinks
      \ifcausetsBreakLinks
        \causets@drawBrokenLinks{#1}{#2}
      \else
        \causets@drawContinuousLinks{#2}
      \fi
    \fi
    \causets@drawEventLabels{#1}
  \end{scope}
\end{scope}
}

\newcommand*{\drawrcauset}[2]{%
% Draws a causet from a permutation and ignoring links given in the second 
% list argument (of link-pairs).
\causets@prepareCauset{#1}
\begin{scope}[rotate=45]
  \begin{scope}[xshift=-\causet@N * \causetTileSize / 2, 
                yshift=-\causet@N * \causetTileSize / 2]
    \causets@drawEvents{#1}
    \causets@drawPermutation{#1}
    \causets@drawSpatialLinks{#1}
    % Add omitted links as spatial links:
    \ifcausetsDrawSpatialLinks
      \foreach \causet@From/\causet@To/\causet@LocalLinkStyle in {#2}{%
        \ifx\causet@To\causet@LocalLinkStyle
          \pgfmathsetmacro\causet@From{int(\causet@From + \causet@Offset)}
          \pgfmathsetmacro\causet@To{int(\causet@To + \causet@Offset)}
          \draw[causets/.cd, every spatial link]
            (E\causet@From) -- (E\causet@To);
        \fi
      }
    \fi
    \ifcausetsDrawLinks
      \foreach \causet@E@i/\causet@LocalStyle [count=\causet@i] in {#1}{%
        \pgfmathsetmacro\causet@E@i{int(\causet@E@i + \causet@Offset)}
        % \causet@Bound stores the upper bound to find linked events.
        \pgfmathsetmacro\causet@Bound{int(\causet@N + \causet@Start)}
        \foreach \causet@E@j/\causet@LocalStyle [count=\causet@j] in {#1}{%
          \pgfmathgreater{\causet@j}{\causet@i}
          \ifnum1=\pgfmathresult
            \pgfmathsetmacro\causet@E@j{int(\causet@E@j + \causet@Offset)}
            \pgfmathparse{and(\causet@E@j < \causet@Bound, \causet@E@i < \causet@E@j)}
            \ifnum1=\pgfmathresult
              % Check second list argument if the link has to be ignored:
              \global\causet@ItemFoundInListfalse
              \foreach \causet@From/\causet@To/\causet@LocalLinkStyle in {#2}{%
                \pgfmathparse{and(int(\causet@From + \causet@Offset) == int(\causet@E@i), int(\causet@To + \causet@Offset) == int(\causet@E@j))}
                \ifnum1=\pgfmathresult
                  \global\causet@ItemFoundInListtrue
                \fi
              }
              \ifcauset@ItemFoundInList
                \relax% Skip link as it is included in the second argument.
              \else
                % Draw link as it is not included in the second argument:
                \draw[causets/.cd, every link]
                  (E\causet@E@i) -- (E\causet@E@j);
              \fi
              % Remember new upper bound:
              \xdef\causet@Bound{\causet@E@j}
            \fi
          \fi
        }
      }
      \ifcausetsBreakLinks
        \causets@drawBrokenLinks[draw=none]{#1}{#2}
      \else
        \causets@drawContinuousLinks[draw=none]{#2}
      \fi
    \fi
    \causets@drawEventLabels{#1}
  \end{scope}
\end{scope}
}

\newcommand*{\pcauset}[2][]{%
% Inserts a TikZ picture with a causet, created from a permutation.
\causets@Padding%
\ifcausetsNameExternal%
  \causets@setCausetName{#1}%
\fi%
\begin{tikzpicture}[causets/.cd, every causet, #1]%
\drawpcauset{#2}%
\end{tikzpicture}%
\causets@Padding%
}

\newcommand*{\causet}[3][]{%
% Inserts a TikZ picture with a causet, created from a permutation and a 
% link-pair list.
\causets@Padding%
\ifcausetsNameExternal%
  \causets@setCausetName{#1}%
\fi%
\begin{tikzpicture}[causets/.cd, every causet, #1]%
\drawcauset{#2}{#3}%
\end{tikzpicture}%
\causets@Padding%
}

\newcommand*{\rcauset}[3][]{%
% Inserts a TikZ picture with a causet, created from a permutation and a 
% link-pair list of links to remove.
\causets@Padding%
\ifcausetsNameExternal%
  \causets@setCausetName{#1}%
\fi%
\begin{tikzpicture}[causets/.cd, every causet, #1]%
\drawrcauset{#2}{#3}%
\end{tikzpicture}%
\causets@Padding%
}

%% Import an external causets (to repeat it):
\newcommand*{\causetfile}[2][]{%
\causets@Padding%
\includegraphics[#1]{\causets@NamePrefix#2}%
\causets@Padding%
}

%% Short hand macros:
% DEPRECIATED with v1.5 and will be removed in the future.
\newcommand*{\pcausetP}[2][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelsfalse\pcauset[#1]{#2}}}
\newcommand*{\pcausetL}[2][]{%
{\causetsDrawPermutationfalse\causetsDrawLabelstrue\pcauset[#1]{#2}}}
\newcommand*{\pcausetX}[2][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelstrue\pcauset[#1]{#2}}}
\newcommand*{\causetP}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelsfalse\causet[#1]{#2}{#3}}}
\newcommand*{\causetL}[3][]{%
{\causetsDrawPermutationfalse\causetsDrawLabelstrue\causet[#1]{#2}{#3}}}
\newcommand*{\causetX}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelstrue\causet[#1]{#2}{#3}}}
\newcommand*{\rcausetP}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelsfalse\rcauset[#1]{#2}{#3}}}
\newcommand*{\rcausetL}[3][]{%
{\causetsDrawPermutationfalse\causetsDrawLabelstrue\rcauset[#1]{#2}{#3}}}
\newcommand*{\rcausetX}[3][]{%
{\causetsDrawPermutationtrue\causetsDrawLabelstrue\rcauset[#1]{#2}{#3}}}