# TikZ: Access coordinates of decorated paths?

Is it possible to access the coordinates of the decoration of some paths? I draw two paths, e.g., with:

   \begin{figure}[t]
\begin{tikzpicture}
\draw [red,decorate,decoration={crosses, segment length=1.05cm,
transform={shift only}}]
{(4, 4) .. controls(6, 3.5) .. (8, 4)};
\draw [red,decorate,decoration={crosses, segment length=1.05cm,
transform={shift only}}]
{(4, 4) .. controls(6, 4.5) .. (8, 4)};
\end{tikzpicture}
\end{figure}


Each of these paths contains some crosses I would like to connect with some arrows. If I could access the coordinates of the crosses, this would be quite simple. Is this somehow possible?

• Not to my knowledge, but you could modify the decoration to add coordinates at each of the points. Would that be acceptable? – Loop Space Aug 2 '12 at 9:54
• At least, this would be a very good workaround. How to do this? – Thomas W. Aug 2 '12 at 10:01
• You define a new decoration (probably a marking) that does the same as the cross that you have but adds a coordinate as well. The label for the coordinate should be of the form <base>-<number> where <base> is user-specified and <number> is incremented for each cross. There are lots of examples of modifying decorations on this site, I'm trying to find the most appropriate (my search skills are not great!). – Loop Space Aug 2 '12 at 10:04

Here's one way to do it which works by modifying the crosses decoration to add a labelling to each cross. You have the option to specify the prefix of the labelling and the numerical index increments as the decoration places crosses.

\documentclass{article}
%\url{http://tex.stackexchange.com/q/65557/86}

\usepackage{tikz}
\usetikzlibrary{decorations.shapes}

\makeatletter
% crosses decoration

\pgfkeys{/pgf/decoration/label cross name/.initial=cross}
\newcount\pgf@lblcross@index

\pgfdeclaredecoration{labelled crosses}{init}
{
\state{init}[width=+0pt,next state=crosses,persistent
precomputation={
\pgfkeysgetvalue{/pgf/decoration/label cross name}{\pgf@lblcross}
\global\pgf@lblcross@index=0\relax
\pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start
width}/2}
\edef\pgf@lib@dec@ssw{\pgfmathresult pt}
\pgfmathparse{\pgfkeysvalueof{/pgf/decoration/shape start
height}/2}
\edef\pgf@lib@dec@ssh{\pgfmathresult pt}
}]{}
\state{crosses}[switch if less than=+\pgfdecorationsegmentlength to
last,
width=+\pgfdecorationsegmentlength]
{
\pgfcoordinate{\pgf@lblcross-\the\pgf@lblcross@index}{\pgfpointorigin}
\pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
\pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
\pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
\pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
}
\state{last}[width=+\pgfdecoratedremainingdistance]
{
\pgfcoordinate{\pgf@lblcross-\the\pgf@lblcross@index}{\pgfpointorigin}
\pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
\pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
\pgfpathmoveto{\pgfqpoint{-\pgf@lib@dec@ssw}{-\pgf@lib@dec@ssh}}
\pgfpathlineto{\pgfqpoint{\pgf@lib@dec@ssw}{\pgf@lib@dec@ssh}}
}
\state{final}{
\pgfpathmoveto{\pgfpointdecoratedpathlast}
}
}

\makeatother

\begin{document}
\begin{tikzpicture}
\draw [postaction={draw=red,decorate},decoration={labelled
crosses, label cross name=A, segment length=1.05cm,
transform={shift only}}]
{(4, 4) .. controls(6, 3.5) .. (8, 4)};
\draw [postaction={draw=red,decorate},decoration={labelled
crosses, label cross name=B, segment length=1.05cm,
transform={shift only}}]
{(4, 4) .. controls(6, 4.5) .. (8, 4)};
\draw[->,green,dashed] (A-1) to[bend right] (B-2);
\draw[->,green,dashed] (A-3) to[bend left] (B-3);
\end{tikzpicture}
\end{document}


This produces:

(Actually, it doesn't but only because after generating the real picture I decided that it was too small to see what was going on so I scaled it up, but I couldn't be bothered to change the code above.)