2

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}}
\newcommand*{\collectionadd}[3][]{%
    \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}
\collectionadd[A]{sEvents}{B&C}
\collectionadd[X]{sEvents}{Y&Z}

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

AThomack
  • 115

1 Answers1

1

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

\documentclass{article}
\usepackage{collection}

\collectionnew{sEvents}
\collectionadd[A]{sEvents}{B&C}
\collectionadd[X]{sEvents}{Y&Z}

\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}

enter image description here

A different realization that avoids most expansion problems.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\definecollection}{m}
 {
  \prop_new:c { l_thomak_collection_#1_prop }
 }
\NewDocumentCommand{\addtocollection}{mmm}
 {
  \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}
\addtocollection{sEvents}{A}{B&C}
\addtocollection{sEvents}{X}{Y&Z}

\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}
egreg
  • 1,121,712
  • 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
  • @AThomack Did you try making three or more? – egreg Aug 17 '18 at 19:39
  • I tried with 6 just now and it gave a header row and six subsequent blank rows with three columns (last column missing right side boarder as before). – AThomack Aug 17 '18 at 19:51
  • Also, I'm getting an error for loopcollection because of the #1 and #2 – AThomack Aug 17 '18 at 20:55
  • Is the comment "#1 and #2 substitute \collectionloopkey and \collectionloopitem" an instruction for me or an indication of the function of the code on the line below? My code does not like the # in that line of code, but seems that \collectionloopitem would not be defined. – AThomack Aug 18 '18 at 14:59
  • @AThomack That's how it's used: where in the original you would use \collectionloopkey and \collectionloopitem, use #1 and #2 instead. – egreg Aug 18 '18 at 15:24
  • My compiler was throwing an error, saying it expected ## instead, but it still produced the correct pdf. I replaced #1 with ##1 and #2 with ##2 and it worked great without errors. – AThomack Aug 20 '18 at 15:38
  • I could not nest these loops. At least the output is not what I expected. – AThomack Aug 20 '18 at 15:39
  • @AThomack Did you ask for nesting? – egreg Aug 20 '18 at 15:56
  • No, I was just noting that it did not work. I would like to nest, and am investigating how to do so, but I thought it might be something worth noting for future readers. If you know why I cannot nest the loops, that would be great to know, but I did not ask for it. – AThomack Aug 20 '18 at 16:25