# Why does this \pgfmathparse not work outside the \foreach loop?

The LaTeX code below builds in Sublime Text 2 and gives the results:

7, -3.0, 4, -9.0, 11, -9.0, 1, two, 3.0, IV, cinq, sechs, 7.0,

But it gives the following errors when I remove the % character:

./example6.tex:12: Undefined control sequence. [\pgfmathparse{\myarray[3]}] ./example6.tex:13: Paragraph ended before \pgfmath@dimen@@ was complete. []

I'm using the latest version of tikz 3.0 and the 2013 version of MacTeX.

\documentclass[12pt]{memoir}
\usepackage{tikz}

\begin{document}

\def\myarray{{7,-3,4,-9,11}}
\foreach \i in  {0,...,4}{\pgfmathparse{\myarray[\i]}\pgfmathresult, }
\pgfmathparse{\myarray[3]}\pgfmathresult,
\def\myarray{{1,"two",2+1,"IV","cinq","sechs",sin(\i*5)*14}}
\foreach \i in  {0,...,6}{\pgfmathparse{\myarray[\i]}\pgfmathresult, }
%\pgfmathparse{\myarray[3]}\pgfmathresult,

\end{document}

-
By \myarray[x] you're trying to reference the x-th entry in \myarray? If so, TeX doesn't work that way... –  Werner Mar 31 '14 at 16:03

This is because outside the foreach loop \i index is not defined as a number. And TikZ touches every entry in the array while looking for the third element (I'm not sure whether this is due to expansion of the array or just to get the final number of elements in case a macro expands to multiple elements)

So if you remove the last \i in the array then you can use it.

\documentclass[12pt]{memoir}
\usepackage{tikz}

\begin{document}

\def\myarray{{7,-1+2,4,-9,11,"a",2+1,1,"two",2+1,"IV","cinq","sechs"}}
\foreach \i in  {0,...,12}{\pgfmathparse{\myarray[\i]}\pgfmathresult, }
\pgfmathparse{\myarray[3]}\pgfmathresult,

\def\myarray{{1,"two",2+1,"IV","cinq","sechs",{(sin(\i*5)*14)}}}
\foreach \i in  {0,...,6}{\pgfmathparse{\myarray[\i]}\pgfmathresult, }

\def\myarray{{1,"two",2+1,"IV","cinq","sechs",{(sin(0.5*5)*14)}}}
\pgfmathparse{\myarray[3]}\pgfmathresult,

\end{document}

-