In my document, I'm using one command to create numbered elements and another command to group all elements up to the next grouping command. I'm trying to create a table that shows these groupings, as in the following example:
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{x}{>{\centering\arraybackslash\hspace{0pt}}p{0.9cm}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcounter{Elements}
\setcounter{Elements}{1}
\newcommand{\newgrouping}[1]{#1 starts.\\}
\newcommand{\newelement}{Element \theElements.\\ \addtocounter{Elements}{1}}
\begin{document}
\newgrouping{Some}
\newelement
\newelement
\newgrouping{Stuff}
\newelement
\newelement
\newelement
\newgrouping{Here}
\newelement
First table:
\begin{tabular}{|@{}c@{}|@{}c@{}|@{}c@{}|}
\hline
\begin{tabularx}{2cm}{Y|Y} 1 & 2 \end{tabularx} & \begin{tabularx}{2cm}{Y|Y|Y} 3 & 4 & 5 \end{tabularx} & \begin{tabularx}{2cm}{Y} 6 \end{tabularx} \\
\hline
Some & Stuff & Here \\ \hline
\end{tabular}
\vspace{3em}
Second table:
\begin{tabular}{|x|x|x|x|x|x|}
\hline
1 & 2 & 3 & 4 & 5 & 6 \\
\hline
\multicolumn{2}{|c|}{Some} & \multicolumn{3}{c|}{Stuff} & \multicolumn{1}{c|}{Here} \\ \hline
\end{tabular}
\end{document}
You can see the output in this image:

In the above code, the tables are hard-coded. I want them to automatically adapt to new groupings and elements. I would prefer the first table version, but since I believe the second version is somewhat simpler, I post my unfinished code for building the second table version.
With the help of some other posts, I got this far (code should compile). Now I'm unable to create the second row of the table automatically:
\documentclass{article}
\usepackage{tabularx}
\newcolumntype{x}{>{\centering\arraybackslash\hspace{0pt}}p{0.9cm}}
\makeatletter
\newcolumntype{\expand}{}
\long\@namedef{NC@rewrite@\string\expand}{\expandafter\NC@find}
\makeatother
\newcounter{Elements}
\setcounter{Elements}{1}
\newcounter{ElementsInGroup}
\setcounter{ElementsInGroup}{0}
\newcounter{GroupNumber}
\setcounter{GroupNumber}{0}
\newcommand{\currentgroupname}{}
\newcommand{\tablealignment}{}
\newcommand{\elementnumberrow}{}
\newcommand{\groupnamesrow}{}
\newcommand{\oldgroupnamesrow}{}
\newcommand{\currentgroupnamesrow}{}
\newcommand{\newgrouping}[1]{#1 starts.\\
\setcounter{ElementsInGroup}{0}
\addtocounter{GroupNumber}{1}
\renewcommand{\currentgroupname}{#1}
\expandafter\edef\expandafter\oldgroupnamesrow\expandafter{\groupnamesrow}
}
\newcommand{\newelement}{Element \theElements.\\
\expandafter\edef\expandafter\elementnumberrow\expandafter{\elementnumberrow \ifnum\theElements=1{}\else&\fi \theElements}
\addtocounter{ElementsInGroup}{1}
\expandafter\edef\expandafter\currentgroupnamesrow\expandafter{\theElementsInGroup \currentgroupname} % !!
\expandafter\edef\expandafter\groupnamesrow\expandafter{\oldgroupnamesrow \ifnum\theGroupNumber=1{}\else,\fi \currentgroupnamesrow} % need to replace , with &
\expandafter\def\expandafter\tablealignment\expandafter{\tablealignment x|}
\addtocounter{Elements}{1}}
\begin{document}
\newgrouping{Some}
\newelement
\newelement
\newgrouping{Stuff}
\newelement
\newelement
\newelement
\newgrouping{Here}
\newelement
\groupnamesrow % prints 2Some,3Stuff,1Here
\begin{tabular}{|\expand\tablealignment}
\hline
\elementnumberrow \\
\hline
\multicolumn{2}{|c|}{Some} & \multicolumn{3}{c|}{Stuff} & \multicolumn{1}{c|}{Here}
\\ \hline
\end{tabular}
\end{document}
The problem is that, as soon as I add \multicolumn to the line with the !!-comment, I receive the error "undefined control sequence \newelement". I had similar problems when I tried to use tabularx for the first version of the table.
After reading some posts, I wonder if it is simply not possible to use multicolumn or tabularx in this context, where \edef is needed? If that is true, maybe there is some other way to achieve what I am looking for?

\xdefinstead of\edef-- your\edef'edcommand is hidden in the group which is formed by the table cells, so\newelementis invisible outside – Sep 01 '16 at 18:46\expandafter\xdef\expandafter\currentgroupnamesrow\expandafter{\multicolumn{\theElementsInGroup}{c}{\currentgroupname} } % !!produces the same error (also when I replace other\edefs with\xdef). But if I remove\multicolumnit works, as with\edef, it just doesn't produce what I need. – nyxl Sep 01 '16 at 18:53expl3features for this. It seems to be easier. – Sep 02 '16 at 16:43expl3features (I have never used them before). I'm going to answer again when it's (hopefully) working. – nyxl Sep 03 '16 at 12:00