1

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

Also tried How do I use the ampersand (&) inside a foreach or conditional (or other group/environment) when building tables?

\documentclass{minimal}
\usepackage{tikz,etoolbox}
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\global\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand\eaddtabtoks[1]{\edef\mytmp{#1}\expandafter\addtabtoks\expandafter{\mytmp}}
\newcommand*\resettabtoks{\global\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\makeatother
\begin{document}
\resettabtoks
\foreach \x/\y in {-2/a,-1/b,1/c,2/d}{%
  \eaddtabtoks{\x & \y \\ }%
}% 
\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, cmhughes May 23 '14 at 22:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    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
  • 1
    See tex.stackexchange.com/questions/165126/… – egreg May 23 '14 at 14:09
  • @egreg tried. Does not seem to work... – WG- May 23 '14 at 14:21
  • 1
    You have to say \protect\\ inside \eaddtabtoks – egreg May 23 '14 at 14:24
5

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)
\usepackage{xinttools}
\begin{document}
\begin{tabular}{ll}
    \xintForpair #1#2 in {(-2,a),(-1,b),(1,c),(2,d)}\do
     {#1&#2\\}
  \end{tabular}
\end{document}

generated table

Not the answer you're looking for? Browse other questions tagged or ask your own question.