I have a slightly different question from the already asked here on TeX.SE. I would like to define many paths inside a foreach statement and then use their names later on in the drawing.
When having one or few paths and no recursive code it is easy to use the save path and use path syntax as in the posted question. Sadly, when there are as many path as in the below MWE I cannot save them with save path=<recursive-path-name-expr>. Suppose e.g. I want to draw a certain number of arrows between two rectangles and then label someone of them by positioning a sloped node midway
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\newlength{\rectlong}
\setlength{\rectlong}{2cm}
\newlength{\rectshort}
\setlength{\rectshort}{1cm}
\begin{document}
\begin{tikzpicture}[mrect/.style={draw, minimum height=\rectlong, minimum width=\rectshort, rounded corners, outer sep=.1em}]
\node[mrect, fill=blue] (r1) {};
\node[right=of r1][mrect, fill=red] (r2) {};
\foreach \x [count=\i] in {-0.9, -0.6, ..., 0.9} {
\path[name=line\i] (r1) -- ([yshift={\x*\rectlong/2}] r2.west);
}
\end{tikzpicture}
\end{document}
Substituting \path with draw in the aforementioned code produce the following picture.
Now suppose i want to put a sloped node after some piece of code on just selected line\i paths. I would like to have some command such as
\draw[use path=line5] node[scale=0.2, midway, sloped, above]{text along line};
to arrive to the following picture. A part from this specific example which can be achieved easily through other ways, is there a general way to reuse a path previously named with name=<path-name> without referring with a specific one with the save/use path syntax?





foreachis that local definitions get lost. One would have to dig through the code to find the macro name actually used by TikZ for path names. Then you need a\global\letusing\csname ...\endcsnameand a whole lot of\expandafters. – John Kormylo Jun 28 '22 at 14:29\endcsnamecould be a nice solution, but is seems a little bit crazy to me that i cannot use paths and define them recursively as i do with nodes, and always did on tikz with very useful results. – GiuTeX Jun 28 '22 at 15:52\draw[name=line5] (r1) -- ([yshift={0.3*\rectlong/2}] r2.west);\draw[use path=line5] node[scale=0.2, midway, sloped, above]{text along line};– John Kormylo Jun 28 '22 at 19:48