I'd appreciate if somebody could explain to me why the to path `rectangle 1' doesn't work, whereas `rectangle 2' does work.
Note that I've added as scope in the to path `side 1' which is used in `rectangle 1'. I had hoped this would keep the definitions of \p1 and \p2 local.
(Note that this example is derived from a more complex example. It is not about drawing rectangles:-)
\tikzset{rectangle 1/.style={to path={%
let \p1=(\tikztostart), \p2=(\tikztotarget)
in (\p1) to (\p1 -| \p2)
to (\p2)
to (\p1 |- \p2)
to (\p1) \tikztonodes (\p1 -| \p2)}}}
\tikzset{side 2/.style={to path={%
let \p3=(\tikztostart), \p4=(\tikztotarget)
in (\p3) - (\p4) \tikztonodes
}}}
\tikzset{rectangle 2/.style={to path={%
let \p1=(\tikztostart), \p2=(\tikztotarget)
in (\p1) to (\p1 -| \p2)
to (\p2)
to (\p1 |- \p2)
to (\p1) \tikztonodes (\p1 -| \p2)}}}
\begin{document}
\begin{tikzpicture}
\draw (0,0) to (2,2);
\draw (3,0) to (5,2);
\end{tikzpicture}
\end{document}
Regards,
Marc van Dongen
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Dear all,
I'd appreciate if somebody could explain to me why the to path `rectangle 1' doesn't work, whereas `rectangle 2' does work.
Note that I've added as scope in the to path `side 1' which is used in `rectangle 1'. I had hoped this would keep the definitions of \p1 and \p2 local.
(Note that this example is derived from a more complex example. It is not about drawing rectangles:-)
Thanks in advance for your help.
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{scopes}
\tikzset{side 1/.style={to path={%
{let \p1=(\tikztostart), \p2=(\tikztotarget)
in (\p1) - (\p2) \tikztonodes}
}}}
\tikzset{rectangle 1/.style={to path={%
let \p1=(\tikztostart), \p2=(\tikztotarget)
in (\p1) to (\p1 -| \p2)
to (\p2)
to (\p1 |- \p2)
to (\p1) \tikztonodes (\p1 -| \p2)}}}
\tikzset{side 2/.style={to path={%
let \p3=(\tikztostart), \p4=(\tikztotarget)
in (\p3) - (\p4) \tikztonodes
}}}
\tikzset{rectangle 2/.style={to path={%
let \p1=(\tikztostart), \p2=(\tikztotarget)
in (\p1) to (\p1 -| \p2)
to (\p2)
to (\p1 |- \p2)
to (\p1) \tikztonodes (\p1 -| \p2)}}}
\begin{document}
\begin{tikzpicture}
\draw (0,0) to (2,2);
\draw (3,0) to (5,2);
\end{tikzpicture}
\end{document}
Regards,
Marc van Dongen