With a slightly different syntax, you can do this:
\foreach \sty/\annotation/\xi/\xii/\yi/\yii in {%
<sty 1>/<annotation 1>/<xi 1>/<xii 1>/<yi 1>/<yii 1>,
<sty 2>/<annotation 2>/<xi 2>/<xii 2>/<yi 2>/<yii 2>,
…,
<sty n>/<annotation n>/<xi n>/<xii n>/<yi n>/<yii n>}
\draw[style/.expand once=\sty]
(\xi, \yi) -- (\xi, \yi + \yii) node[midway, right] {\annotation}
-- (\xi + \xii, \yi + \yii) -- (\xi + \xii, \yi) -- cycle;
It also might be useful to use relative coordinates:
\draw[style/.expand once=\sty]
(\xi, \yi) -- + (up:\yii) node[midway, right] {\annotation}
-- + (\xii, \yii) -- + (right:\xi) -- cycle;
or even
\draw[style/.expand once=\sty]
(\xi, \yi) |- + (\xii, \yii) node[near start, right] {\annotation} |- + (0, 0) -- cycle;
The code below shows an example and the same example with the a to path
and the to path
example with a .list
ed key.
This allows also other coordinate systems than the cartesian one for both corners of the rectangles. (With the edges
on one path, you can also specify the first corner of the rectangle as a relative coordinate to another rectangle.)
There is also the rectangle
path operator, i.e (corner A) rectangle (corner B)
, but unfortunately, there is no proper timer setup to place the nodes, see Is It Possible to Combine TikZ Distance and Line-To Operations?
You might as well consider doing this as a node, see tikz: Can we set the corner coordinates of a rectangle?
Code
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \sty/\annotation/\xi/\xii/\yi/\yii in {%
draw /Foo/ 0 /1/0 /2 ,
{fill=blue!25, draw=blue} /Bar/ 1.5/2/-.5/1.5,
{transform shape,rotate=30}/Woah!/2 /2/0.5/1 }
\draw[style/.expand once=\sty]
(\xi, \yi) |- + (\xii, \yii) node[near start, right] {\annotation}
|- + (0, 0) -- cycle;
\end{tikzpicture}
\tikzset{
my Rect/.style={
to path={
|- node[near start, right] {#1} (\tikztotarget) |- (\tikztostart)}}}%
\begin{tikzpicture}
\path (0, 0) edge[my Rect=Foo] + (1,2)
(1.5, -.5) edge[my Rect=Bar, fill=blue!25, draw=blue] + (2, 1.5)
(2, 0.5) edge[my Rect=Woah!, rotate=30, transform shape] + (2,1);
\end{tikzpicture}
\begin{tikzpicture}[my Foreach/.style args={[#1]#2:#3)#4}{
insert path={#3) edge[my Rect={#2},#1] #4}}]
\path [my Foreach/.list={{[] Foo: (0,0) +(1,2) },
{[fill=blue!25, draw=blue] Bar: (1.5,-.5) +(2,1.5)},
{[rotate=30, transform shape] Woah!:(2,0.5) +(2,1) }}];
\end{tikzpicture}
\end{document}