I am trying to simplify my work with some latex coding, where the intention is to generate two types of diagrams based on the input. The first type is diagrams with lines only going up
and the second is where the lines are allowed to go back like these (draw with PowerPoint):
I already have the working code for the first type of diagrams here where the diagrams' dots on both top and bottom are numbered from left to right 1 to n. Below is a MWE of the code.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\tikzset{pics/planar/.style 2 args = {
code = {
\draw[color=red] (0,0) rectangle (#1*0.3+0.3,0.7);
\foreach \dot in {1,...,#1}{ % draw the dots
\filldraw (0.3*\dot,0) circle [radius=1pt];
\filldraw (0.3*\dot,0.7) circle [radius=1pt];
}
% draw the lines
\foreach \x/\y in #2
\draw[->,>=stealth](0.3*\x,0) .. controls +(0,0.2) and +(0,-0.2) .. (0.3*\y,0.7);
}
}
}
\usepackage{xparse}
\NewDocumentCommand\PlanarDiagram{ O{} D(){3} m }{%
\begin{tikzpicture}[#1]
\foreach \diag [count=\c] in {#3} {
\draw(0,\c*0.7) pic[#1]{planar={#2}{\diag}};
}
\end{tikzpicture}%
}
\begin{document}
\PlanarDiagram(5){{1/2}} \quad
\PlanarDiagram{{1/2,2/2}, {2/3,3/1}} \quad
\PlanarDiagram(4){{1/2, 3/1, 3/3}, {1/1, 2/3, 3/3}}
\PlanarDiagram[scale=0.7, draw=blue](4){{1/2, 3/1, 3/3}, {1/1, 2/3, 3/3}}
\end{document}
My hope is that if I number the dots on the top positively from 1 to n and number the dots at the bottom negatively from -1 to -n, I can separate the two types of lines by the commands' input so the command could be like this
\NewPlanarDiagram{{-1/-4,-2/-3,1/2,3/4}} % diagram on the left
\NewPlanarDiagram{{-1/3,-2/-3,1/2,4/4}} % diagram on the right
I could do something like this to the macro in the for loop
\foreach \x/\y in #2
\ifnum \x < 0
\let \signx=-1
\else
\let \signx=1
\fi
\ifnum \y < 0
\let \signy=-1
\else
\let \signy=1
\fi
\let \xysign= \multiply\signx by \signy
\ifnum\xysign<0
\draw(0.3*\x,0.35+\signx*0.35) .. controls +(0,0.2) and +(0,-0.2) .. (0.3*\y,0.35+\signy*0.35);
\else
\draw(0.3*\x,0.35+\signx*0.35) .. controls +(0,0.2) and +(0,-0.2) .. (0.3*\y,0.35+\signy*0.35);
\fi
but there are numerous error messages coming out of it. Any ideas?