# Ideas/Help on Drawing Braid Permutations?

I've been using the braids package and it has worked quite well for me. I'm trying to "decorate" the braids that I'm drawing, and since TikZ can often take a lot of time to draw manually, I'm trying to automate this process. What I want is:

• Top and bottom bars to my braid
• Numeric labels "1,2, ..., n" at the top bar
• The corresponding labels at the bottom given by the braid itself (which brings me to my question)

I've mostly got all of this except the last point. An example is below: Specifically I have the first and second things I want. Using the code of Shrodinger's Cat in another question of mine, I was shown how to automatically draw the top and bottom bars. Then, I used the calc package and a for loop to draw the top numbers "1, 2, 3, ..." automatically at the top.

My question: I of course want to draw the numbers at the bottom, right? But how should I do this? This is kind of hard because you need to now calculate the permutation of the braid in question.

My ideas:

1. Create a program in TeX that (1) parses a copy of my braid e.g. "s_1 s_2 s_3 ..." The program will parse through this string, recover its underlying permutation (this is the hard part). Using this underlying permutation, TikZ then knows where to draw all of the integers at the bottom bar. Perhaps I could use somehow get LaTeX to communicate with a python shell, because this is probably easier to code in python.
2. Simply copy and paste "\node at ..." over and over again and manually label the bottom bar. This is less elegant and less in the spirit of automating the drawing, but perhaps method (1) will require so many for loops that it will take forever to compile and ultimately be useless.

However, does anyone have any other ideas on how to do this? I'm looking for suggestions/package references since this is kind of hard, and maybe there's an easier way I'm not seeing.

MWE of above braid:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{braids}
\begin{document}
\def\nstrands{4}    % NUMBER OF STRANDS
\newcounter{xcoord}
\begin{tikzpicture}
\pic[local bounding box=my braid,braid/.cd,
number of strands = \nstrands,
thick, % change me if you want
name prefix=braid]
{braid={ s_1, s_2, s_3}}; % BRAID GENERATORS
\draw[thick] % the top and bottom bars; help from https://tex.stackexchange.com/questions/549755/tikz-braids-how-to-draw-singular-braids-intersections
([xshift=-1ex]my braid.north west) --  ([xshift=1ex]my braid.north east)
([xshift=-1ex]my braid.south west) --  ([xshift=1ex]my braid.south east);
% labels the top bar with numbers automatically
\foreach \n in {1,...,\nstrands}{
\setcounter{xcoord}{\n -1}
\node at ([xshift = \thexcoord cm, yshift = 0.3 cm]my braid.north west) {\n};
}
% Prints the numbers on the bottom bar...
% ..One problem is calculating the permutation of the braid.
% theoretically, this is very easy, but for us it requires (1) a parser
% (2) a permutation calculator (3) a way to return these values
% back into latex...
% They're drawn manually for now.
\node at ([xshift = 0 cm, yshift = -0.3 cm]my braid.south west) {2};
\node at ([xshift = 1 cm, yshift = -0.3 cm]my braid.south west) {3};
\node at ([xshift = 2 cm, yshift = -0.3 cm]my braid.south west) {4};
\node at ([xshift = 3 cm, yshift = -0.3 cm]my braid.south west) {1};
\end{tikzpicture}
\end{document}

• Please read the documentation. There are nodes created at the ends of the strands indexed by the strand number. With these and a loop you can achieve what you want very easily. Jun 20, 2020 at 20:51
• Of course! I just didn't think it'd be in there. I found it and figured out how to do it, thank you. Jun 20, 2020 at 21:20

As Andrew pointed out, sometimes you just need to read the documentation. So here's my solution in case this is of any use to anyone. The code below draws the top/bottom bars automatically as well as labels the bars appropriately.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{braids}
\begin{document}
\def\nstrands{4}
\begin{tikzpicture}
\pic[local bounding box=my braid,braid/.cd,
number of strands = \nstrands, % number of  strands
thick,
name prefix=braid]
{braid={ s_1, s_2, s_3}}; %the generators
\draw[thick] % draws the top/bottom bars
([xshift=-1ex]my braid.north west) --  ([xshift=1ex]my braid.north east)
([xshift=-1ex]my braid.south west) --  ([xshift=1ex]my braid.south east);
% labels the top bar
\foreach \n in {1,...,\nstrands}{
\node at (braid-\n-s)[yshift = 0.3cm] {\n};
}
% labels the bottom bar
\foreach \n in {1,...,\nstrands}{
\node at (braid-\n-e)[yshift = -0.3cm] {\n};
}
\end{tikzpicture}
\end{document}


The above code produces 