# Automatically Generate a Table with multiple rows and columns from keys in the .tex file

I would like to loop through a LaTeX document and collect every instance of a key with multiple entries.

i.e.

\begin{document}
\lipsum[1-1]
\key{name=Peter, age=50, gender=male}
\lipsum[1-1]
\key{name=Max, age=20, gender=male}
\lipsum[1-1]
\key{name=Lena, age=90, gender=female}

\makekeylist

\end{document}


\makekeylist should generate a \tabularx like table with the above entries as well as theire respective page they apear first.

My tries to do this with makeidx so far did not yield a result - and I am a bit clueless towards the right ansatz.

Thanks for all help.

\documentclass[a5paper]{article}
\usepackage{keyval,tabularx}
\usepackage{lipsum}

\makeatletter
\def\zz@table{}
\newcount\zz@count
\define@key{zz}{name}{\def\zz@name{#1}}
\define@key{zz}{age}{\def\zz@age{#1}}
\define@key{zz}{gender}{\def\zz@gender{#1}}

\newcommand\key[1]{%
\label{zz\the\zz@count}%
\setkeys{zz}{name=?,age=?,gender=?}%
\setkeys{zz}{#1}%
\xdef\zz@table{\expandafter\unexpanded\expandafter{\zz@table}%
\zz@name&\zz@age&\zz@gender&\noexpand\pageref{zz\the\zz@count}\noexpand\\}%
}

\newcommand\makekeylist{%
\begin{center}
\begin{tabularx}{\textwidth}{XXXX}
Name&Age&Gender&Page\\
\hline
\zz@table
\end{tabularx}
\end{center}}

\begin{document}
\lipsum[1-1]
\key{name=Peter, age=50, gender=male}
\lipsum[1-1]
\key{name=Max, age=20, gender=male}
\lipsum[1-1]
\key{name=Lena, age=90, gender=female}

\makekeylist

\end{document}