# Generate table using the foreach command from pgffor [duplicate]

I want to generate a table from a list. However for some reason it fails to work. I searched:

PGFplots foreach equivalent to TikZ's with multiple variables separated by a slash

Copy table row n times

and tried different solutions but none of them work for some reason :(

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tabular}{ll}
\foreach \x/\y in {-2/a,-1/b,1/c,2/d} {%
\edef\temp{\noexpand \x & \y \\ }%
\temp
}%
\end{tabular}
\end{document}


Edit...

\documentclass{minimal}
\usepackage{tikz,etoolbox}
\makeatletter
\newtoks\@tabtoks
\newcommand*\resettabtoks{\global\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\begin{document}
\resettabtoks
\foreach \x/\y in {-2/a,-1/b,1/c,2/d}{%
}%
\begin{tabular}{ll}
\printtabtoks
\end{tabular}
\end{document}


Does not seem to work.

(/usr/local/texlive/2013/texmf-dist/tex/generic/oberdiek/etexcmds.sty))) (/usr/local/texlive/2013/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg)) ! Undefined control sequence. \ ->\let \reserved@e \relax \let \reserved@f \relax \@ifstar {\let \reserv... l.17 } % ?

## marked as duplicate by WG-, egreg, Werner, user31729, cmhughesMay 23 '14 at 22:21

• This has appeared several times, I believe. – egreg May 23 '14 at 14:06
• Thats true. But why doesn't my code work then? I tried both solutions in the topics to which I link. – WG- May 23 '14 at 14:08
• – egreg May 23 '14 at 14:09
• @egreg tried. Does not seem to work... – WG- May 23 '14 at 14:21
• You have to say \protect\\ inside \eaddtabtoks – egreg May 23 '14 at 14:24

If you don't mind changing loops and a bit syntax for that specific task, there are other choices such as \xintForpair, naturally not having all the nice functionalities of \foreach, but having some others.
\documentclass{article}\usepackage{tikz}% (for other things)