[Tex/LaTex] How to create lists in tikz

loopstikz-pgf

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}.

Best Answer

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?

1st result from macro, 2nd manually

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?

Related Question