# tikz array indexing

I'm trying to generate glyph plot. The idea is that I've a list of elements with 4 values (0.0 .. 1.0) that represent the value of a particular characteristic. My goal is to connect those points but I cannot access the data within the elements array. I tried some macros, and using \pgfmathparse with no success. Here's an example of what I'm trying to do:

\documentclass{standalone}
\usepackage{pgfmath,pgffor}
\begin{document}
\def\elements{{0.5,0.5,0.59,0.5}, {0.2,0.5,0.5,0.8} }% romboide and a diamond shape

\foreach \elementPoints [count=\i] in \elements {
\draw ({2.2*\i},0) ++(\elementPoints[1],0) node (a) {}
({2.2*\i},0) ++(0,\elementPoints[2]) node (b) {}
({2.2*\i},0) ++({-1*\elementPoints[3]},0) node (c) {}
({2.2*\i},0) ++(0,{-1*\elementPoints[4]}) node (d) {};

\draw (a) -- (b) -- (c) -- (d) -- (a);
}

\end{document}


Thanks

You have a few errors in your code:

You load only the packages pgfmath and pgffor but not tikz, yet you use \draw and TikZ’s path syntax. Though, they also need the tikzpicture environment (or the \tikz macro).

The index of the PGFmath arrays start at 0 not 1.

The arrays in the \elements macro need another set of braces { } so as do indicate that they are in fact arrays, the first pair of braces are stripped away when \foreach splits the list in its two arrays.

Furthermore, you use nodes which are by default of the shape rectangle and have vertical and horizontal dimensions. This, I believe, is not what you want. Use coordinates if you simply want to save a coordinate under a name:

\foreach \elementPoints [count=\i] in \elements {
\path (2.2*\i,0) +(   \elementPoints[0],                    0) coordinate (a)
+(                   0,    \elementPoints[1]) coordinate (b)
+(-1*\elementPoints[2],                    0) coordinate (c)
+(                   0, -1*\elementPoints[3]) coordinate (d);

\draw (a) -- (b) -- (c) -- (d) -- cycle;
}


Instead of saving the coordinates and connecting them later, you can also connect them direct on the path.

Eitherway, maybe you are interested in a shorter version of your code using an insert path style and polar coordinates (which could have been used in your example, too).

### References

• PGF manual
• Difference between + and ++ (section 2.15 “Specifying Coordinates”, pp. 31f.)
• Key insert path (chapter 14 “Syntax for Path Specications”, p. 139)
• Take a look at the kite shape in section 62.3 “Geometric Shapes”, pp. 623f.

## Original approach

\documentclass[tikz,convert]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\elements{{{0.5,0.5,0.59,0.5}}, {{0.2,0.5,0.5,0.8}}}% romboide and a diamond shape

\foreach \elementPoints [count=\i] in \elements
\draw (2.2*\i,0) +(   \elementPoints[0],                    0)
-- +(                   0,    \elementPoints[1])
-- +(-1*\elementPoints[2],                    0)
-- +(                   0, -1*\elementPoints[3])
-- cycle;
\end{tikzpicture}
\end{document}


## Different approach

\documentclass[tikz,convert]{standalone}
\usepackage{tikz}
\tikzset{
romb/.style args={#1:#2:#3:#4}{
insert path={   +(right:{#1}) -- +(   up:{#2})
-- +( left:{#3}) -- +( down:{#4}) -- cycle
}
}
}
\begin{document}
\begin{tikzpicture}
\draw[            draw=blue,  fill=red ] (0,0) [romb=.5:.5:.59:.5];
\draw[very thick, draw=green, fill=blue] (2,0) [romb=.2:.5:.5 :.8];
\end{tikzpicture}
\end{document}