# How to create lists in tikz

I would like to create lists and use them in tikz \foreach loops. Example:

\newcommand{\createSquareList}[1]{ this is the part I do not know }

\foreach \n in \createSquareList{5}
{
% do something with \n
}


Where the expression \createSquareList{5} should create the list of the first 5 square numbers, i.e. {1,4,9,16,25}.

-
In the example you give, a simpler solution would be to % do something with \n*\n; for example \foreach \n in {1,...,5} {\draw (0,\n*\n) circle[radius=10pt];}. But maybe you have another example where that wouldn't be possible. – Loop Space Aug 20 '11 at 9:56
The function I am really interested in is much more complicated than \n*\n. But with your comment, the problem may be solved by defining a function and using {1,...,n}. But it would still be great to do it in one step as in the initial example. – Felix Wellen Aug 20 '11 at 10:14
One reason for doing it in a way similar to that which you describe would be if you were going to use the same list many times. But if it's only a few then putting the formula inside the list would be simpler. My initial thought on how to create the list in the first place was to use a \foreach loop, by the way. – Loop Space Aug 20 '11 at 10:22

I think I have an answer that should work, but it does not, apparently as something does not work correctly with the \pgfmathparse{} and \pgfmathresult macros. Here's what I have done:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\newcommand{\listgen}[4]% expression, start, start+step, stop
{   \foreach \n in {#2,#3,...,#4}
{   \pgfmathparse{#1}
\node at (0,\n/2) {\pgfmathresult};
\node at (-1,\n/2) {\pgfmathresult};
\node at (\n,0) {\pgfmathresult};
\node at (\n,-1) {\pgfmathresult};
}
}

\begin{document}

\begin{tikzpicture}[framed]
\listgen{\n*\n}{1}{2}{5}
\end{tikzpicture}

\begin{tikzpicture}[framed]
\foreach \n in {1,2,...,5}
{   \pgfmathparse{\n*\n}
\node at (0,\n/2) {\pgfmathresult};
\node at (-1,\n/2) {\pgfmathresult};
\node at (\n,0) {\pgfmathresult};
\node at (\n,-1) {\pgfmathresult};
}
\end{tikzpicture}

\end{document}


I defined a command to take start and stop value, the stepping and the expression. First the expression is evaluated by tikz, then it should be drawn in a node. But ehat really happens is that \pgfmathresult always returns zero. What's even more weird is that it's conditionally changed to the x-coordinate of the node. To make sure it is not my definition of \listgen, I also did the same thing manually, but still the same strange result.

Could you please run this to check wheater the error is reproducable?

Edit: Hmm, thats odd. I already used \pgfmathresult successfully, like this:

\begin{tikzpicture}[framed]
\foreach \x in {70,71,...,79}
{   \pgfmathparse{(\x-70)*5+25}
\fill[green!\pgfmathresult!black] (\x/10,1.6-\x/50) rectangle (\x/10+0.1,1.4-\x/50);
}
\end{tikzpicture}


Which should give a green staicase like thing, where the color changes with the variable through \pgfmathparse. Probably it is a bug in tikz?

Edit 2: Wohoo, it works. Thanks to Jake :D

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\newcommand{\listgen}[4]% expression, start, start+step, stop
{   \foreach \n in {#2,#3,...,#4}
{   \pgfmathsetmacro{\myresult}{#1};
\node at (0,\n/2) {\myresult};
\node at (-1,\n/2) {\myresult};
\node at (\n,0) {\myresult};
\node at (\n,-1) {\myresult};
}
}

\begin{document}

\begin{tikzpicture}[framed]
\listgen{\n*\n}{1}{2}{5}
\end{tikzpicture}

\end{document}


Edit 3: Thanks to Andrey Vihrov and Jake in this post, I was able to let it return lists as strings with brackets:

\documentclass{article}
\usepackage{tikz}
\usepackage{ifthen}
\usetikzlibrary{backgrounds}

\newcommand{\listgen}[4]% expression, start, start+step, stop
{   \gdef\myresult{}%
\foreach \n in {#2,#3,...,#4}%
{   \pgfmathsetmacro{\tikzresult}{#1}%
\ifthenelse{\n=#2}{\global\edef\myresult{(\tikzresult, }}%
{   \ifthenelse{\n=#4}{\global\edef\myresult{\myresult\tikzresult)}}%
{   \global\edef\myresult{\myresult\tikzresult, }%
}%
}%
}%
\myresult%
}

\begin{document}

\listgen{\n*\n}{1}{2}{5}\\

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\\

\begin{tikzpicture}
\draw (0,1) -- (10,1);
\node[right] at (0,0.5) {\listgen{\n*\n}{1}{2}{5}};
\draw (0,0) -- (10,0);
\end{tikzpicture}

\end{document}


which returns (1.0, 4.0, 9.0, 16.0, 25.0)

It uses TikZ for math calculations, so it can be used inside or outside of a TikZ environment. However it crashes if I try to put the list in {} via \{ \} in the commands. Does anyone know why?

-
I got the same output. – Felix Wellen Aug 20 '11 at 12:47
The last tikzpicture of your first code example works, if \pgfmathparse{\n*\n} is replaced by \def\result{\pgfmathparse{\n*\n}\pgfmathresult} and every following \pgfmathresult by \result. – Felix Wellen Aug 20 '11 at 13:14
\pgfmathresult gets overwritten by all kinds of things very quickly. You generally can't rely on the contents of that macro, unless you use it directly after \pgfmathparse. Usually, you should use \pgfmathsetmacro\<some macro>{<expression>}, and use \<some macro> later on to access the result. – Jake Aug 20 '11 at 13:31