4

In the the tikz/pgf manual, we can read in the subsection "14.13 The to path operation" about the key /tikz/execute at begin to but without any examples on how to use it concretely. So I am asking for some examples that may demonstrate how this key may be useful in practical cases. So please give one example by answer.

1 Answers1

6

OK, here is a rather simple but perhaps not completely pointless example which draws a line parallel to the line you want to draw.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\tikzset{
illustrate at begin to/.style=
    {to path={
      (\tikztostart) -- (\tikztotarget) \tikztonodes
    },
    execute at begin to={
    \draw[blue] ($(\tikztostart)!5pt!90:(\tikztotarget)$) --
    ($(\tikztotarget)!5pt!-90:(\tikztostart)$) node[midway,above,sloped]{I'm parallel};}}
}

\begin{document}
\begin{tikzpicture}
\draw[illustrate at begin to] (0,0) to (5,1);
\draw[illustrate at begin to] (0,-2) to (4,-3);
\end{tikzpicture}
\end{document}

enter image description here

Just for fun, another application, using Jake's complete sines and motivated by (my interpretation of) this question. (No, this trick does not work for curved paths, just in case you're wondering. ;-)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations,calc}
\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=sine,
        persistent precomputation={\pgfmathsetmacro\matchinglength{
            \pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
            \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
        }] {}
    \state{sine}[width=\pgfdecorationsegmentlength]{
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
    \state{final}{}
}

\begin{document}
\tikzset{fancy sines/.style={#1,decoration={
            complete sines,
            segment length=3mm,
            amplitude=1mm
        },decorate,to path={
      (\tikztostart) -- 
      ($(\tikztotarget)!{-5*\pgflinewidth}!-135:(\tikztostart)$)
      (\tikztotarget) \tikztonodes
    },
    execute at begin to={\foreach \X in {1,...,5}
    {\draw[decorate,#1]
    ($(\tikztostart)!{\X*\pgflinewidth}!45:(\tikztotarget)$) 
    -- ($(\tikztotarget)!{(-5+\X)*\pgflinewidth}!-135:(\tikztostart)$);}}}
}
\begin{tikzpicture}    
\draw[fancy sines=blue] (0,0) to (0,-3);
\draw[fancy sines=red] (0,0) to (3,0);
\draw[fancy sines=green!60!black] (3,0) to (0,-3);
\end{tikzpicture}
\end{document}

enter image description here