# loop makes empty tabular

First, I have seen a couple similar posts, which is how I got as far as I have gotten:

loop and char &

problem with using loop inside the tabular environment

I am also pretty sure this could be done with edef, but I it seems that one could do it without also.

I am using collection.sty; the relevent parts are here:

\newcommand*{\collectionnew}[1]{%
\newcounter{collection@#1@count}}
\expandafter\def\csnamecollection@#2@item\roman{collection@#2@count}\endcsname{#3}%
\if\relax\noexpand#1\relax% if #1 is empty
\else\expandafter\def\csnamecollection@#2@key\roman{collection@#2@count}\endcsname{#1}\fi%
\stepcounter{collection@#2@count}}
\newcommand*{\collectiongetitem}[2]{%
\csname collection@#1@item\romannumeral #2\endcsname}
\newcommand*{\collectiongetkey}[2]{%
\csname collection@#1@key\romannumeral #2\endcsname}
\newcounter{collection@iterator}
\newcommand*{\collectionloop}[2]{%
\setcounter{collection@iterator}{0}%
\loop\ifnum\value{collection@iterator}<\value{collection@#1@count}%
\def\collectionloopid{\arabic{collection@iterator}}%
\def\collectionloopitem{\collectiongetitem{#1}{\collectionloopid}}%
\def\collectionloopkey{\collectiongetkey{#1}{\collectionloopid}}%
#2%
\stepcounter{collection@iterator}%
\repeat}


The full file is found at https://github.com/glittershark/resume/blob/master/collection.sty

I am trying to take a collection (with a column delimiter in collectionloop item and make a tabular

\documentclass{article}
\usepackage{collection}
\collectionnew{sEvents}

\def\schedulelines{}%
\collectionloop{sEvents}{%
\expandafter\def\expandafter\schedulelines\expandafter{\schedulelines \collectionloopkey}
\expandafter\def\expandafter\schedulelines\expandafter{\schedulelines & }
\expandafter\def\expandafter\schedulelines\expandafter{\schedulelines \collectionloopitem}
\expandafter\def\expandafter\schedulelines\expandafter{\schedulelines \\ \hline }
}

\begin{document}
\begin{tabular}{|l|c|l|}
\hline\textbf{col1}&\textbf{col2}&\textbf{col3}\\
\hline
\schedulelines
\end{tabular}
\end{document}


I should get a 3x3 tabular, every cell boxed, with row 2 being A B C and row three being X Y Z. Instead I get a 3x3 tabular with rows two and three empty and the right boarder line missing.

I have also tried replacing & with a command to hide it, \def\TAB{&}, in both the \collectionadd and the \expandafter inside the loop but it did not change anything that I could tell.

• Please, make a compilable example. – egreg Aug 14 '18 at 16:16
• @egreg, is that sufficient or should I put the .sty commands in with the document? – AThomack Aug 14 '18 at 16:47
• Where's collection.sty available? – egreg Aug 14 '18 at 17:46
• @egreg, I put a link in the post now. – AThomack Aug 14 '18 at 19:57

Not sure whether I'd use an undocumented package. I believe there are much better ways.

\documentclass{article}
\usepackage{collection}

\collectionnew{sEvents}

\def\schedulelines{}%
\collectionloop{sEvents}{%
\edef\schedulelines{%
\unexpanded\expandafter{\schedulelines}%
\romannumeral-Q\collectionloopkey
&%
\romannumeral-Q\collectionloopitem
\unexpanded{\\ \hline}%
}%
}

\begin{document}
\begin{tabular}{|l|c|l|}
\hline\textbf{col1}&\textbf{col2}&\textbf{col3}\\
\hline
\schedulelines
\end{tabular}
\end{document}


A different realization that avoids most expansion problems.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\definecollection}{m}
{
\prop_new:c { l_thomak_collection_#1_prop }
}
{
\prop_put:cnn { l_thomak_collection_#1_prop } { #2 } { #3 }
}
\NewDocumentCommand{\loopcollection}{mm}
{
\cs_set:Nn \__thomak_collection_do:nn { #2 }
\prop_map_function:cN { l_thomak_collection_#1_prop } \__thomak_collection_do:nn
}
\NewDocumentCommand{\clearcontainer}{m}
{
\tl_clear_new:N #1
}
\NewDocumentCommand{\appendto}{mm}
{
\tl_put_right:Nn #1 { #2 }
}

\ExplSyntaxOff

\definecollection{sEvents}

\clearcontainer{\schedulelines}

% #1 and #2 substitute \collectionloopkey and \collectionloopitem
\loopcollection{sEvents}{\appendto\schedulelines{#1 & #2 \\ \hline}}

\begin{document}

\begin{tabular}{|l|c|l|}
\hline
\textbf{col1}&\textbf{col2}&\textbf{col3}\\
\hline
\schedulelines
\end{tabular}

\end{document}

• Is there a way to do it without edef? I suppose it doesn't really matter, but it seems like it should be doable, and not too complicated – AThomack Aug 14 '18 at 21:32
• What does Q do in this code? – AThomack Aug 14 '18 at 21:32
• Is there a class that you would use besides collections which has similar functionality but is documented. I used it because a CV package I was using required it previously so I was a little familiar. – AThomack Aug 14 '18 at 21:34
• @AThomack \romannumeral-Q triggers expansion of the following tokens (an interesting trick). I added a different solution. – egreg Aug 14 '18 at 21:41
• Any idea why my tabular had the right number of rows and columns but was blank? – AThomack Aug 17 '18 at 19:24