Is it possible to use a loop to generate a list for a foreach loop? More specifically I want the following:
Consider the following answer to a question I asked some time ago: https://tex.stackexchange.com/a/38793/4011
It worked very well except if I define \alist
as follows:
\def\alist{
\foreach \i in {1,2,3}{
\i/1,
}4/1
}
I get the following error:
ERROR: Undefined control sequence.
--- TeX said ---
\foreach ...reach \let \pgffor@assign@before@code
=\pgfutil@empty \let \pgff...
l.38 \rectDiv{7}{5}{(1,1)}{(4,3)}{\alist}
Is there any way to fix this?
Edit:
Full example:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\rectDiv#1#2#3#4#5{%#columns, #rows, rectangle start, rectangle end, list of elements to fill
\begin{tikzpicture}
\draw #3 rectangle #4;
\path #3;
\pgfgetlastxy{\firstx}{\firsty}
\path #4;
\pgfgetlastxy{\secondx}{\secondy}
\pgfmathsetlengthmacro{\xdiff}{\secondx-\firstx}
\pgfmathsetlengthmacro{\ydiff}{\secondy-\firsty}
\pgfmathsetlengthmacro{\myxstep}{\xdiff/#1}
\pgfmathsetlengthmacro{\myystep}{\ydiff/#2}
\foreach \x in {1,...,#1}{
\draw ($#3 +\x*(\myxstep,0)$) -- ($#3 +(0,\ydiff) +\x*(\myxstep,0)$);
}
\foreach \y in {1,...,#2}{
\draw ($#3 +\y*(0,\myystep)$) -- ($#3 +(\xdiff,0) +\y*(0,\myystep)$);
}
\edef\temp{\noexpand\foreach \noexpand\i/\noexpand\j in {#5}}
\temp{
\path[fill=blue!20,draw] ($#3 + (\i*\myxstep,\j*\myystep)$) rectangle ($#3 + (\i*\myxstep,\j*\myystep) + (\myxstep,\myystep)$);
}
\end{tikzpicture}
}
\begin{document}
\rectDiv{7}{5}{(1,1)}{(4,3)}{0/0,1/1,2/0,5/3}
\def\list{1/0}
\rectDiv{7}{5}{(1,1)}{(4,3)}{\list}
\rectDiv{7}{5}{(1,1)}{(4,3)}{\list,2/0,5/3}
\def\alist{
\foreach \i in {1,2,3}{
\i/1,
}4/1
}
\alist
\rectDiv{7}{5}{(1,1)}{(4,3)}{\alist} %this doesn't work
\end{document}