If you use \foreach \i/\j in {1/2} { .. } then the \foreach macro can see the separator / and then splits 1 and 2. However, if both are hidden inside a macro like \testlist (defined to be 1/2) it does not find the separator and assumes that only a single value is used. Therefore \testlist is takes as \i and \j as empty.
You need to expand \testlist first, or use \foreach \i/\j in \testlist, because \foreach is defined to accept a macro instead of the { } argument.
Expanding would work as follows:
\edef\@tempa{\noexpand\foreach \noexpand\i/\noexpand\j in {\testlist, ..}}
\@tempa { <loop content> }
which works with multiple list macros and assumes \makeatletter and \makeatother around the code.
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}
\end{document}
Or alternatively:
\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\list{#5}
\foreach \i/\j in \list {
\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\alist{1/0}
\rectDiv{7}{5}{(1,1)}{(4,3)}{\alist}
\def\blist{5/3}
\rectDiv{7}{5}{(1,1)}{(4,3)}{\alist,2/0,\blist}
\end{document}
rectDivmacro, remove the braces around#5i.e. replace that line with\foreach \i/\j in #5 {and it would work. – percusse Dec 19 '11 at 14:18\foreachloop to generate a list for another\foreachloop. It isn't fully expandable and therefore breaks inside the\edef. – Martin Scharrer Dec 19 '11 at 21:06